-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataHandler.java
439 lines (436 loc) · 13.4 KB
/
DataHandler.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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
import java.io.File;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.util.HashMap;
/**
* Diese Klasse übernimmt alle Ein- und Ausgabeoperationen. Alle Methoden und Attribute sind static und über die Klasse aufrufbar.
* @author Thomas Davies
* @version 1.0
*/
public class DataHandler{
private static String path = System.getProperty("user.dir")
+ File.separator;
private static String folderPath = path + "data" + File.separator;
private static String syllabusPath = folderPath + "syllabus.dat";
private static String userPath = folderPath + "user.dat";
private static String testPath = folderPath + "testfile.dat";
/**
* Diese Methode erstellt einen "Data" Ordner, sofern noch keiner vorhanden ist.
*/
private static void setupDirectory(){
File folder = null;
folder = new File(folderPath);
if(!folder.exists()){
folder.mkdirs();
}
}
/**
* Diese Methode ruft die setupDirectory Methode auf.
*/
public static void run(){
setupDirectory();
}
/**
* Diese Methode schreibt die Map mit allen Modulen(Scores), als ein Objekt in eine Datei in den Data Ordner.
* @param syllabusMap Ist die Map, die es in eine Datei zu schreiben gilt.
*/
public static void writeSyllabus(HashMap<String, Score> syllabusMap){
File syllabusFile = null;
ObjectOutputStream oos = null;
try{
syllabusFile = new File(syllabusPath);
oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(syllabusFile)));
oos.writeObject(syllabusMap);
}catch(IOException e){
PrintWriter writer = null;
try{
writer = new PrintWriter("error.txt");
writer.write(e.toString());
e.printStackTrace(writer);
writer.close();
}catch(IOException ex){}
}finally{
if(oos != null){
try{
oos.close();
}catch(IOException e){
PrintWriter writer = null;
try{
writer = new PrintWriter("error.txt");
writer.write(e.toString());
e.printStackTrace(writer);
writer.close();
}catch(IOException ex){}
}
}
}
}
/**
* Diese Methode schreibt das User Objekt in eine Datei in den Data Ordner.
* @param user Enthält das User Objekt mit all seinen User-Daten.
*/
public static void writeUser(User user){
File userFile = null;
ObjectOutputStream oos = null;
try{
userFile = new File(userPath);
oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(userFile)));
oos.writeObject(user);
}catch(IOException e){
PrintWriter writer = null;
try{
writer = new PrintWriter("error.txt");
writer.write(e.toString());
e.printStackTrace(writer);
writer.close();
}catch(IOException ex){}
}finally{
if(oos != null){
try{
oos.close();
}catch(IOException e){
PrintWriter writer = null;
try{
writer = new PrintWriter("error.txt");
writer.write(e.toString());
e.printStackTrace(writer);
writer.close();
}catch(IOException ex){}
}
}
}
}
/**
* Diese Methode holt den Vornamen aus dem User Objekt aus "user.dat".
* @return Gibt den Vornamen als String zurück.
*/
public static String getSurname(){
File file = new File(userPath);
String surname = "";
if(file.exists()){
ObjectInputStream ois = null;
try{
ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(userPath)));
User user = (User) ois.readObject();
String name = user.getName();
surname = name.substring(name.indexOf(',')+2);
}catch(IOException e){
PrintWriter writer = null;
try{
writer = new PrintWriter("error.txt");
writer.write(e.toString());
e.printStackTrace(writer);
writer.close();
}catch(IOException ex){}
}catch(ClassNotFoundException e){
PrintWriter writer = null;
try{
writer = new PrintWriter("error.txt");
writer.write(e.toString());
e.printStackTrace(writer);
writer.close();
}catch(IOException ex){}
}finally{
if(ois != null){
try{
ois.close();
}catch(IOException e){
PrintWriter writer = null;
try{
writer = new PrintWriter("error.txt");
writer.write(e.toString());
e.printStackTrace(writer);
writer.close();
}catch(IOException ex){}
}
}
}
}
return surname;
}
/**
* Diese Methode holt die Map mit all den Modulen(Scores) aus der Datei "syllabus.dat".
* @return Gibt die Map als HashMap zurück, wobei als Key das Studienelement in Form eines Strings ist, und als Value das Modul(Score).
*/
public static HashMap<String, Score> getSyllabus(){
HashMap<String, Score> syllabusMap = null;
ObjectInputStream ois = null;
try{
ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(new File(syllabusPath))));
@SuppressWarnings("unchecked")
HashMap<String, Score> read = (HashMap<String, Score>) ois.readObject();
syllabusMap = read;
}catch(IOException e){
PrintWriter writer = null;
try{
writer = new PrintWriter("error.txt");
writer.write(e.toString());
e.printStackTrace(writer);
writer.close();
}catch(IOException ex){}
}catch(ClassNotFoundException e){
PrintWriter writer = null;
try{
writer = new PrintWriter("error.txt");
writer.write(e.toString());
e.printStackTrace(writer);
writer.close();
}catch(IOException ex){}
}finally{
if(ois != null){
try{
ois.close();
}catch(IOException e){
PrintWriter writer = null;
try{
writer = new PrintWriter("error.txt");
writer.write(e.toString());
e.printStackTrace(writer);
writer.close();
}catch(IOException ex){}
}
}
}
return syllabusMap;
}
/**
* Diese Methode holt den User als User Objekt aus der Datei "syllabus.dat".
* @return Gibt den User aus der User Datei zurück.
*/
public static User getUser(){
File file = new File(userPath);
User user = null;
if(file.exists()){
ObjectInputStream ois = null;
try{
ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(file)));
user = (User) ois.readObject();
}catch(IOException e){
PrintWriter writer = null;
try{
writer = new PrintWriter("error.txt");
writer.write(e.toString());
e.printStackTrace(writer);
writer.close();
}catch(IOException ex){}
}catch(ClassNotFoundException e){
PrintWriter writer = null;
try{
writer = new PrintWriter("error.txt");
writer.write(e.toString());
e.printStackTrace(writer);
writer.close();
}catch(IOException ex){}
}finally{
if(ois != null){
try{
ois.close();
}catch(IOException e){
PrintWriter writer = null;
try{
writer = new PrintWriter("error.txt");
writer.write(e.toString());
e.printStackTrace(writer);
writer.close();
}catch(IOException ex){}
}
}
}
}
return user;
}
/**
* Diese Methode überprüft ob schon eine Datei existiert, in der die Testdateien des Notentesters existieren.
* @return Gibt wahr zurück, wenn die Testdatei existiert.
*/
public static boolean testfileExists(){
File file = null;
boolean exists = false;
file = new File(testPath);
if(file.exists()){
exists = true;
}
return exists;
}
/**
* Diese Methode überprüft ob schon eine User Datei existiert.
* @return Gibt wahr zurück, wenn die Userdatei existiert.
*/
public static boolean userfileExists(){
File file = null;
boolean exists = false;
file = new File(userPath);
if(file.exists()){
exists = true;
}
return exists;
}
/**
* Diese Methode ob die Modulplan Datei existiert.
* @return Gibt wahr zurück, wenn die Moduplandatei existiert.
*/
public static boolean mainfileExists(){
File file = null;
boolean exists = false;
file = new File(syllabusPath);
if(file.exists()){
exists = true;
}
return exists;
}
/**
* Diese Methode erzeugt eine Testdatei, in der die Noten des Notentesters gespeichert werden.
*/
public static void createTestfile(){
File testFile = null;
HashMap<String, Score> syllabusMap = null;
ObjectOutputStream oos = null;
try{
testFile = new File(testPath);
syllabusMap = getSyllabus();
oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(testFile)));
oos.writeObject(syllabusMap);
User user = getUser();
user.setTestAverage(user.getAverage());
writeUser(user);
}catch(IOException e){
PrintWriter writer = null;
try{
writer = new PrintWriter("error.txt");
writer.write(e.toString());
e.printStackTrace(writer);
writer.close();
}catch(IOException ex){}
}finally{
try{
if(oos != null){
oos.close();
}
}catch(IOException e){
PrintWriter writer = null;
try{
writer = new PrintWriter("error.txt");
writer.write(e.toString());
e.printStackTrace(writer);
writer.close();
}catch(IOException ex){}
}
User user = getUser();
user.setTestAverage(user.getAverage());
}
}
/**
* Diese Methode liest die Map mit den Noten aus dem Notentester aus.
* @return Gibt die Map des Notentesters als HashMap zurück, wobei als Key das Studienelement in Form eines Strings ist, und als Value das Modul(Score).
*/
public static HashMap<String,Score> getTestMap(){
HashMap<String, Score> testMap = null;
ObjectInputStream ois = null;
try{
ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(new File(testPath))));
@SuppressWarnings("unchecked")
HashMap<String, Score> read =(HashMap<String, Score>) ois.readObject();
testMap = read;
}catch(IOException e){
PrintWriter writer = null;
try{
writer = new PrintWriter("error.txt");
writer.write(e.toString());
e.printStackTrace(writer);
writer.close();
}catch(IOException ex){}
}catch(ClassNotFoundException e){
PrintWriter writer = null;
try{
writer = new PrintWriter("error.txt");
writer.write(e.toString());
e.printStackTrace(writer);
writer.close();
}catch(IOException ex){}
}finally{
if(ois != null){
try{
ois.close();
}catch(IOException e){
PrintWriter writer = null;
try{
writer = new PrintWriter("error.txt");
writer.write(e.toString());
e.printStackTrace(writer);
writer.close();
}catch(IOException ex){}
}
}
}
return testMap;
}
/**
* Diese Methode updated die Testmap mit den aktuellen Veränderungen aus dem Notentester.
* @param testMap Enthält alle Noten des Notentesters, wobei als Key das Studienelement in Form eines Strings ist, und als Value das Modul(Score).
*/
public static void updateTestMap(HashMap<String,Score> testMap){
File testFile = null;
ObjectOutputStream oos = null;
try{
testFile = new File(testPath);
oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(testFile)));
oos.writeObject(testMap);
}catch(IOException e){
PrintWriter writer = null;
try{
writer = new PrintWriter("error.txt");
writer.write(e.toString());
e.printStackTrace(writer);
writer.close();
}catch(IOException ex){}
}finally{
try{
if(oos != null){
oos.close();
}
}catch(IOException e){
PrintWriter writer = null;
try{
writer = new PrintWriter("error.txt");
writer.write(e.toString());
e.printStackTrace(writer);
writer.close();
}catch(IOException ex){}
}
}
}
/**
* Updated den Durchschnitt des Users
* @param score Ist der Durchschnitt, der in das Userobjekt geschrieben wird.
*/
public static void updateTestAverage(float score){
User user = getUser();
user.setTestAverage(score);
writeUser(user);
}
/**
* Erzeugt einen Wahlpflicht Zähler, der Überblick über die maximale Anzahl der Wahlpflichtfächer gibt.
*/
public static void createTestWpfCounter(){
File file = new File(userPath);
User user = null;
if(!file.exists()){
user = getUser();
user.setTestWpfCounter(user.getWpfCounter());
writeUser(user);
}
}
/**
* Löscht die Testdatei des Notentesters. Diese Funktion dient dazu um den Notentester zurückzusetzen.
*/
public static void removeTestFile(){
File file = new File(testPath);
file.delete();
}
}