-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankAccount.java
More file actions
55 lines (45 loc) · 1.36 KB
/
BankAccount.java
File metadata and controls
55 lines (45 loc) · 1.36 KB
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
// DO NOT MODIFY THIS FILE.
public class BankAccount {
private final String accountNumber;
private MonetaryValue balance;
public BankAccount(String accountNumber, MonetaryValue balance) {
this.accountNumber = accountNumber;
this.balance = balance;
}
public BankAccount(String accountNumber) {
this.accountNumber = accountNumber;
balance = MonetaryValue.ZERO;
}
public BankAccount(BankAccount original) {
this.accountNumber = original.accountNumber;
this.balance = original.balance;
}
public String getAccountNumber() {
return accountNumber;
}
public MonetaryValue getBalance() {
return balance;
}
public boolean deposit(MonetaryValue amount) {
if (amount.isNegative()) {
return false;
} else {
balance = balance.plus(amount);
return true;
}
}
public boolean withdraw(MonetaryValue amount) {
if (withdrawalNotAllowed(amount)) {
return false;
} else {
balance = balance.minus(amount);
return true;
}
}
protected boolean withdrawalNotAllowed(MonetaryValue amount) {
return amount.isNegative() || balance.isLessThan(amount);
}
public String toString() {
return accountNumber + " " + balance.toString();
}
}