LikeLion🦁
사자가 된 감자 1주차
potatoo
2023. 4. 5. 23:30
728x90
1주차 과제를 해보았다.
감자라서 처음에는 그냥 무조건 하드코딩으로 머리를 박고 마지막에 다 메소드로 빼서 효율 맞게 뺐는지는 모르겠다.ㅎㅎㅎㅎ
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
public class Main {
static int money = 10000;
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static HashMap<Integer,Product> map = new HashMap<>();
public static void main(String[] args) throws IOException {
map.put(1,new Product(1400,"파워에이드"));
map.put(2,new Product(900,"초코라떼"));
map.put(3,new Product(1300,"쫄병"));
map.put(4,new Product(700,"초코파이"));
map.put(5,new Product(1500,"초코송이"));
System.out.println("-----자판기에 오신 것을 환영합니다-----");
System.out.println("당신은 지금 "+money+"원을 갖고 있습니다");
while (true){
System.out.println("메뉴 고르기를 선택하시겠습니까? y or n :");
String command = br.readLine();
if(menuCheck(command)){
while (true){
System.out.println("메뉴의 번호를 정수 형태로 입력해주세요 (메뉴는 하나만 고를 수 있습니다.) :");
try{
int order = menuOrder(map);
String check = br.readLine();//구매 할 것인가?
if(check.equals("y")|| check.equals("Y")){
money=calcMoney(order,money,map);
break;
}
}
catch (NumberFormatException exception) {
System.out.println("정수 형태로 다시 입력하세요.");
}//int 가 아닌 string 타입으로 입력했을 시에
}
}//주문을 할것이면
else if(command.equals("n")||command.equals("N")){
System.out.println("자판기를 종료합니다.");
break;
}//주문을 안할 것이면 종료
else {
System.out.println("***제대로 확인해주세요***");
}
}
}
public static boolean menuCheck(String command){
if(command.equals("y")||command.equals("Y")){
System.out.println(
"1) 파워에이드 1400\n" +
"2) 초코라떼 900\n" +
"3) 쫄병 1300\n" +
"4) 초코파이 700\n"+
"5) 초코송이 1500\n"
);
return true;
}
return false;
}
public static int menuOrder(HashMap<Integer,Product> map) throws IOException {
while (true){
int order = Integer.parseInt(br.readLine());
if(order>5 || order<0){
System.out.println("***메뉴 번호를 확인 해주세요***");
continue;
}
if(order==1){
System.out.printf("[%s]를 구매하시겠습니까? y or n :\n",map.get(1).getName());
}
else if (order==2) {
System.out.printf("[%s]를 구매하시겠습니까? y or n :\n",map.get(2).getName());
}
else if (order==3) {
System.out.printf("[%s]을 구매하시겠습니까? y or n :",map.get(3).getName());
}
else if (order==4) {
System.out.printf("[%s]을 구매하시겠습니까? y or n :",map.get(4).getName());
}
else {
System.out.printf("[%s]을 구매하시겠습니까? y or n :",map.get(5).getName());
}
return order;
}
}
public static int calcMoney(int order, int money, HashMap<Integer,Product> map){
money = money-map.get(order).getPrice();
if(money<0){
System.out.println("예산이 부족합니다");
System.out.println("프로그램이 종료됩니다.");
System.exit(0);
}
System.out.println("당신은 지금 "+money+"원을 가지고 있습니다");
return money;
}
}
class Product{
private int price;
private String name;
public Product(int price, String name) {
this.price = price;
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
728x90