-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserDatabase.java
More file actions
74 lines (64 loc) · 2.51 KB
/
Copy pathUserDatabase.java
File metadata and controls
74 lines (64 loc) · 2.51 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
import java.io.*;
import java.util.*;
public class UserDatabase {
private static final String FILE = "customerInfo.csv";
private static final String HEADER = "customerID,firstName,lastName,SSN,DOB,email," +
"phoneNumber,BankLocation,bankPhoneNumber,SavingsAccount,CheckingAccount," +
"CreditCard,creditCardLimit,creditScore,hasDebitCard,cdBalance,cdInterestRate," +
"password,securityQuestion,securityAnswer,isLocked";
private Map<String, User> usersByEmail = new LinkedHashMap<>();
public UserDatabase() {
load();
}
private void load() {
File f = new File(FILE);
if (!f.exists()) return;
try (BufferedReader br = new BufferedReader(new FileReader(f))) {
String line;
boolean firstLine = true;
while ((line = br.readLine()) != null) {
line = line.trim();
if (line.isEmpty() || firstLine) { firstLine = false; continue; }
User u = User.fromCsv(line);
if (u != null) usersByEmail.put(u.email.toLowerCase(), u);
}
} catch (IOException e) {
System.out.println("Warning: Could not read customerInfo.csv");
}
}
private void save() {
try (PrintWriter pw = new PrintWriter(new FileWriter(FILE))) {
pw.println(HEADER);
for (User u : usersByEmail.values()) {
pw.println(u.toCsv());
}
} catch (IOException e) {
System.out.println("Error: Could not save to customerInfo.csv");
}
}
public boolean emailExists(String email) {
return usersByEmail.containsKey(email.toLowerCase());
}
public User getByEmail(String email) {
return usersByEmail.get(email.toLowerCase());
}
public void addUser(User u) {
usersByEmail.put(u.email.toLowerCase(), u);
save();
}
public void updateUser(User u) {
usersByEmail.values().removeIf(x -> x.customerID.equals(u.customerID));
usersByEmail.put(u.email.toLowerCase(), u);
save();
}
public String generateAccountId() {
Random rand = new Random();
Set<String> existing = new HashSet<>();
for (User u : usersByEmail.values()) existing.add(u.customerID);
String id;
do {
id = String.format("%09d", rand.nextInt(1_000_000_000));
} while (existing.contains(id));
return id;
}
}