-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankAccount.java
More file actions
96 lines (82 loc) · 3.24 KB
/
BankAccount.java
File metadata and controls
96 lines (82 loc) · 3.24 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
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
import java.util.ArrayList;
public abstract class BankAccount implements Comparable<BankAccount> {
private int accountNumber;
private Name name;
protected MonetaryValue balance;
private ArrayList<Transaction> arrListTransaction;
public BankAccount() {
this(0, new Name(), new MonetaryValue());
}
public BankAccount(int accountNumber, Name name, MonetaryValue balance) {
this.accountNumber = accountNumber;
this.name = name;
this.balance = balance;
arrListTransaction = new ArrayList<>();
Transaction t = new Transaction("Create new Account", new MonetaryValue(0), new MonetaryValue(balance), true);
arrListTransaction.add(t);
}
public BankAccount(BankAccount oldAccount) {
this.accountNumber = oldAccount.accountNumber;
this.name = oldAccount.name;
this.balance = oldAccount.balance;
this.arrListTransaction = oldAccount.arrListTransaction;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof BankAccount) {
BankAccount other = (BankAccount) obj;
return this.accountNumber == other.accountNumber && this.balance.equals(other.balance)
&& this.name.equals(other.name) && this.arrListTransaction.equals(other.arrListTransaction);
} else {
return false;
}
}
@Override
public String toString() {
return "Account Number: " + accountNumber + "\n" + "Name: " + name + "\n" + "Balance:" + balance;
}
public int getAccountNumber() {
return accountNumber;
}
public MonetaryValue getBalance() {
return balance;
}
public boolean deposit(MonetaryValue money) {
if (money.isNegative()) {
arrListTransaction.add(new Transaction("Deposit", money, new MonetaryValue(balance), false));
return false;
} else {
balance.add(money);
arrListTransaction.add(new Transaction("Deposit", money, new MonetaryValue(balance), true));
return true;
}
}
public boolean withdraw(MonetaryValue money) throws InsufficientFundException {
if (money.isNegative()) {
System.out.println("ERROR: cannot withdraw negative amount");
arrListTransaction.add(new Transaction("Withdraw", money, new MonetaryValue(balance), false));
return false;
} else if (money.isGreaterThan(availableAmount())) {
arrListTransaction.add(new Transaction("Withdraw", money, new MonetaryValue(balance), false));
throw new InsufficientFundException(money, availableAmount());
} else {
balance.subtract(money);
arrListTransaction.add(new Transaction("Withdraw", money, new MonetaryValue(balance), true));
return true;
}
}
@Override
public int compareTo(BankAccount other) {
if (this.accountNumber > other.accountNumber) {
return 1;
} else if (this.accountNumber < other.accountNumber) {
return -1;
} else {
return 0;
}
}
public ArrayList<Transaction> getArrayListTransaction() {
return arrListTransaction;
}
public abstract MonetaryValue availableAmount();
}