Repository navigation
Expand file tree
/
Copy pathBankAccount.java
More file actions
74 lines (60 loc) · 1.89 KB
/
Copy pathBankAccount.java
File metadata and controls
74 lines (60 loc) · 1.89 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
import java.util.Random;
import java.util.ArrayList;
import java.util.List;
// ### Inheritance ###
public abstract class BankAccount {
private String user;
private String password;
private int accountNumber;
private double balance;
private List<Transaction> transactionHistory;
public BankAccount(String user, String password) {
this.user = user;
this.password = password;
Random rand = new Random();
this.accountNumber = 100000000 + rand.nextInt(900000000);
this.balance = 0.00;
// add this new account instance to our database
AccountInfo.accountDatabase.add(this);
this.transactionHistory = new ArrayList<>(); // initialize list of transaction history
}
// Helper method to add a completed transaction to the log
public void addTransaction(Transaction tx) {
this.transactionHistory.add(tx);
}
// helper for updating balance after deposit
public void credit(double amount) {
if (amount > 0) {
this.balance += amount;
}
}
// helper for decrementing balance by withdrawal amt
public void debit(double amount) {
if (amount > 0 && this.balance >= amount) {
this.balance -= amount;
}
}
// helper for changing password
public void changePassword(String newPassword) {
if (newPassword != null && !newPassword.trim().isEmpty()) {
this.password = newPassword;
}
}
// Getters
// getter for transaction history
public List<Transaction> getTransactionHistory() {
return this.transactionHistory;
}
public String getUser() {
return this.user;
}
public String getPass() {
return this.password;
}
public int getAccountNumber() {
return this.accountNumber;
}
public double getBalance() {
return this.balance;
}
}