ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 자바 콘솔 -커피머신
    미니 2023. 3. 9. 11:36
    import java.util.Scanner;
    
    
    
    public class CoffeeMachine {
        public static void main(String[] args) {
            CoffeeMachine a = CoffeeMachine.getCoffeeMachine();
            a.On();
        }
        private int[] igre = new int[5];
        private enum selection {
            ESPRESSO(new int[]{250,0,16,4}), LATTE(new int[]{350,75,20,7}), CAPPUCCINO(new int[]{200,100,12,6});
            private int[] ing = new int[4];
            private selection(int[] re){
                this.ing = re;
            }
            public int[] getIng(){
                return ing;
            }
        }
        private CoffeeMachine() {
            igre[0] = 400;
            igre[1] = 540;
            igre[2] = 120;
            igre[3] = 9;
            igre[4] = 550;
        }
        public static CoffeeMachine getCoffeeMachine() {
            return new CoffeeMachine();
        }
        public void On() {
            Scanner s = new Scanner(System.in);
            String input;
            while (true) {
                System.out.println("Write action (buy, fill, take, remaining, exit): ");
                input = s.next();
                switch (input) {
                    case "buy":
                        buy();
                        break;
                    case "fill":
                        fills();
                        break;
                    case "take":
                        takes();
                        break;
                    case "remaining":
                        state();
                    case "exit":
                        break;
                }
                if (input.equals("exit")) break;
            }
        }
        private void state() {
            System.out.println("The coffee machine has:\n"
                    + igre[0] + " ml of water\n"
                    + igre[1] + " ml of milk\n"
                    + igre[2] + " g og coffee beans\n"
                    + igre[3] + " disposable cups\n"
                    + "$" + igre[4] + " of money\n");
        }
        private void takes() {
            System.out.println("I gave you $" + igre[4]);
            igre[4] = 0;
        }
        private void fills() {
            Scanner s = new Scanner(System.in);
            System.out.println("Write how many ml of water you want to add:");
            igre[0] += s.nextInt();
            System.out.println("Write how many ml of milk you want to add");
            igre[1] += s.nextInt();
            System.out.println("Write how many grams of coffee beans you want to add:");
            igre[2] += s.nextInt();
            System.out.println("Write how many disposable cups you want to add:");
            igre[3] += s.nextInt();
        }
        private void buy() {
            System.out.println("What do you want to buy? 1 - espresso, 2 - latte, 3 - cappuccino, back - to main menu:");
            Scanner s = new Scanner(System.in);
            String input = s.next();
    
            switch (input) {
                case "1":
                    if (!cantmake(selection.ESPRESSO)) {
                        igre[3]--;
                        igre[4] += 4;
                    }
                    break;
    
                case "2":
                    if (!cantmake(selection.LATTE)) {
                        igre[3]--;
                        igre[4] += 7;
                    }
                    break;
    
                case "3":
                    if (!cantmake(selection.CAPPUCCINO)) {
    
                        igre[3]--;
                        igre[4] += 6;
                    }
                    break;
    
                case "back":
                    break;
            }
        }
        private boolean cantmake(selection coffee) {
            String ok_c ="I have enough resources, making you a coffee!";
            boolean state = false;
            String[] Resource = {"water","milk","beans"};
            String shortResource ="";
            int[] re = coffee.getIng();
    
            if(igre[0]>=re[0] && igre[1] >=re[1] && igre[2] >= re[2]){
                System.out.println(ok_c);
                for(byte i=0; i<3 ;i++){
                    igre[i] -= re[i];
                }
                return state;
            }
            for(byte i=0; i<3 ;i++){
                if(igre[i] - re[i] <0){
                    shortResource += shortResource.length() == 0 ? Resource[i]:","+Resource[i];
                }
            }
            System.out.println("Sorry, not enough "+shortResource+"!");
    
            return !state;
    
        }
    
    }
    

     

    문법 연습 겸 3가지 커피를 뽑을 수 있는 커피머신을 만들어 보았다.

     

    -커피머신을 켠다.

    -메뉴를 선택한다.

    ----------------메뉴의 선택--------------------------

    -remaining -> 현재 남아있는 재료 및 현금과 컵 상태 확인

    -buy -> 세 종류의 커피를 살 수 있다. 각각의 종류의 커피에 필요한 재료는 다르며, 

    재료가 부족하면, 만들 수 없다는 문구와 어떤 재료가 부족한지 알려준다

    재료가 충분하면, 커피를 만든다.

    -fills ->재료를 채운다

    -takes -> 돈을 빼간다

    ---------종료------------------------------------------

    -exit -> 커피머신을 종료한다.

     

    아주아주 간단한 콘솔 커피머신이다.

     

    연습한것 

    -enum,switch 등

     

    개선할 사항

    -좀 더 깔끔하게 코드를 정리하고 싶다.. 중복되는 부분이 눈에 보인다. 

    -다음에는 자바17에 text blocks를 이용해서 메뉴출력이나, 남은 재료 출력 등을 좀 더 깔끔하게 정리해보자.

    -메뉴 선택 시에 유효성 검사가..

     

    '미니' 카테고리의 다른 글

    Naver Maps openAPI (1) -JSON  (0) 2023.06.23
    자바 - BattleShip Game  (0) 2023.05.14
    자바 콘솔 -bulls and cows 게임  (0) 2023.03.10
Designed by Tistory.