forked from madonawambua/Bank-Account-Draft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.java
More file actions
executable file
·121 lines (92 loc) · 2.95 KB
/
User.java
File metadata and controls
executable file
·121 lines (92 loc) · 2.95 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
package Root;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
/**
* @author syombua
*
*/
public class User {
private String firstName;
private String lastName;
private String uuid;
private byte pinHash[];
private ArrayList<Account> accounts;
public User(String firstName, String lastName, String pin, Bank theBank){
//set users name
this.firstName = firstName;
this.lastName = lastName;
//store the pins MD5 hash, rather than the original value for security reasons;
try{
MessageDigest md = MessageDigest.getInstance("MD5");
this.pinHash = md.digest(pin.getBytes());
}catch(NoSuchAlgorithmException e){
System.err.println("Error, Caught NoSuchAlgorithmException");
e.printStackTrace();
System.exit(1);
}
//get a new unique universal IF for the user
this.uuid = theBank.getNewUserUUID();
//create empty list of accounts
this.accounts = new ArrayList<Account>();
//print log message
System.out.printf("New user %s, %s with ID %s created.\n", lastName, firstName, this.uuid);
}
//add account for users should be public
public void addAccount(Account onAcct){
this.accounts.add(onAcct);
}
//return user UUId
public String getUUID(){
return this.uuid;
}
//Check if a given pin matches the true user pin
//return whether the pin is valid or not
public boolean validatePin(String aPin){
try {
MessageDigest md = MessageDigest.getInstance("MD5");
return MessageDigest.isEqual(md.digest(aPin.getBytes()), this.pinHash);
} catch (NoSuchAlgorithmException e) {
System.err.println("Error, Caught NosuchAlgorithmException");
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(1);
}
return false;
}
public String getFirstName() {
// TODO Auto-generated method stub
return this.firstName;
}
public void printAccountsSummary() {
System.out.printf("\n\n%s Accounts summary\n", this.firstName);
for(int a = 0; a< this.accounts.size(); a++){
System.out.printf("%d. %s\n Accounts summary\n",a+1, this.accounts.get(a).getSummaryLine());
}
// TODO Auto-generated method stub
System.out.println();
}
public int numAccounts() {
// TODO Auto-generated method stub
return this.accounts.size();
}
/**
*
* @param acctIdx
*/
public void printAcctTransHistory(int acctIdx){
this.accounts.get(acctIdx).printTransHistory();
}
public double getAcctBalance(int acctIdx) {
// TODO Auto-generated method stub
return this.accounts.get(acctIdx).getBalance();
}
public String getAcctUUID(int acctIdx) {
// TODO Auto-generated method stub
return this.accounts.get(acctIdx).getUUID();
}
public void addAcctTransaction(int acctIdx, double amount, String memo) {
// TODO Auto-generated method stub
this.accounts.get(acctIdx).addTransaction(amount,memo);
}
}