-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.java
More file actions
170 lines (143 loc) · 3.88 KB
/
User.java
File metadata and controls
170 lines (143 loc) · 3.88 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
public class User {
// User's first name
private String firstName;
// User's last name
private String lastName;
// The ID number of the user
private String uuid;
// The MD5 has of the user's pin number
private byte pinHash[];
// The list of accounts for this user
private ArrayList<Account> accounts;
/**
* Create a new user
*
* @param firstName
* User's first name
* @param lastName
* User's last name
* @param pin
* User's account pin number
* @param bank
* Bank object that the user is a customer of
*/
public User(String firstName, String lastName, String pin, Bank bank) {
// Set user's name
this.firstName = firstName;
this.lastName = lastName;
// Store the pin's MD5 has, rather than the original value
// for security purposes
try {
MessageDigest md = MessageDigest.getInstance("MD5");
this.pinHash = md.digest(pin.getBytes());
} catch (NoSuchAlgorithmException e) {
System.out.println("NoSuchAlgorithmException caught");
e.printStackTrace();
}
// Get a new, unique universal ID for the user
this.uuid = bank.getNewUserUUID();
// Create empty list of accounts
this.accounts = new ArrayList<Account>();
// Print out a log message
System.out.printf("Created test user %s %s.\n" + "Bank: %s\n"
+ "User ID: %s\n" + "PIN: %s ", firstName, lastName,
bank.getName(), this.uuid, pin);
}
/**
* Add an acocunt for the user
*
* @param account
* The account to add
*/
public void addAccount(Account account) {
this.accounts.add(account);
}
/**
* Return user's UUID
*
* @return UUID
*/
public String getUUID() {
return this.uuid;
}
/**
* Check whether a given pin matches the true USer pin
*
* @param pin
* The ping to check
* @return Whether the pin is valid or not
*/
public boolean validatePin(String pin) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
return MessageDigest.isEqual(md.digest(pin.getBytes()),
this.pinHash);
} catch (NoSuchAlgorithmException e) {
System.out.println("NoSuchAlgorithmException caught");
e.printStackTrace();
}
return false;
}
/**
* Return the first name of the user
*
* @return The first name
*/
public String getFirstName() {
return this.firstName;
}
public void printAccountsSummary(Gui gui) {
gui.appendMainDisplay(String.format("%s's accounts summary:\n ",
this.firstName));
for (int a = 0; a < this.accounts.size(); a++) {
gui.appendMainDisplay(String.format("%d) %s\n ", a + 1,
this.accounts.get(a).getSummaryLine()));
}
System.out.println();
}
/**
* Get the number of accounts the user has
*
* @return The number of accounts
*/
public int numAccounts() {
return this.accounts.size();
}
/**
* Print transaction history for a particular account
*
* @param accountIdx
* The index of the account to use
* @param gui
* The Gui object used for user input
*/
public void printAccountSummary(int accountIndex, Gui gui) {
this.accounts.get(accountIndex).printTransHistory(gui);
}
/**
* Gets the balance for a particular account
*
* @param accountIndex
* The index of the account to use
* @return The balance of the account
*/
public double getAccountBalance(int accountIndex) {
return this.accounts.get(accountIndex).getBalance();
}
/**
* Get the UUID of a particular account
*
* @param accountIndex
* The index of the account to use
* @return The UUID of the account
*/
public String getAcctUUID(int accountIndex) {
return this.accounts.get(accountIndex).getUUID();
}
public void addAcctTransaction(int accountIndex, double amount, String memo) {
this.accounts.get(accountIndex).addTransaction(amount, memo);
}
}