-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUser.java
More file actions
95 lines (88 loc) · 3.21 KB
/
User.java
File metadata and controls
95 lines (88 loc) · 3.21 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
import java.util.*;
import java.security.*;//MessageDigest,NoSuchAlgorthmException
import java.lang.*;
public class User{
private String firstName; //first name of user
private String lastName;//last name of user
private String uuid;//ID no of user
private byte pinHash[];//MD5 hash of user's pin number
private ArrayList<Account>accounts;//list of accounts for this user
/**
* Create a new user
* @param firstName - user's first name
* @parm lastName - user's last name
* @param pin - the user's account pin
* @param theBank - the Bank object that user is customer of
*/
public User(String firstName,String lastName,String pin,Bank theBank){
//set the user name
this.firstName = firstName;
this.lastName = lastName;
//store the pin's MD5 hash, rather than original value for security purpose
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 ID for user
this.uuid = theBank.getNewUserUUID();
//create emptylist of accounts
this.accounts = new ArrayList<Account>();
//Print log message
System.out.println("New User : "+firstName+" "+lastName+" with ID : "+this.uuid+" created");
}
/**
* @param anAct the account to add
*/
public void addAccount(Account anAct){
this.accounts.add(anAct);
}
/**
* return's the user's uuid
*/
public String getUUID(){
return this.uuid;
}
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");
e.printStackTrace();
System.exit(1);
}
return false;
}
public String getFirstName(){
return this.firstName;
}
public void printAccountSummary(){
System.out.println(this.firstName+"'s account Summary :");
for(int a = 0;a<this.accounts.size();a++){
System.out.println((a+1)+" "+this.accounts.get(a).getSummaryLine());
}
System.out.println();
}
public int numAccounts(){
return this.accounts.size();
}
/**
* @param actInx the index of the account to use
*/
public void printActTransHistory(int actIdx){
this.accounts.get(actIdx).printTransHistory();
}
public double getAccountBalance(int actIdx){
return this.accounts.get(actIdx).getBalance();
}
public String getActUUID(int actIdx){
return this.accounts.get(actIdx).getUUID();
}
public void addActTransaction(int actIdx,double amount,String memo){
this.accounts.get(actIdx).addTransaction(amount,memo);
}
}