-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUser.java
97 lines (84 loc) · 2.32 KB
/
User.java
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
/*
* User class reads the user.txt file, loads and manages the user data
*/
package natasha;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import static natasha.Natasha.print;
/**
*
* @author garyanewsome
*/
public class User {
Scanner keyboard = new Scanner(System.in);
private String name;
private String pronoun;
private String filePath;
private Scanner input = null;
private List<String> lines;
private String[] fileLines;
public User(String filePath) {
this.name = name;
this.pronoun = pronoun;
this.fileLines = null;
this.filePath = filePath;
}
public void loadUser() {
File inputFile = new File(filePath);
try {
this.input = new Scanner(inputFile);
} catch (FileNotFoundException ex) {
Logger.getLogger(PasswordReader.class.getName()).log(Level.SEVERE, null, ex);
}
this.lines = new ArrayList();
while (this.input.hasNext()) {
String line = this.input.nextLine();
if (line.charAt(0) != '#') { // excludes commented header lines
this.lines.add(line);
}
for (int i = 0; i < this.lines.size(); i++) {
fileLines = line.split(";");
}
}
}
public String getName() {
this.name = fileLines[0];
return this.name;
}
public String getPronoun() {
this.pronoun = fileLines[1];
return this.pronoun;
}
public String setUser(){
print("Please enter new user name: ");
this.name = keyboard.nextLine();
print("Please enter your pronoun of choice (ex. Sir or Ma'am): ");
this.pronoun = keyboard.nextLine();
FileWriter userWriter = null;
PrintWriter outFile = null;
try {
userWriter = new FileWriter("user.txt", false);
outFile = new PrintWriter(userWriter);
} catch (IOException ex) {
Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
}
outFile.println("# User data file");
outFile.println("# name;pronoun;");
outFile.print(this.name + ";" + this.pronoun + ";");
try {
userWriter.close();
} catch (IOException ex) {
Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
}
return this.pronoun;
}
}