문제1. 커피 메뉴를 출력하고, 사용자의 입력에 따라 총 계산값을 구하여 출력해보자.

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
package whileex;
 
import javax.swing.JOptionPane;
 
public class WhileTest01 {
    public static void main(String[] args) {
 
        String menu = "*** 커피 메뉴 ***\n";
        menu += "1. 아메리카노    2000원\n";
        menu += "2. 카페라떼    3500원\n";
        menu += "3. 카라멜 마끼아또        5000원\n";
        menu += "4. 자바칩 프라프치노    6500원\n";
        menu += "5. 프로그램 종료\n";
        
        int sum = 0;
        int exit = 0;
        String bill = "*** 영수증 ***\n";
        
        while(true) {
            
            int choice = Integer.parseInt(JOptionPane.showInputDialog(menu + "커피를 선택하세요."));
            
            switch (choice) {
            case 1:
                sum += 2000;
                bill += "아메리카노    2000원\n";
                break;
                
            case 2:
                sum += 3500;
                bill += "카페라떼    3500원\n";
                break;
                
            case 3:
                sum += 5000;
                bill += "카라멜 마끼아또    5000원\n";
                break;
                
            case 4:
                sum += 6500;
                bill += "자바칩 프라프치노    6500원\n";
                break;
                
            case 5:
                exit = -1;
                break
                
            default:
                JOptionPane.showMessageDialog(null"잘못된 입력입니다.");
            } // switch
            
            if (exit == -1) {
                JOptionPane.showMessageDialog(null"프로그램 종료");
                break;
            }
        } // while
        
        bill += "--------------------\n";
        bill += "총 합계:         " + sum + "원";
        JOptionPane.showMessageDialog(null, bill);
        
    }
}
cs




문제2. 은행업무인 '1)입금 2)출금 3)잔액조회 4)종료' 를 메뉴에 나타내고,

   출금의 경우에서 인출 금액이 잔액보다 클 경우 안내 문구를 내보낸다.

   이때 입금, 출금, 잔액조회에서는 사용자에게 비밀번호(1234)를 요구하게 된다.

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
package whileex;
 
import javax.swing.JOptionPane;
 
public class WhileTest02 {
    public static void main(String[] args) {
 
        String myPass = "1234";    // 비밀번호 설정
        String menu = "*** 코리아 ATM ***\n";
        menu += "1. 입금\n";
        menu += "2. 출금\n";
        menu += "3. 잔액조회\n";
        menu += "4. 종료\n";
        
        int money = 0;            // 잔액
        int addMoney = 0;         // 입금 금액
        int deleteMoney = 0;           // 출금 금액
        
        while (true) {
            
            int choice = Integer.parseInt(JOptionPane.showInputDialog(menu + "번호를 입력해주세요."));
            if (choice == 4){
                JOptionPane.showMessageDialog(null"프로그램을 종료합니다.");
                break;
            } 
 
            String pass = JOptionPane.showInputDialog("비밀번호를 입력해주세요");
            if (pass.equals(myPass)) {
                switch(choice) {
                case 1:
                    addMoney = Integer.parseInt(JOptionPane.showInputDialog("입금할 금액을 입력해주세요"));
                    money += addMoney;
                    break;
                    
                case 2:
                    deleteMoney = Integer.parseInt(JOptionPane.showInputDialog("출금할 금액을 입력해주세요"));
                    if (deleteMoney > money)     JOptionPane.showMessageDialog(null"잔액이 부족합니다.");
                    else                          money -= deleteMoney;
                    
                    break;
                    
                case 3:
                    break;
                } // switch
                
                JOptionPane.showMessageDialog(null"현재 잔액은 " + money + "원 입니다.");
                
            } else {
                JOptionPane.showMessageDialog(null"비밀번호를 다시 입력해주세요.");
            } // if
        } // while    
        
    }
}
 
 
cs