Java 예제: ATM
scanner, if, while, switch ~ case 문 등의 기초 문법을 활용한 간단한 ATM 프로그램입니다. 어린이들이 은행 놀이 할 때 사용하면 좋겠네요.
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
import java.util.Scanner;
public class ATM {
public static void main(String[] args) {
long deposit = 0;
while(true) {
System.out.println("<< ATM >>");
System.out.println("1. 입금");
System.out.println("2. 출금");
System.out.println("3. 조회");
System.out.println("4. 종료");
System.out.print(">> ");
Scanner s = new Scanner(System.in);
int select = s.nextInt();
// 단위: 원
switch(select){
case 1:
while(true) {
System.out.print( "\n입금할 금액을 입력하세요: ");
long currentDeposit = s.nextLong();
if (currentDeposit >= 1)
{
deposit += currentDeposit;
break;
}
else
System.out.println( "(경고) 1원 이상의 금액을 입력하세요.\n");
}
System.out.println("입금이 완료되었습니다.");
// 현재 잔액 표시
System.out.println("\n현재 잔액: ₩" + deposit + "\n");
break;
case 2:
System.out.println("\n현재 잔액: ₩" + deposit + "\n");
// 잔액이 없을 시
if ( deposit == 0) {
System.out.println("(경고) 잔액이 없습니다.\n");
}
else
{
while(true) {
System.out.print( "\n출금할 금액을 입력하세요 (0: 빠져나가기): ");
long currentWithdraw = s.nextLong();
if( currentWithdraw < 0 )
System.out.println("(경고) 마이너스 단위는 출금할 수 없습니다.\n");
else if( currentWithdraw == 0)
{
System.out.println("(경고) 출금 절차를 종료합니다.\n");
break;
}
else if ( currentWithdraw > deposit)
System.out.println("(경고) 잔액보다 많은 액수를 출금할 수 없습니다.\n");
else
{
deposit -= currentWithdraw;
System.out.println("출금이 완료되었습니다.");
System.out.println("\n현재 잔액: ₩" + deposit + "\n");
break;
}
} // while 끝
}
break;
case 3:
System.out.println("\n현재 잔액: ₩" + deposit + "\n");
break;
case 4:
System.out.println("\nATM을 종료합니다.");
System.exit(0);
break;
default:
System.out.println("올바른 메뉴 번호를 입력하세요. (1~4)");
}
}
}
}
This post is licensed under
CC BY 4.0
by the author.
