-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccount.java
More file actions
108 lines (87 loc) · 2.33 KB
/
Account.java
File metadata and controls
108 lines (87 loc) · 2.33 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
97
98
99
100
101
102
103
104
105
106
107
108
import java.util.ArrayList;
public class Account {
// Name of account
private String name;
// Account ID number
private String uuid;
// The User object that owns this account
private User holder;
// The list of transactions for this account
private ArrayList<Transaction> transactions;
/**
* Create a new account
*
* @param name
* Name of the account
* @param holder
* User object that holds this account
* @param bank
* Bank that issues the account
*/
public Account(String name, User holder, Bank bank) {
// Set the account name and holder
this.name = name;
this.holder = holder;
// Get the new account UUID
this.uuid = bank.getNewAccountUUID();
// Initialize the list of transactions
this.transactions = new ArrayList<Transaction>();
}
/**
* Returns account UUID
*
* @return UUID
*/
public String getUUID() {
return this.uuid;
}
public String getSummaryLine() {
// Get the account's balance
double balance = this.getBalance();
// Format the summary line depending on a negative balance or not
if (balance >= 0) {
return String.format("%s : %s : $%.02f", this.uuid, this.name,
balance);
} else {
return String.format("%s : %s : $(%.02f)", this.uuid, this.name,
balance);
}
}
/**
* Get the balance of this account by totaling the transaction amounts
*
* @return The balance value
*/
public double getBalance() {
double balance = 0;
for (Transaction t : this.transactions) {
balance += t.getAmount();
}
return balance;
}
/**
* Print the transaction history of the account
*/
public void printTransHistory(Gui gui) {
gui.appendMainDisplay(String.format(
"\n\nTransaction history for account: %s\n ", this.uuid));
for (int t = this.transactions.size() - 1; t >= 0; t--) {
gui.appendMainDisplay(this.transactions.get(t).getSummaryLine());
gui.appendMainDisplay("\n ");
}
gui.appendMainDisplay("\n\n\n");
}
/**
* Add a transaction to a particular account
*
* @param amount
* The amount transacted
* @param memo
* The transaction memo
*/
public void addTransaction(double amount, String memo) {
// Create new Transaction object and add it to our list
Transaction newTrans = new Transaction(amount, this, memo);
this.transactions.add(newTrans);
}
}