-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathBankAccount.java
68 lines (61 loc) · 2.21 KB
/
BankAccount.java
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
package lecture05;
import javax.swing.*;
public class BankAccount {
// invariant: balance >= 0
private int balance;
/** BankAccount - 계좌 개설
* @param initial_amount 초기 입금 금액 (0 이상 양수)
*/
public BankAccount(int initial_amount) {
if (initial_amount < 0) {
balance = 0;
} else {
balance = initial_amount;
}
}
boolean deposit(int amount) {
boolean result = false;
if (amount >= balance) {
balance = balance + amount;
result = true;
} else {
JOptionPane.showMessageDialog(null,
"입금액에 문제가 있어서 입금이 취소되었습니다.");
}
return result;
}
boolean withdraw(int amount) {
boolean result = false;
if (amount < 0) {
JOptionPane.showMessageDialog(null, "출금액에 문제가 있어서 출금이 취소되었습니다.");
}
else if (amount > balance)
JOptionPane.showMessageDialog(null, "출금액이 잔고액보다 많아서 출금이 취소되었습니다.");
else {
balance = balance - amount;
result = true;
}
return result;
}
int getBalance() {
return balance;
}
public static void main(String[] args) {
BankAccount tester = new BankAccount(0);
System.out.println("잔액 = " + tester.getBalance());
int five = 50000;
int three = 30000;
if (tester.deposit(five))
System.out.println(five + "원 입금 성공 : 잔액 = " + tester.getBalance());
else
System.out.println(five + "원 입금 실패 : 잔액 = " + tester.getBalance());
if (tester.withdraw(three))
System.out.println(three + "원 출금 성공 : 잔액 = " + tester.getBalance());
else
System.out.println(three + "원 출금 실패 : 잔액 = " + tester.getBalance());
if (tester.withdraw(three))
System.out.println(three + "원 출금 성공 : 잔액 = " + tester.getBalance());
else
System.out.println(three + "원 출금 실패 : 잔액 = " + tester.getBalance());
}
}