-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.java
More file actions
301 lines (260 loc) · 9.74 KB
/
User.java
File metadata and controls
301 lines (260 loc) · 9.74 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
package flashcards.model;
import static flashcards.drivers.ProgramDriver.verifyUserPassword;
import static flashcards.drivers.ProgramDriver.generateSecurePassword;
import static flashcards.drivers.ProgramDriver.hash;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* A class which models a user of the FlashCards program. Collects all of the
* user's subjects and flash cards in one place, and allows for saving and
* loading this information from file.
*
* @author Daniel Nedrow
*/
public class User {
private String username, password;
private String salt; // not currently using password encrypt. Will later.
private String encryptedPassword;
private ArrayList<Subject> subjects;
private int numSubjects;
/**
* Creates new user with given username and password.
*
* @param username user's username
* @param password user's password
*/
public User(String username, String password) {
this.username = username;
this.password = password;
subjects = new ArrayList<>();
}
/**
* @return ArrayList containing user's subjects
*/
public ArrayList<Subject> getSubjects() {
return subjects;
}
/**
* Adds a given subject for this user.
*
* @param subject subject to be added for this user
*/
public void addSubject(Subject subject) {
subjects.add(subject);
numSubjects++;
}
public void deleteSubject(Subject subject) {
subjects.remove(subject);
numSubjects--;
}
/**
* Returns the given users username.
*
* @return Username
*/
public String getUsername() {
return username;
}
/**
* @return the current number of subjects for this user
*/
public int getNumSubjects() {
return numSubjects;
}
//set encrypted password
public void setEncryptedPassword(String encryptedPassword) {
this.encryptedPassword = encryptedPassword;
}
//get encrypted password
public String getEncryptedPassword() {
return this.encryptedPassword;
}
//set salt
public void setSalt(String salt) {
this.salt = salt;
}
//get salt
public String getSalt() {
return this.salt;
}
/**
* Save the user's subjects, flash cards, and right/wrong history to file.
*/
public void saveUserState() {
// make a new directory (if it doesn't exist) to hold user save files
new File(System.getProperty("user.dir") + "/userStates").mkdirs();
String filename = System.getProperty("user.dir") + "/userStates/"
+ username + ".txt";
PrintWriter output;
ArrayList<FlashCard> flashcards;
try {
output = new PrintWriter(filename);
// output each of user's subjects to file
for (Subject subject : subjects) {
output.println("Subject: " + subject.getTitle());
// output each of subject's flash cards to file
flashcards = subject.getFlashCards();
for (FlashCard flashcard : flashcards) {
output.print(flashcard);
}
}
output.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* Load the user's subjects, flash cards, and right/wrong history from file.
*/
public void loadUserState() {
String filename = System.getProperty("user.dir") + "/userStates/"
+ username + ".txt";
File userState = new File(filename);
Scanner input;
String inputLine;
FlashCard flashcard;
try {
input = new Scanner(userState);
// read each line of user's save file to load user's state
while (input.hasNextLine()) {
inputLine = input.nextLine();
if (inputLine.startsWith("Subject: ")) {
// ignore "Subject: " and just load the subject's title
inputLine = inputLine.substring("Subject: ".length());
addSubject(new Subject(inputLine));
} else {
// the input line has the data to create a flash card
flashcard = FlashCard.fromString(inputLine);
subjects.get(numSubjects - 1).addFlashCard(flashcard);
}
}
} catch (FileNotFoundException ex) {
try {
userState.createNewFile(); // if the file didn't exist, create
} catch (IOException ex1) {
Logger.getLogger(User.class.getName()).log(Level.SEVERE, null,
ex1);
}
}
}
/**
* Checks if username is already taken. Used during user registration.
*
* @param potentialUsername user's desired username
* @return true if username already taken, false otherwise
*/
public static boolean isDuplicateUser(String potentialUsername) {
File userList = new File("users.txt");
Scanner input;
String usernameAndPassword, usernameOnly;
if (!userList.exists()) {
return false; // this is the first user created, no worries
} else {
try {
input = new Scanner(userList);
while (input.hasNextLine()) {
usernameAndPassword = input.nextLine();
usernameOnly = usernameAndPassword.split(" ")[0];
if (potentialUsername.equals(usernameOnly)) {
return true;
}
}
} catch (FileNotFoundException ex) {
Logger.getLogger(User.class.getName()).log(Level.SEVERE, null,
ex);
}
}
return false; // this username was not found as duplicate
}
/**
* Save user's registered username and password to file users.txt
*
* @return true if successfully saved, false otherwise
*/
public boolean saveUsernameAndPassword() {
File userList = new File("users.txt");
PrintWriter output;
String oldFileContents = "";
// if there is already users.txt, hold old content to copy to new file
if (userList.exists()) {
oldFileContents = getOldFileContents(userList);
}
try {
output = new PrintWriter(userList);
output.print(oldFileContents); // copy previous user list to file
output.println(username + " " + salt + " " + encryptedPassword); //add this user
//output.println(username + " " + password); // add this user
output.close();
return true; // username, password saved
} catch (FileNotFoundException ex) {
return false; // username, password not saved due to error
}
}
/**
* Allows user to log in with existing username, password. If credentials
* are correct, load user's state.
*
* @param typedUsername username that user is attempting to login with
* @param typedPassword password that user is attempting to login with
* @return User object containing this user's flash cards, subjects, history
*/
public static User login(String typedUsername, String typedPassword) {
File userList = new File("users.txt");
String[] usernamePasswordEntry;
User theUser;
try {
Scanner input = new Scanner(userList);
// check users.txt to see if provided credentials are correct
while (input.hasNextLine()) {
usernamePasswordEntry = input.nextLine().split(" ");
if (typedUsername.equals(usernamePasswordEntry[0])) {
if (verifyUserPassword(typedPassword,
usernamePasswordEntry[1],
usernamePasswordEntry[2])) {
theUser = new User(typedUsername, typedPassword);
theUser.loadUserState(); // load user's flash cards etc.
return theUser;
}
/* OLD CODE
if (typedPassword.equals(usernamePasswordEntry[1])) {
theUser = new User(typedUsername, typedPassword);
theUser.loadUserState(); // load user's flash cards etc.
return theUser;
}
*/
}
}
return null; // we couldn't find username/password combo in file
} catch (FileNotFoundException ex) {
return null; // there are currently no registered users to login
}
}
/**
* Helper method to get old contents from file (so that they can later be
* copied to newly written file, and not simply be overwritten).
*
* @param file file with old contents we want to keep
* @return a String containing all contents of file
*/
public static String getOldFileContents(File file) {
Scanner input;
String oldFileContents = "";
try {
input = new Scanner(file);
while (input.hasNextLine()) {
oldFileContents += input.nextLine() + "\n";
}
input.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
}
return oldFileContents;
}
}