간단하게 자판기를 만들어보자
조건은 이러하다.
1. 사용자가 볼 수 있게 메뉴를 표시한다.
2. 사용자는 음료를 선택할 수 있다.
3. 사용자는 지불할 금액을 입력할 수 있다.
4. 사용자는 음료를 구매하고 남은 잔액을 확인할 수 있다.
인풋은 자바의 Scanner를 사용해서 입력을 받고 나머지 구현은 Map을 사용해서 구현하면 될 것 같다.
구현
import java.text.MessageFormat;
import java.util.*;
public class VendingMachine {
public VendingMachine(){
setUp();
}
public Map<String, Integer> prices = new HashMap<>();
private void setUp() {
prices.put("사이다", 1700);
prices.put("콜라", 1900);
prices.put("식혜", 2500);
prices.put("솔의눈", 3000);
}
public void use(){
Scanner scanner = new Scanner(System.in);
printProductInfo();
System.out.println("음료수를 선택해주세요");
var drinkName = scanner.next();
if(!prices.containsKey(drinkName)) return;
System.out.println("돈을 넣어주세요");
var inputMoney = scanner.nextInt();
int price = prices.get(drinkName);
if(price > inputMoney) {
System.out.println("돈이 부족합니다.");
return;
}
System.out.println(MessageFormat.format("잔돈: {0}원", inputMoney - price));
}
private void printProductInfo() {
prices.forEach((x,y) -> {
System.out.println(MessageFormat.format("{0} : {1}원", x, y));
});
System.out.println();
}
}
public class Main {
public static void main(String[] args) {
VendingMachine machine = new VendingMachine();
machine.use();
}
}
'부트캠프 > 데일리 미션' 카테고리의 다른 글
단어 맞추기 게임을 웹페이지로 만들어 보자! (0) | 2024.07.03 |
---|---|
보너스 문제 (0) | 2024.07.01 |
Lv3. 단어 맞추기 게임 (0) | 2024.06.30 |
Lv1. 랜덤 닉네임 생성기 (0) | 2024.06.30 |