-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomerClass.java
More file actions
381 lines (350 loc) · 15.9 KB
/
Copy pathCustomerClass.java
File metadata and controls
381 lines (350 loc) · 15.9 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import java.util.*;
import java.io.*;
import java.util.regex.*;
public class CustomerClass {
private static String username;
private static String password;
private static Scanner input = new Scanner(System.in);
public static void menu() throws IOException{ //asks user if they want to login or register
int choice = 0;
do{
System.out.println("1. Login\n2. Register\n3. TestMode");
System.out.print("Enter Your Choice: ");
choice = input.nextInt();
System.out.println("--------------------");
}
while(choice < 1 || choice > 3);
if(choice == 1){
login();
}
else if(choice == 2){
register();
}
else{
Data.testingmode = true;
register();
}
}
public static void login() throws IOException{ //for login
boolean validation = false;
int threshold = 0;
do{
System.out.print("Username: ");
username = input.next();
System.out.print("Password: ");
password = input.next();
validation = validate(threshold);
threshold++;
}
while(validation == false && threshold < 4);
if(threshold == 4){
System.out.println("You have too many failed attempts, please wait a few seconds and try again.");
System.out.println("--------------------");
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
menu();
}
if(validation == true){
BankMain.menu();
}
}
public static void register() throws IOException{ //register
String newusername ="";
boolean usernameIsValid = true;
do{ //checks if username meets the requirements
System.out.println("Username has to be between 8 and 24 characters.\nUsername cannot contain special charcters.");
System.out.println("--------------------");
System.out.print("Enter a username: ");
newusername = input.next();
if(Data.testingmode == true){
usernameIsValid = true;
}
else{
usernameIsValid = usernameValidate(newusername);
}
}
while(usernameIsValid == false);
System.out.println("Username is valid!");
System.out.println("--------------------");
String newpassword ="";
boolean passwordIsValid = true;
do{ //checks if password meets the requirements
System.out.println("Password has to be at least 8 characters long.\nPassword has to contain at least one uppercase and lowercase letter.\nPassword has to contain at least one number.\nPassword has to contain at least one special character.");
System.out.println("--------------------");
System.out.print("Enter a password: ");
newpassword = input.next();
if(Data.testingmode == true){
passwordIsValid = true;
}
else{
passwordIsValid = passwordValidate(newpassword);
}
}
while(passwordIsValid == false);
System.out.println("Password is valid!");
System.out.println("--------------------");
System.out.print("Enter your first name: "); //asks for first name
String newfirstname = input.next();
System.out.println("--------------------");
System.out.print("Enter your last name: "); //asks for last name
String newlastname = input.next();
System.out.println("--------------------");
String newbirthdate = "";
int newage = 0;
do{
System.out.print("Enter your birthday(mm/dd/yyyy): "); //asks for birthdate
newbirthdate = input.next();
String month = newbirthdate.substring(0, 2);
String day = newbirthdate.substring(3, 5);
String year = newbirthdate.substring(6);
if (!(month.matches("[0-9][0-9]") && day.matches("[0-9][0-9]") && year.matches("[0-9][0-9][0-9][0-9]"))) {
System.out.println("Invalid format.\n");
continue;
}
int monthInt = Integer.parseInt(month);
int dayInt = Integer.parseInt(day);
if (monthInt > 12 || monthInt < 1) {
System.out.println("Month should be a number between 01 and 12\n");
continue;
} else if (dayInt > 31 || dayInt < 1) {
System.out.println("Day should be a number between 01 and 28, 29, 30, and 31 depending on the month.\n");
continue;
}
int birthdateyear = Integer.parseInt(newbirthdate.substring(6));
newage = 2023-birthdateyear;
if(newage < 18){
System.out.println("You are to young to make a bank account.");
UserMenu.main(null);
return;
}
else if(newage > 105){
System.out.println("You are too old to make a bank account");
UserMenu.main(null);
return;
}
}
while(newage < 18 || newage > 105);
System.out.println("--------------------");
String newssn = "";
do{
System.out.print("Enter the last 4 digits of your social security number: "); //asks for ssn
newssn = input.next();
}
while(newssn.length() != 4 && newssn.contains(newssn));
System.out.println("--------------------");
System.out.print("Enter your email: "); //asks for email
String newemail = input.next();
System.out.println("--------------------");
String pin = "";
do{
System.out.print("Enter a 4 digit pin number: "); //asks for a pin
pin = input.next();
}
while(pin.length() != 4);
System.out.println("--------------------");
System.out.println("Please make sure your information is correct."); //prints out registration info
System.out.println("Username: " + newusername);
System.out.println("Password: " + newpassword);
System.out.println("First Name: " + newfirstname);
System.out.println("Last Name: " + newlastname);
System.out.println("Birthday: " + newbirthdate);
System.out.println("Age: " + newage);
System.out.println("Social Security Number: " + newssn);
System.out.println("Email: " + newemail);
System.out.println("Pin: " + pin);
System.out.println("--------------------");
String savingsaccountnumber = "";
for(int i = 0; i < 12; i++){
int number = (int)(Math.random() * 9) + 0;
savingsaccountnumber += number;
}
String checkingsaccountnumber = "";
for(int i = 0; i < 12; i++){
int number = (int)(Math.random() * 9) + 0;
checkingsaccountnumber += number;
}
String creditcardnumber = Card.numbergenerator();
String debitcardnumber = Card.numbergenerator();
String creditcardnum = creditcardnumber.replaceAll(" ", "");
String debitcardnum = debitcardnumber.replaceAll(" ", "");
int creditscore = (int)(Math.random() * 850) + 0;
int newsafetydeposit = (int)(Math.random() * 300) + 1;
System.out.println("Registering your details...");
File file = new File("GeneralData.csv"); //describing the general file
FileWriter fw = new FileWriter(file , true);
FileReader fr = new FileReader(file);
BufferedWriter bw = new BufferedWriter(fw);
BufferedReader br = new BufferedReader(fr);
String line = "";
if((line = br.readLine()) == null){
bw.write("username[0],");
bw.write("password[1],");
bw.write("firstname[2],");
bw.write("lastname[3],");
bw.write("birthdate[4],");
bw.write("age[5],");
bw.write("ssn[6],");
bw.write("email[7],");
bw.write("safetydepositboxnumber[8]");
}
bw.newLine();
bw.write(newusername + ","); //adds general details to database
bw.write(newpassword + ",");
bw.write(newfirstname + ",");
bw.write(newlastname + ",");
bw.write(newbirthdate + ",");
bw.write(newage + ",");
bw.write(newssn + ",");
bw.write(newemail + ",");
bw.write(newsafetydeposit + "");
bw.close();
br.close();
file = new File("CustomerData.csv"); //describing the customer file
fw = new FileWriter(file , true);
fr = new FileReader(file);
bw = new BufferedWriter(fw);
br = new BufferedReader(fr);
line = "";
if((line = br.readLine()) == null){
bw.write("username[0],");
bw.write("savingsaccountnumber[1],");
bw.write("checkingsaccountnumber[2],");
bw.write("savingsbalance[3],");
bw.write("checkingsbalance[4],");
bw.write("debitcardnum[5],");
bw.write("creditcardnum[6],");
bw.write("creditscore[7]");
}
bw.newLine();
bw.write(newusername + ",");
bw.write(savingsaccountnumber + ","); //adds customer details to database
bw.write(checkingsaccountnumber + ",");
bw.write("100.00,");
bw.write("0.00,");
bw.write(creditcardnum + ",");
bw.write(debitcardnum + ",");
bw.write(creditscore + "");
bw.close();
br.close();
file = new File("Card.csv"); //describing the file
fw = new FileWriter(file , true);
fr = new FileReader(file);
bw = new BufferedWriter(fw);
br = new BufferedReader(fr);
//card info
String creditcardexp = Card.expdategenerator();
String debitcardexp = Card.cvvgenerator();
String debitcardcvv = Card.expdategenerator();
line = "";
if((line = br.readLine()) == null){
bw.write("username[0],");
bw.write("debitcardnumber[1],");
bw.write("debitcardexp[2],");
bw.write("debitcardcvv[3],");
bw.write("creditcardnumber[4],");
bw.write("creditcardexp[5],");
bw.write("pin[6]");
}
bw.newLine();
bw.write(newusername + ",");
bw.write(debitcardnumber + ",");
bw.write(debitcardexp + ",");
bw.write(debitcardcvv + ",");
bw.write(creditcardnumber + ",");
bw.write(creditcardexp + ",");
bw.write(pin + "");
bw.close();
br.close();
try {
Thread.sleep(7000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Account successfully created. Welcome, " + newfirstname + "!");
System.out.println("As a reward of registering with the " + BankLocations.bankname + ", we have deposited a $100.00 to you savings account!");
System.out.println("Redirecting you to login...");
try {
Thread.sleep(7000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("--------------------");
menu();
}
public static boolean validate(int threshold) throws IOException{ //validates username and password
boolean isValid = false;
Data.generalarray(username, 0);
if(Data.generaldata.size() != 0){
if(username.equals(Data.generaldata.get(0)) && password.equals(Data.generaldata.get(1))){ //checks for match
isValid = true;
}
}
else{
int triesremaining = 3 - threshold; //caluclates tries remaining
System.out.println("Username and Password do not match! You have " + triesremaining + " try(s) remaining!");
}
return isValid;
}
public static boolean usernameValidate(String newusername) throws IOException{ //checks if username is valid
Data.generalarray(newusername, 0);
if(Data.generaldata.size() != 0){ //checks if username already exists
System.out.println("Username is already taken! Please try another username.");
return false;
}
if(newusername.length() < 8 || newusername.length() > 24){ //checks username length is between 8 and 24 characters
System.out.println("Username has to be between 8 and 24 characters. Please try another username");
return false;
}
for(int i = 0; i < newusername.length(); i++){ //checks if username contains special characters
if(!Character.isDigit(newusername.charAt(i)) && !Character.isLetter(newusername.charAt(i))){
System.out.println("Username cannot contain special charcters. Please try another username");
return false;
}
}
return true;
}
public static boolean passwordValidate(String newpassword) throws IOException{ //checks is password is valid
boolean passwordIsValid = true;
if(newpassword.length()>=8){ //first checks if password is 8 characters long
Pattern lowercaseletter = Pattern.compile("[a-z]"); //defines lowercase letters
Pattern uppercaseletter = Pattern.compile("[A-Z]"); //defines upper letters
Pattern digit = Pattern.compile("[0-9]"); //defines all numbers for 0-9
Pattern special = Pattern.compile("[!@#$%&*()_=+|<>?{}\\[\\]~-]"); //defines all special characters
Matcher hasLowerCaseLetter = lowercaseletter.matcher(newpassword); //looks for lowercase
Matcher hasUpperCaseLetter = uppercaseletter.matcher(newpassword); //looks for uppercase
Matcher hasDigit = digit.matcher(newpassword); //looks for number
Matcher hasSpecial = special.matcher(newpassword);//looks for special chracter
boolean LowerCaseletterFind = hasLowerCaseLetter.find(); //converts to boolean
boolean UpperCaseletterFind = hasUpperCaseLetter.find(); //converts to boolean
boolean digitFind = hasDigit.find(); //converts to boolean
boolean specialFind = hasSpecial.find(); //converts to boolean
if(LowerCaseletterFind == false){ //if password is missing lowerletter
System.out.println("Password has to contain at least one uppercase and lowercase letter. Please try again.");
passwordIsValid = false;
}
if(UpperCaseletterFind == false){ //if password is uppercase letter
System.out.println("Password has to contain at least one uppercase and lowercase letter. Please try again.");
passwordIsValid = false;
}
if(digitFind == false){ //if password is missing a number
System.out.println("Password has to contain at least one number. Please try again.");
passwordIsValid = false;
}
if(specialFind == false){ //if password is special character
System.out.println("Password has to contain at least one special character. Please try again.");
passwordIsValid = false;
}
}
else{
System.out.println("Password has to be at least 8 characters long. Please try again."); //if password is not 8 characters long, it returns false
passwordIsValid = false;
}
return passwordIsValid;
}
}