-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUser.java
More file actions
150 lines (137 loc) · 5.43 KB
/
User.java
File metadata and controls
150 lines (137 loc) · 5.43 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
import java.io.File;
import java.util.Scanner;
import java.io.PrintWriter;
public class User
{
//static file which contains all the user data
public static final File ACCOUNT_DATA_FILE = new File("res/userData.txt");
//data for each user.
private double milesFlown;
private boolean completedTraining; //has this user completed flight training or not?
private String username;
private String password;
private GameSettings userSettings;
//constructor for creating a NEW user. This automatically writes the user into the file.
public User(String usernameIn, String passwordIn)
{
PrintWriter fileWriter = Utils.makeWriter(ACCOUNT_DATA_FILE, true);
username = usernameIn;
password = passwordIn;
milesFlown = 0;
completedTraining = false;
userSettings = new GameSettings();
fileWriter.append("\n--");
fileWriter.append("\nu " + username);
fileWriter.append("\np " + password);
fileWriter.append("\nmilesFlown " + milesFlown);
fileWriter.append("\ncompletedTraining " + completedTraining);
fileWriter.append("\nsettings " + userSettings.toString());
fileWriter.close();
}
//private constructor which allows this class to create a custom user object based on information in the user data file.
private User(String usernameIn, String passwordIn, boolean completedTrainingIn, double milesFlownIn, GameSettings gameSettings)
{
username = usernameIn;
password = passwordIn;
milesFlown = milesFlownIn;
completedTraining = completedTrainingIn;
userSettings = gameSettings;
}
//writes the data of the User instance into ACCOUNT_DATA_FILE.
public void saveData()
{
//make the readers/writers
Scanner fileReader = Utils.makeReader(ACCOUNT_DATA_FILE);
PrintWriter fileWriter = Utils.makeWriter(ACCOUNT_DATA_FILE, false);
String line = null;
while (fileReader.hasNextLine())
{
line = fileReader.nextLine();
fileWriter.println(line);
if (line.equals("u " + username)) //if it finds the username
{
//overwrites whatever is listed under the username with updated data.
fileReader.nextLine();
fileWriter.println("p " + password);
fileReader.nextLine();
fileWriter.println("milesFlown " + milesFlown);
fileReader.nextLine();
fileWriter.println("completedTraining " + completedTraining);
fileReader.nextLine();
fileWriter.println("settings " + userSettings.toString());
break;
}
}
line = "";
//goes through the rest of the file and simply writes what it reads.
while (fileReader.hasNextLine())
{
line += fileReader.nextLine()+"\n";
}
fileWriter.write(line);
fileWriter.close();
}
//returns the user object associated with the specified username. This does not return the
//origonal user object itself, rather a clone of it using the data in ACCOUNT_DATA_FILE
//returns null if no user with the specified username was found.
public static User getUser(String username)
{
User user = null;
if (username != null)
{
Scanner fileReader = Utils.makeReader(ACCOUNT_DATA_FILE);
String line = "";
while (fileReader.hasNextLine())
{
line = fileReader.nextLine();
if (line.equals("u " + username))
{
String pass = fileReader.nextLine().substring(2);
double miles = Double.parseDouble(fileReader.nextLine().substring(11));
boolean completedTraining = Boolean.parseBoolean(fileReader.nextLine().substring(18));
//TODO: make this work with conlin conventions
String[] settingsLine = Utils.split(fileReader.nextLine(), " ");
GameSettings newSettings = new GameSettings
(
Integer.parseInt(settingsLine[1]), Integer.parseInt(settingsLine[2]),
Integer.parseInt(settingsLine[3]), Integer.parseInt(settingsLine[4]),
Integer.parseInt(settingsLine[5]), Integer.parseInt(settingsLine[6]),
Integer.parseInt(settingsLine[7]), Integer.parseInt(settingsLine[8]),
Integer.parseInt(settingsLine[9]), Double.parseDouble(settingsLine[10]),
Double.parseDouble(settingsLine[11]), Boolean.parseBoolean(settingsLine[12])
);
user = new User(line.substring(2), pass, completedTraining, miles, newSettings);
break;
}
}
}
return user;
}
//#region get/set methods
public String getPassword()
{
return password;
}
public String getUsername()
{
return username;
}
public void setCompletedTraining(boolean value)
{
completedTraining = value;
saveData();
}
public boolean getCompletedTraining()
{
return completedTraining;
}
public GameSettings getSettings()
{
return userSettings;
}
public void setMilesFlown(double miles)
{
milesFlown = miles;
}
//#endregion
}