-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBank.java
More file actions
166 lines (139 loc) · 3.32 KB
/
Bank.java
File metadata and controls
166 lines (139 loc) · 3.32 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
import java.util.ArrayList;
import java.util.Random;
public class Bank {
// Name of the bank
private String name;
// List of users
private ArrayList<User> users;
// List of accounts
private ArrayList<Account> accounts;
/**
* Create a new Bank object with empty lists of users and accounts
*
* @param name
* The name of the bank
*/
public Bank(String name) {
this.name = name;
this.users = new ArrayList<User>();
this.accounts = new ArrayList<Account>();
}
/**
* Generate a new universally unique ID for a user.
*
* @return The new UUID
*/
public String getNewUserUUID() {
// Initialize
String uuid;
Random rng = new Random();
int length = 6;
boolean nonUnique;
// Continue generating new UUID till it's unique
do {
// Generate the number
uuid = "";
for (int c = 0; c < length; c++) {
uuid += ((Integer) rng.nextInt(10)).toString();
}
// Check UUID to make sure it's unique
nonUnique = false;
for (User u : this.users) {
if (uuid.compareTo(u.getUUID()) == 0) {
nonUnique = true;
break;
}
}
} while (nonUnique);
return uuid;
}
/**
* Gets a new universally unique identifier
*
* @return The new UUID
*/
public String getNewAccountUUID() {
// Initialize
String uuid;
Random rng = new Random();
int length = 10;
boolean nonUnique;
// Continue generating new UUID till it's unique
do {
// Generate the number
uuid = "";
for (int c = 0; c < length; c++) {
uuid += ((Integer) rng.nextInt(10)).toString();
}
// Check UUID to make sure it's unique
nonUnique = false;
for (Account a : this.accounts) {
if (uuid.compareTo(a.getUUID()) == 0) {
nonUnique = true;
break;
}
}
} while (nonUnique);
return uuid;
}
/**
* Add an account
*
* @param account
* The account to add
*/
public void addAccount(Account account) {
this.accounts.add(account);
}
/**
* Create a new user of the bank
*
* @param firstName
* The user's first name
* @param lastName
* The user's last name
* @param pin
* The user's PIN
* @return The new User object
*/
public User addUser(String firstName, String lastName, String pin) {
// Create a new User object and add it to our list
User newUser = new User(firstName, lastName, pin, this);
this.users.add(newUser);
// Create a savings account for the user and add to User and Bank
// account lists
Account newAccount = new Account("Savings", newUser, this);
newUser.addAccount(newAccount);
this.addAccount(newAccount);
return newUser;
}
/**
* Get the User object associated with a particular userID and PIN if they
* are valid
*
* @param userID
* The UUID of the user to log in
* @param pin
* The PIN of the user
* @return The User object, if the login is successful, or null otherwise
*/
public User userLogin(String userID, String pin) {
// Search through the list of users
for (User u : this.users) {
// Check if user ID is correct
if (u.getUUID().compareTo(userID) == 0 && u.validatePin(pin)) {
return u;
}
}
// If the user wasn't found or the PIN was incorrect
return null;
}
/**
* Returns the name of the bank
*
* @return The name of the bank
*/
public String getName() {
return this.name;
}
}