사자가 된 감자 1주차

2023. 4. 5. 23:30·LikeLion🦁
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

'LikeLion🦁' 카테고리의 다른 글

감자의 MT 참여하기  (3) 2023.05.16
우리 아이가 사고쳤어요~  (0) 2023.05.09
사자가 된 감자 4주차  (1) 2023.05.07
사자가 된 감자 3주차  (2) 2023.05.02
사자가 된 감자 2주차  (1) 2023.04.09
'LikeLion🦁' 카테고리의 다른 글
  • 우리 아이가 사고쳤어요~
  • 사자가 된 감자 4주차
  • 사자가 된 감자 3주차
  • 사자가 된 감자 2주차
Bello's
Bello's
개발하는 벨로
  • Bello's
    벨로의 개발일지
    Bello's
  • 전체
    오늘
    어제
    • 분류 전체보기 (199) N
      • 노예 일지 (7)
        • 스타트업 노예일지 (3)
      • CS 이론 (81)
        • 학과 수업 (4)
        • 알고리즘 (64)
        • 시스템 프로그래밍 (3)
        • 데이터 통신 (1)
        • 운영체제 (2)
        • 데이터베이스 (1)
      • project (3)
      • 나는 감자다. (4)
      • Spring (27)
      • 모각코 (45)
        • 절개와지조(모각코) (7)
        • 어쩌다보니 박준태가 조장이조 (11)
        • 어쩌다보니 박준태가 또 조장이조 (12)
      • LikeLion🦁 (20)
      • 캘리포니아 감자 (4)
      • OpenSource Contribute (1)
      • 우아한테크벨로 (1) N
        • 프리코스 회고록 (6)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    티스토리챌린지
    JPA
    자바
    누적합
    감자
    회고록
    절개와지조
    BFS
    8기
    백준
    어렵다
    타임리프
    DFS
    모각코
    오블완
    그래프 순회
    뛰슈
    나는 감자
    프리코스
    Spring
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.5
Bello's
사자가 된 감자 1주차
상단으로

티스토리툴바