🥤 자판기객체를 만들어서 돈 입력, 음료선택, 잔돈반환 등등 조건을 맞추어서 구현하시오.
package RealProject;
// 음료 객체
class Drink {
private int drinkNum;
private String name;
private int price;
public Drink(int drinkNum, String name, int price) {
setDrinkNum(drinkNum);
setName(name);
setPrice(price);
}
public String toString() {
return drinkNum + ". " + name + ": " + price + "원";
}
public int getDrinkNum() {
return drinkNum;
}
public void setDrinkNum(int drinkNum) {
this.drinkNum = drinkNum;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
// 자판기 객체
class VendingMachine {
public static final int COKE = 1;
public static final int WATER = 2;
public static final int TEA = 3;
private Drink coke = new Drink(COKE, "콜라", 1500);
private Drink water = new Drink(WATER, "물", 600);
private Drink tea = new Drink(TEA, "녹차", 1000);
private int inputMoney;
// 메뉴
public void menu() {
System.out.println("===================");
System.out.println(" Menu ");
System.out.println("===================");
System.out.println(coke + "\n" + water + "\n" + tea);
System.out.println("===================");
}
public int getInputMoney() {
return inputMoney;
}
public void setInputMoney(int inputMoney) {
this.inputMoney = inputMoney;
}
public Drink getCoke() {
return coke;
}
public void setCoke(Drink coke) {
this.coke = coke;
}
public Drink getWater() {
return water;
}
public void setWater(Drink water) {
this.water = water;
}
public Drink getTea() {
return tea;
}
public void setTea(Drink tea) {
this.tea = tea;
}
// 넣은 돈
public void intputMoney(int inputMoney) {
boolean result = true;
if(inputMoney >= 600) {
this.inputMoney += inputMoney;
System.out.println("넣은 돈: " + inputMoney + "원");
} else {
System.out.println("600원 이상 돈을 넣어주세요.");
}
}
// 음료 선택
public Drink drinkSelect(int drinkNum) {
switch(drinkNum) {
case COKE :
return drinkOutCondition(coke);
case WATER :
return drinkOutCondition(water);
case TEA :
return drinkOutCondition(tea);
default :
return null;
}
}
// 잔돈 반환
public int changeMoney(Drink drink) {
int change = inputMoney - drink.getPrice();
System.out.println("잔액: " + change + "원" );
return change;
}
// 음료 나오는 조건
public Drink drinkOutCondition(Drink drink) {
if(inputMoney < drink.getPrice()) {
return null;
}
return drink;
}
}
public class UseVendingMachine{
public static void main(String[] args) {
VendingMachine vm = new VendingMachine();
vm.menu();
vm.drinkSelect(VendingMachine.WATER);
vm.intputMoney(1500);
vm.changeMoney(vm.drinkSelect(VendingMachine.WATER));
}