-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAccount.java
90 lines (78 loc) · 2.73 KB
/
Account.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package ATMBankManager;
import java.util.ArrayList;
public class Account {
// Account details.
private String name;
private String uuid; // Account's uuid (ID). Different from user's.
private User holder; // Account holder (user who owns account).
private ArrayList<Transaction> transactions; // List of transactions. ArrayList (imported).
/**
*
* @param name
* @param holder
* @param theBank
*/
// Create a new Account. @param name (String Name of Account), @param holder (User object that owns/hold this account), @param theBank (bank that issues this Account).
public Account(String name, User holder, Bank theBank) {
// Set account name and holder.
this.name = name;
this.holder = holder;
// Get new account uuid.
this.uuid = theBank.getNewAccountUUID();
// init transactions.
this.transactions = new ArrayList<Transaction>();
}
/**
*
* @return
*/
// "String" is returned. @return uuid (account's uuid). Public method available to get access to a Private value.
public String getUUID() {
return this.uuid;
}
/**
*
* @return
*/
// Get Summary line for the account
public String getSummaryLine() {
// Get balance.
double balance = this.getBalance();
// Format the sumamry line, dependant on negative or positive balance.
if (balance >= 0) {
return String.format("%s : £%.02f : %s", this.uuid, balance, this.name); // Similar to 'printf()'. "%.02f" Wildcard for a double with 2 decimal places.
} else {
return String.format("%s : £(%.02f) : %s", this.uuid, balance, this.name); // Similar to 'printf()'. "%.02f" Wildcard for a double with 2 decimal places.
}
}
/**
*
* @return
*/
// Get and return balance amount of Account.
public double getBalance() {
double balance = 0;
for (Transaction t: this.transactions) {
balance += t.getAmount();
}
return balance;
}
// Print the Transaction history for the Account.
public void printAcctTransHistory() {
System.out.printf("\nTransaction history for account %s\n", this.uuid);
for (int i = this.transactions.size()-1; i >= 0; i--) {
System.out.println(this.transactions.get(i).getSummaryLine());
}
System.out.println();
}
/**
*
* @param amount
* @param memo
*/
// Create a new Transaction object and add it to our list.
public void addTransaction(double amount, String memo) {
Transaction newTrans = new Transaction(amount, memo, this); //
this.transactions.add(newTrans);
}
}