-
Notifications
You must be signed in to change notification settings - Fork 515
Expand file tree
/
Copy pathFileUserDataAccessObject.java
More file actions
125 lines (98 loc) · 3.83 KB
/
FileUserDataAccessObject.java
File metadata and controls
125 lines (98 loc) · 3.83 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
package data_access;
import entity.User;
import entity.UserFactory;
import use_case.clear_users.ClearUserDataAccessInterface;
import use_case.login.LoginUserDataAccessInterface;
import use_case.signup.SignupUserDataAccessInterface;
import java.io.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
public class FileUserDataAccessObject implements SignupUserDataAccessInterface, LoginUserDataAccessInterface, ClearUserDataAccessInterface {
private final File csvFile;
private final Map<String, Integer> headers = new LinkedHashMap<>();
private final Map<String, User> accounts = new HashMap<>();
private UserFactory userFactory;
public FileUserDataAccessObject(String csvPath, UserFactory userFactory) throws IOException {
this.userFactory = userFactory;
csvFile = new File(csvPath);
headers.put("username", 0);
headers.put("password", 1);
headers.put("creation_time", 2);
if (csvFile.length() == 0) {
save();
} else {
try (BufferedReader reader = new BufferedReader(new FileReader(csvFile))) {
String header = reader.readLine();
// For later: clean this up by creating a new Exception subclass and handling it in the UI.
assert header.equals("username,password,creation_time");
String row;
while ((row = reader.readLine()) != null) {
String[] col = row.split(",");
String username = String.valueOf(col[headers.get("username")]);
String password = String.valueOf(col[headers.get("password")]);
String creationTimeText = String.valueOf(col[headers.get("creation_time")]);
LocalDateTime ldt = LocalDateTime.parse(creationTimeText);
User user = userFactory.create(username, password, ldt);
accounts.put(username, user);
}
}
}
}
@Override
public void save(User user) {
accounts.put(user.getName(), user);
this.save();
}
@Override
public User get(String username) {
return accounts.get(username);
}
private void save() {
BufferedWriter writer;
try {
writer = new BufferedWriter(new FileWriter(csvFile));
writer.write(String.join(",", headers.keySet()));
writer.newLine();
for (User user : accounts.values()) {
String line = String.format("%s,%s,%s",
user.getName(), user.getPassword(), user.getCreationTime());
writer.write(line);
writer.newLine();
}
writer.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public String delete() {
StringBuilder deleted = new StringBuilder();
ArrayList<String> accountsToDelete = new ArrayList<>(accounts.keySet());
for (String account : accountsToDelete) {
accounts.remove(account);
// start a new line after each user
deleted.append(account).append("\n");
}
BufferedWriter writer;
try {
writer = new BufferedWriter(new FileWriter(csvFile));
writer.flush();
writer.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
return deleted.toString();
}
/**
* Return whether a user exists with username identifier.
* @param identifier the username to check.
* @return whether a user exists with username identifier
*/
@Override
public boolean existsByName(String identifier) {
return accounts.containsKey(identifier);
}
}