ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • (JAVA) 미니 프로젝트 - 자판기 구현하기
    JAVA 2021. 7. 2. 15:36

    1. 명세

    - 자판기 프로그램을 구현한다. 자판기의 종류는 임의로 결정하며 아래와 같은 방식을 구현한다.

    • 자판기 소개와 함께 자판기를 출력한다. ex) 1. 커피 2.주스 3.콜라
    • 돈을 입력받는다.
    • 번호를 입력하면 그에 맞는 상품이 출력된다. 여기서 재고 관리를 해야 하는데 재고가 없는 경우엔 "재고 없음"을 출력한다.
    • 이외의 실제 자판기에서 나올법한 예외들 ex) 잔액 부족, 잔돈 반환 등등 을 처리한다.
    • 위 내용 까지가 자판기 유저 인터페이스 부분이고 돈을 넣을 때, 지정해 놓은 돈을 입력하면 ex)1004 관리자 메뉴로 넘어간다.

    2. 기능

    - 관리자 메뉴에서의 기능은 아래와 같다.

    • 자판기의 메뉴를 변경하는 기능
    • 자판기의 메뉴의 가격을 변경하는 기능
    • 자판기의 메뉴의 재고를 추가하는 기능
    • 자판기의 메뉴를 추가하는 기능
    • 현재까지 자판기에 들어온 수익을 출력하는 기능

    3. 구현

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    package day08;
     
    import java.util.Scanner;
     
     
    public class Project {
        public static final int MAX = 10// 메뉴의 최대 갯수 == 10
        
        // 전역변수 선언
        static String[] box = new String[MAX];
        static int[] price = new int[MAX];
        static int[] stock = new int[MAX];
        static int count = 3// 메뉴의 갯수
        static int admin = 1004// 관리자 메뉴로 가는 비밀번호
        static int profit = 0// 총 수익
        static Scanner sc = new Scanner(System.in);
        
        // 처음 자판기를 초기화 하는 함수
        public static void initialize() {
            box[0= "장미";
            price[0= 500;
            stock[0= 3;
            box[1= "프리지아";
            price[1= 1000;
            stock[1= 3;
            box[2= "국화";
            price[2= 3000;
            stock[2= 3;
        }
        
        // 관리자 페이지를 담당하는 함수
        public static void admin() {
            while(true) {
                System.out.println("==================================================");
                System.out.println("관리자 페이지 입니다.");
                System.out.println("1: 메뉴 변경 2: 가격 변경 3: 재고 추가 4: 메뉴 추가 5: 수익 확인 (종료는 -1)");
                System.out.print("번호를 입력하세요: ");
                int num = sc.nextInt();
                System.out.println("==================================================");
                
                // 1. 메뉴 변경
                if(num == 1) {
                    System.out.print("변경하실 메뉴의 번호를 입력하세요(1~" + count + ") :");
                    int i = sc.nextInt();
                    System.out.print(box[i-1+ "를(을) 무엇으로 바꾸시겠습니까? :");
                    sc.nextLine();
                    String name = sc.next();
                    System.out.print(name + "의 가격은 얼마입니까? ");
                    int v = sc.nextInt();
                    System.out.print(name + "의 재고를 몇 개 등록하시겠습니까? : ");
                    int n = sc.nextInt();
                    
                    box[i-1= name;
                    price[i-1= v;
                    stock[i-1= n;
                    System.out.println("메뉴 변경이 완료되었습니다!");
                }
                // 2. 가격 변경
                else if(num == 2) {
                    System.out.print("가격을 변경하실 메뉴의 번호를 입력하세요(1~" + count + ") :");
                    int i = sc.nextInt();
                    System.out.print(box[i-1+ "의 가격을 얼마로 바꾸시겠습니까? :");
                    int j = sc.nextInt();
                    price[i-1= j;
                    System.out.println("가격 변경이 완료되었습니다!");
                }
                // 3. 재고 추가
                else if(num == 3) {
                    System.out.print("재고를 추가하실 메뉴의 번호를 입력하세요(1~" + count + ") :");
                    int i = sc.nextInt();
                    System.out.print(box[i-1+ "를(을) 몇 개 추가 하시겠습니까? :");
                    int j = sc.nextInt();
                    stock[i-1+= j;
                    System.out.println("재고 추가가 완료되었습니다!");
                }
                // 4. 메뉴 추가
                else if(num == 4) {
                    if(count >= MAX - 1) {
                        System.out.println("더 이상 메뉴가 들어갈 자리가 없습니다!");
                        continue;
                    }
                    else {
                        System.out.print("추가하실 메뉴의 가격을 입력하세요 : ");
                        int i = sc.nextInt();
                        System.out.print("추가하실 메뉴의 이름을 입력하세요 : ");
                        sc.nextLine();
                        String name = sc.next();
                        System.out.print(name + "의 재고는 몇 개 입니까? : ");
                        int j = sc.nextInt();
                        
                        box[count] = name;
                        price[count] = i;
                        stock[count] = j;
                        count++;
                        System.out.println("메뉴 추가가 완료되었습니다!");
                    }
                }
                
                // 5. 수익 확인
                else if(num == 5) {
                    System.out.println("현재까지의 수익은 " + profit + "입니다.");
                }
                // -1을 입력하면 다시 자판기 메뉴로 돌아감
                else if(num == -1) {
                    user();
                    return;
                }
            }
        }
        
        // 유저 인터페이스를 담당하는 함수
        public static void user() {
            Boolean first = true;
            int money = 0;
            
            while(true) {
                System.out.println("==================================================");
                System.out.println("꽃 자판기 입니다. (번호:상품(재고))");
                // 번호:메뉴(가격) - 재고 출력
                for(int i = 0 ; i < count; i++) {
                    System.out.print((i+1+ ":" + box[i] + "(" + price[i] + "W)-" + stock[i] + "개   ");
                }
        
                System.out.println();
                System.out.println("==================================================");
                
                // 처음에만 돈을 입력받고 잔돈 반환 전 까지는 돈을 입력받지 않는다.
                if(first){
                    System.out.print("돈을 넣어주세요: ");
                    money = sc.nextInt();
                    first = false;
                }
                
                // 관리자 비밀번호는 1004, 입력하면 관리자 함수로 넘어감.
                if(money == 1004) {
                    admin();
                    return;
                }
                
                System.out.print("메뉴 입력: ");
                int num = sc.nextInt();
                
                // 돈이 충분한가?
                if(money >= price[num-1]) {
                    // 재고가 충분한가?
                    if(stock[num-1> 0) {
                        money = money - price[num-1];
                        stock[num-1]--;
                        profit += price[num-1];
                        System.out.println(box[num-1+ "이(가) 나왔다!");
                    }
                    else {
                        System.out.println("현재 재고가 없습니다!");
                        continue;
                    }
                }
                else {
                    System.out.println("잔액이 부족합니다!");
                }
                
                System.out.println("잔액 : " + money);
                
                // 모든 돈을 사용하면 이용 종료 후 초기화면 진입
                if(money == 0) {
                    System.out.println("감사합니다! 다음에 또 이용해주세요!!");
                    first = true;
                    continue;
                }
                
                // 돈이 남아있다면 선택지를 준다.
                System.out.println("1: 계속 구매하기 2: 금액 추가하기 3: 잔돈 반환하기 ");
                System.out.print("번호를 입력하세요: ");
                int num1 = sc.nextInt();
                
                if(num1 == 1) {
                    continue;
                }
                else if (num1 == 2) {
                    System.out.print("돈을 넣어주세요 : ");
                    int extra = sc.nextInt();
                    money += extra;
                    System.out.println("금액이 추가되었습니다! 잔액: " + money);
                }
                
                else if(num1 == 3) {
                    System.out.println("거스름돈 " + money + "원이 반환됩니다.");
                    System.out.println("감사합니다! 다음에 또 이용해주세요!!");
                    first = true;
                }
        
            }
        }
            
        public static void main(String[] args) {
            initialize();
            user();
            return;
            
        }
    }
     
    cs

    - 실행 화면

    유저 화면
    관리자 화면, 데이터가 잘 바뀐것을 확인할 수 있다.

     

Designed by Tistory.