-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabase.java
More file actions
402 lines (354 loc) · 12.9 KB
/
Copy pathDatabase.java
File metadata and controls
402 lines (354 loc) · 12.9 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
import java.io.*;
import java.util.ArrayList;
public class Database
{
private ArrayList<TeamObject> teams = new ArrayList<TeamObject>();
private ArrayList<Account> accounts = new ArrayList<Account>();
private ScheduleObject schedule = new ScheduleObject();
//_______________________________________________________________________________________Constructor
public Database() throws IOException
{
try(FileReader fr = new FileReader("teams.csv");
BufferedReader br = new BufferedReader(fr))
{
String line = br.readLine();
while(line != null)
{
int commaIndex = line.indexOf(",");
String currentName = line.substring(0, commaIndex);
int currentPoints = Integer.parseInt(line.substring(commaIndex + 1));
TeamObject newTeam = new TeamObject(currentName, currentPoints);
teams.add(newTeam);
line = br.readLine();
}
br.close();
fr.close();
}
catch(FileNotFoundException e)
{
System.out.println("Dang");
try(FileWriter fw = new FileWriter("teams.csv");
BufferedWriter out = new BufferedWriter(fw))
{
//empty csv file to edit with one admin account with account and teams objects
/*out.write("This is line of text.\n");
out.write("This is another line of text.\n");
out.append("This is yet another line of text.\n");*/
out.close();
fw.close();
}
catch(IOException ex)
{
System.out.printf("IO Exception: %s", ex.getMessage());
}
}
catch(IOException e)
{
e.printStackTrace();
}
//____________________________________________________________________________________________seperation of team and account file readers/writers
try(FileReader fr = new FileReader("accounts.csv");
BufferedReader br = new BufferedReader(fr))
{
String line = br.readLine();
while(line != null)
{
int commaIndex = line.indexOf(",");
int adminIndex = line.indexOf("$");
String newUser = line.substring(0, commaIndex);
String newPassword = line.substring(commaIndex+1, adminIndex-1);
String admin = line.substring(adminIndex + 1);
Account newAccount = new Account(newUser, newPassword);
if(admin.equals("true"))
{
newAccount.admin();
}
this.accounts.add(newAccount);
line = br.readLine();
}
br.close();
fr.close();
}
catch(FileNotFoundException e)
{
try(FileWriter fw = new FileWriter("accounts.csv");
BufferedWriter out = new BufferedWriter(fw))
{
Account newAccount = new Account("ADMIN", "PASSWORD");
newAccount.admin();
out.write(newAccount.getUsername() + "," + newAccount.getPassword()+ ",$true");
out.close();
}
catch(IOException ex)
{
System.out.printf("IO Exception: %s", ex.getMessage());
}
}
catch(IOException e)
{
e.printStackTrace();
}
//___________________________________________________________________________________ schedule file reader/writer start here
try(FileReader fr = new FileReader("schedule.csv");
BufferedReader br = new BufferedReader(fr))
{
int currentWeek = 0;
String line = br.readLine();
while(line != null)
{
boolean newWeek = false;
for(int i = 1; i <= 10; i++)
{
if(line.equals("Week" + i))
{
newWeek = true;
currentWeek = i;
schedule.addWeek(i);
break;
}
}
if(newWeek)
{
line = br.readLine();
newWeek = false;
continue;
}
int commaIndex = line.indexOf(",");
TeamObject firstTeam = new TeamObject(line.substring(0, commaIndex));
TeamObject opposingTeam = new TeamObject(line.substring(commaIndex + 1));
schedule.getWeek(currentWeek).newMatch(firstTeam, opposingTeam);
line = br.readLine();
}
br.close();
fr.close();
}
catch(FileNotFoundException e)
{
try(FileWriter fw = new FileWriter("schedule.csv");
BufferedWriter out = new BufferedWriter(fw))
{
out.close();
}
catch(IOException ex)
{
System.out.printf("IO Exception: %s", ex.getMessage());
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
//___________________________________________________________________________________Update CSV File (will replace old file with info in arraylists)
public void updateCSV()
{
try(FileWriter fw = new FileWriter("teams.csv");
BufferedWriter out = new BufferedWriter(fw))
{
for(int i = 0; i < teams.size(); i++)
{
out.write(String.format("%s,%d%n", teams.get(i).getName(), teams.get(i).getPoints()));
}
}
catch(IOException ex)
{
System.out.printf("IO Exception: %s", ex.getMessage());
}
try(FileWriter fw = new FileWriter("accounts.csv");
BufferedWriter out = new BufferedWriter(fw))
{
for(int i = 0; i < accounts.size(); i++)
{
out.write(String.format("%s,%s,$%s%n",accounts.get(i).getUsername(), accounts.get(i).getPassword(), accounts.get(i).checkAdmin()));
}
}
catch(IOException ex)
{
System.out.printf("IO Exception: %s", ex.getMessage());
}
try(FileWriter fw = new FileWriter("schedule.csv");
BufferedWriter out = new BufferedWriter(fw))
{
ArrayList<TeamObject> allMatches = new ArrayList<>();
out.write("Week1\n");
for(int i = 1; i <= schedule.getTotalWeeks(); i++)
{
allMatches = schedule.getWeek(i).getAllMatches();
for(int j = 0; j < allMatches.size() - 1; j++)
{
out.write(String.format("%s,%s%n", allMatches.get(j).getName(), allMatches.get(j+1).getName()));
j++;
}
if(i < schedule.getTotalWeeks())
{
out.write(String.format("Week%d%n", i+1));
}
}
}
catch(IOException ex)
{
System.out.printf("IO Exception: %s", ex.getMessage());
}
}
//________________________________________________________________________________Database editing methods
public ArrayList<TeamObject> getTeamsList()
{
return teams; //WILL RETURN REFERENCE TYPES. NOT NAMES.
}
public ArrayList<Account> getAccountsList()
{
return accounts; //WILL RETURN REFERENCE TYPES. NOT NAMES.
}
public int getTeamListSize()
{
return teams.size();
}
public int getAccountListSize()
{
return accounts.size();
}
public void addTeam(String teamName, int points)
{
TeamObject newTeam = new TeamObject(teamName, points);
teams.add(newTeam);
}
public void addAccount(String username, String password, boolean admin)
{
Account newAccount = new Account(username, password);
if(admin == true)
{
newAccount.admin();
}
this.accounts.add(newAccount);
}
public TeamObject getTeam(String teamName)
{
TeamObject returnTeam = new TeamObject(teamName);
for (int i = 0; i < teams.size(); i++)
{
if(teams.get(i).getName().equals(teamName))
{
return teams.get(i);
}
else
{
returnTeam.setName("NO TEAM");
returnTeam.setPoints(0);
}
}
return returnTeam;
}
public Account getAccount(String accountName)
{
Account returnAccount = new Account("", "");
for (int i = 0; i < accounts.size(); i++)
{
if(accounts.get(i).getUsername().equals(accountName))
{
return accounts.get(i);
}
else
{
returnAccount.changeUsername("NO ACCOUNT");
returnAccount.changePassword("NO PASSWORD");
returnAccount.removeAdmin();
}
}
return returnAccount;
}
public void replaceTeam(String oldTeam, TeamObject team)
{
for(int i = 0; i < teams.size(); i++)
{
if(teams.get(i).getName().equals(oldTeam))
{
teams.set(i, team);
}
}
}
public void replaceAccount(String oldAccount, Account account)
{
for(int i = 0; i < accounts.size(); i++)
{
if(accounts.get(i).getUsername().equals(oldAccount))
{
accounts.set(i, account);
}
}
}
public void removeTeam(String team)
{
for(int i = 0; i < teams.size(); i++)
{
if(teams.get(i).getName().equals(team))
{
teams.remove(i);
}
}
}
public void removeAccount(String account)
{
for(int i = 0; i < accounts.size(); i++)
{
if(accounts.get(i).getUsername().equals(account))
{
accounts.remove(i);
}
}
}
public void addPoints(String additionTeam, int pointsGained)
{
TeamObject team = getTeam(additionTeam);
team.addPoints(pointsGained);
replaceTeam(additionTeam ,team);
}
public ScheduleObject newSchedule()
{
ScheduleObject newSchedule = new ScheduleObject();
return newSchedule;
}
public void setSchedule(ScheduleObject newSchedule)
{
this.schedule = newSchedule;
}
public ScheduleObject getSchedule()
{
return schedule;
}
}
/*
Database()
- Searches for exisiting csv file and reads the data, then places it into a list for you to access
- If the file does not exist, it will create a new file in it's place. The account.csv will have an admin account automatically.
- I gave mine the name data. So i use data.(FUNCTION NAME) for the following functions
getTeamsList() | getAccountsList()
- RETURNS REFERENCE OBJECTS. USE getName() (teamObject) OR getUsername() (account) TO GET THE NAME
- returns the list of teams or accounts.
- Will return the reference objects. Not names, points or passwords.
- Must use .getName(), .getPassword() if you want them. Can only do one at a time.
addTeam("Team Name", int points) | addAccount("Username", "Password", "Admin Boolean")
- Creates a new team or accounts object.
- Use it's versatility when creating new accounts. Can set it to be a variable in your function or give it default values
getTeam("Team Name") | getAccount("Account username")
- Returns a team or account OBJECT. Use .getName(), .getPassword() if you want specific information.
- We can discuss later what should happen if the account is not found.
replaceTeam("Team Name", new teamObject) | replaceAccount("Account Username", Account Obejct)
- Search for the team or account and replace it with a new object
- Must have a team or account object made beforehand.
- We can discuss later what should happen if the account is not found.
removeTeam("Team Name") | removeAccount("Account Username")
- Search for the team or account and remove it from the list
- We can discuss later what should happen if the account is not found.
addPoints("Team Name", int [POINTS TO BE ADDED])
- Search for a team and add points to it's current total
- We can discuss later what should happen if the account is not found.
newSchedule()
- Create a new shedule object and returns it
setSchedule()
- replace the schedule object in the database with a new one
getSchedule()
- retrieve the schedule object. Use this to edit the schedule
updateCSV()
- Takes current team and account lists and overwrites the CSV file.
- Un edited data will remain the same since you did not edit it when it was in the arrayList
*/