Skip to content

Commit 90b06e7

Browse files
authored
Merge pull request #21 from gms0817/v2.0.0-alpha
V2.0.0 alpha
2 parents 113b614 + db871ce commit 90b06e7

File tree

1 file changed

+187
-94
lines changed

1 file changed

+187
-94
lines changed

src/User.java

Lines changed: 187 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,155 +1,248 @@
1+
//Version 2.0 - Let's Try Graphics
12
//This is where we will create and process user data
23
import java.io.File;
3-
import java.io.FileInputStream;
4-
import java.io.FileOutputStream;
4+
import java.io.FileNotFoundException;
5+
import java.io.FileWriter;
56
import java.io.IOException;
6-
import java.io.InputStream;
7-
import java.io.OutputStream;
87
import java.util.*;
9-
import java.util.prefs.*;
108

119
public class User {
1210
//Private fields
1311
private String name;
14-
private Properties userInfo = new Properties();
15-
private Properties aiInfo = new Properties();
16-
private Preferences prefs;
1712
private String gender;
18-
private int age;
13+
private String[] month = new String[] {"January","February","March","April","May","June","July","August","September","October","November","December"};
14+
15+
16+
private String birthMonth;
17+
private String birthday;
1918

2019
//default constructor
2120
public User() {
2221
name = "Unknown";;
2322
gender = "Unknown";
24-
age = 0;
23+
birthMonth = "";
2524
}
2625

2726
//Post-Condition: calls setUser() to assign values to object
28-
public User(String n,String g, int a) {
29-
setUser(n,g,a);
30-
setData(n,g,a);
27+
public User(String n,String g, int bd, String bm, int by) {
28+
setUser(n,g,bd,bm,by);
29+
setData(n,g,birthday);
3130
}
3231

3332
//Post-Condition: Overloaded constructor to store user information and instantiate user object
34-
public void setUser(String n,String g, int a) {
33+
public void setUser(String n,String g, int bd, String bm, int by) {
3534
//assigns data to current object's variables
36-
this.name = n;
37-
this.gender = g;
38-
this.age = a;
35+
name = n;
36+
gender = g;
37+
birthMonth = bm;
3938
}
4039

4140
//Post-Condition: Stores data fields into file for later use
42-
public void setData(String name,String gender, int age) {
43-
//set output location
44-
45-
//create string variable using age values to save data
46-
String strAge = "" + age;
47-
48-
//tries to save data / throws exception if failed
49-
try (OutputStream output = new FileOutputStream("user.properties")){
50-
//sets property values
51-
userInfo.setProperty("name", name);
52-
userInfo.setProperty("gender", gender);
53-
userInfo.setProperty("age", strAge);
54-
55-
//saves properties to root folder
56-
userInfo.store(output,null);
57-
58-
/*prints properties
59-
System.out.println(userInfo);*/
60-
61-
} catch (IOException io) {
62-
io.printStackTrace();
63-
}
64-
65-
//stores ai data
66-
try (OutputStream aiOutput = new FileOutputStream("ai.properties")){
67-
//set property values
68-
aiInfo.setProperty("aiName", Central.getAiName());
69-
70-
//save properties file to root folder
71-
aiInfo.store(aiOutput, null);
41+
public void setData(String name, String gender, String birthday) {
42+
//Write data to file
43+
try {
44+
45+
//Writing to User File
46+
FileWriter myWriter = new FileWriter("user/user_name.txt");
47+
myWriter.write(getName());
48+
myWriter.close();
49+
myWriter = new FileWriter("user/user_gender.txt");
50+
myWriter.write(getGender());
51+
myWriter.close();
52+
myWriter = new FileWriter("user/user_birthday.txt");
53+
myWriter.write(getBirthday());
54+
myWriter.close();
55+
myWriter = new FileWriter("user/user_exists.txt");
56+
myWriter.write("User Exists");
57+
myWriter.close();
7258

73-
} catch (IOException io) {
74-
io.printStackTrace();
75-
}
59+
//Writing to AI File
60+
myWriter = new FileWriter("ai/ai_name.txt");
61+
myWriter.write(Central.getAiName());
62+
myWriter.close();
7663

64+
//Success Statement
65+
Iris.outputText.setText("Data saved.");
66+
} catch (IOException e) {
67+
Iris.outputText.setText("An error occured while saving your data.");
68+
e.printStackTrace();
69+
}
7770
}
7871

7972
//Post-Condition: reads data files and builds object using their values
8073
public void getData() {
81-
try (InputStream input = new FileInputStream("user.properties")) {
82-
//load user properties file
83-
userInfo.load(input);
74+
try {
75+
//Gets user info from file(s)
76+
//Name
77+
File userName = new File("user/user_name.txt");
78+
Scanner myReader = new Scanner(userName);
79+
while(myReader.hasNextLine()) {
80+
name = myReader.nextLine();
81+
}
82+
myReader.close();
83+
//Gender
84+
File userGender = new File("user/user_gender.txt");
85+
myReader = new Scanner(userGender);
86+
while(myReader.hasNextLine()) {
87+
gender = myReader.nextLine();
88+
}
89+
myReader.close();
8490

85-
//assigns values from properties file to current object
86-
this.name = userInfo.getProperty("name");
87-
this.gender = userInfo.getProperty("gender");
88-
String strAge = userInfo.getProperty("age");
89-
this.age = Integer.parseInt(strAge);
90-
Central.getAiName();
91+
//Age
92+
File userAge = new File("user/user_birthday.txt");
93+
myReader = new Scanner(userAge);
94+
while(myReader.hasNextLine()) {
95+
birthday = myReader.nextLine();
96+
}
97+
myReader.close();
9198

92-
} catch (IOException ex) {
93-
ex.printStackTrace();
99+
//Gets ai info from files
100+
File aiName = new File("ai/ai_name.txt");
101+
myReader = new Scanner(aiName);
102+
while(myReader.hasNextLine()) {
103+
Central.setAiName(myReader.nextLine());
104+
}
105+
myReader.close();
106+
} catch(FileNotFoundException e) {
107+
e.printStackTrace();
94108
}
95-
//load ai properties file
96-
try (InputStream input = new FileInputStream("ai.properties")) {
97-
//load properties file
98-
aiInfo.load(input);
109+
}
110+
111+
//Post-Condition: Creates folders and files for user and ai
112+
public void createFiles() {
113+
//Create the file or utilize what exists
114+
try {
115+
//Create User Folder
116+
File userDir = new File("user");
117+
if(!userDir.exists())
118+
userDir.mkdir();
99119

100-
//assigns values from properties file to current object
101-
this.name = userInfo.getProperty("name");
102-
Central.setAiName(aiInfo.getProperty("aiName"));
120+
//Create AI Folder
121+
File aiDir = new File("ai");
122+
if(!aiDir.exists())
123+
aiDir.mkdir();
103124

104-
} catch (IOException ex) {
105-
ex.printStackTrace();
106-
}
107-
108-
}
125+
//Creates Files for user and ai
126+
File userName = new File("user/user_name.txt");
127+
File userGender = new File("user/user_gender.txt");
128+
File userBirthday = new File("user/user_birthday.txt");
129+
File aiName = new File("ai/ai_name.txt");
130+
File userExists = new File("user/user_exists.txt");
109131

110-
/* //Post-Conditions: assigns default values of settings
111-
public void setPreferences() {
112-
prefs = Preferences.userRoot();
113-
//sets the value
114-
prefs.put();
115-
prefs.put("Gender",gender);
116-
prefs.putInt("Age", age);
132+
if(userName.createNewFile() && userGender.createNewFile() && userBirthday.createNewFile() && aiName.createNewFile() && userExists.createNewFile()) {
133+
System.out.println("File(s) created."); //Use for debugging
134+
} else
135+
System.out.print("");
136+
//System.out.println("File(s) already exists. Overwriting prior file(s)."); //Use for debugging
137+
} catch (IOException e) {
138+
Iris.outputText.setText(("An error occured while saving your data."));
139+
e.printStackTrace();
140+
}
117141
}
118-
*/
142+
119143
//Post-Condition: Return boolean value true or false if file exists
120-
public static boolean fileExists() {
121-
File temp = new File("user.properties");
122-
File temp2 = new File("ai.propeties");
123-
boolean exists = (temp.exists() || temp2.exists());
144+
public boolean userExists() {
145+
boolean exists = false;
146+
File userExists = new File("user/user_exists.txt");
147+
if(userExists.exists()) {
148+
try {
149+
Scanner myReader = new Scanner(userExists);
150+
String temp = myReader.nextLine();
151+
if(userExists.exists() && temp.isBlank() == false)
152+
exists = true;
153+
else {
154+
createFiles();
155+
exists = true;
156+
}
157+
158+
} catch(FileNotFoundException e) {
159+
e.printStackTrace();
160+
}
161+
162+
}
163+
else
164+
createFiles();
124165

125166
return exists;
126167
}
168+
127169
//Post-Condition: returns the name of the user
128170
public String getName() {
129-
if(fileExists())
171+
if(userExists())
130172
getData();
131-
132173
return name;
133174
}
134175

176+
//Post-Condition: sets the name of the user
177+
public void setName(String n) {
178+
name = n;
179+
try {
180+
FileWriter myWriter = new FileWriter("user/user_name.txt");
181+
myWriter.write(getName());
182+
myWriter.close();
183+
} catch (IOException e) {
184+
e.printStackTrace();
185+
}
186+
}
187+
135188
//Post-Condition: returns the gender of the user
136189
public String getGender() {
137-
if(fileExists())
190+
if(userExists())
138191
getData();
139192
return gender;
140193
}
141194

142-
//Post-Condition: returns the age of the user
195+
//Post-Condition: sets the gender of the user
196+
public void setGender(String g) {
197+
gender = g;
198+
try {
199+
FileWriter myWriter = new FileWriter("user/user_gender.txt");
200+
myWriter.write(getGender());
201+
myWriter.close();
202+
} catch (IOException e) {
203+
e.printStackTrace();
204+
}
205+
}
206+
207+
/*//Post-Condition: returns the age of the user
143208
public int getAge() {
144-
if(fileExists())
209+
int age = 0;
210+
if(userExists())
145211
getData();
146212
return age;
147213
}
148214
149-
//Post-Condition: toString method
150-
public String toString() {
151-
String str = "";
215+
//Post-Condition: sets the age of the user
216+
public void setAge(String age) {
217+
age = Integer.parseInt(age);
218+
219+
}*/
220+
221+
//Post-Condition: returns string value of the user's birthday
222+
public String getBirthday() {
223+
if(userExists())
224+
getData();
225+
return birthday;
152226

153-
return str;
227+
}
228+
//Post-Condition: returns value of birthdayMonth as String type format for writing to file exclusively
229+
public String getBirthMonth(int monthCount) {
230+
if(userExists())
231+
getData();
232+
birthMonth = month[monthCount];
233+
return birthMonth;
234+
}
235+
236+
//Post-Condition: assigns value of birthday created by birthDay, birthMonth, and birthYear
237+
public void setBirthday(int day,int monthCount, int year) {
238+
//System.out.println(String.format("%s/%d/%d", month,day,year)); Use for debug
239+
birthday = String.format("%s %d,%d", getBirthMonth(monthCount),day,year);
240+
try {
241+
FileWriter myWriter = new FileWriter("user/user_birthday.txt");
242+
myWriter.write(birthday);
243+
myWriter.close();
244+
} catch (IOException e) {
245+
e.printStackTrace();
246+
}
154247
}
155248
}

0 commit comments

Comments
 (0)