-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentDatabase.java
More file actions
492 lines (408 loc) · 9.93 KB
/
StudentDatabase.java
File metadata and controls
492 lines (408 loc) · 9.93 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
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
import java.util.HashMap;
import java.util.Scanner;
import java.util.Collections;
import java.util.Comparator;
import java.util.ArrayList;
import java.io.IOException;
import java.io.File;
import java.io.PrintWriter;
class Student implements Comparable<Student>
{
private String name;
private int grade;
Student(String name, int grade)
{
this.name = name;
if (grade > 100)
{
grade = 100;
}
else if (grade < 0)
{
grade = 0;
}
this.grade = grade;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setGrade(int grade)
{
if (grade > 100)
{
grade = 100;
}
else if (grade < 0)
{
grade = 0;
}
this.grade = grade;
}
public int getGrade()
{
return grade;
}
public int compareTo(Student other)
{
return other.grade - this.grade;
}
public void printInfo()
{
System.out.println(name + " : " + grade);
}
}
public class StudentDatabase
{
private static boolean modified = false;
public static Comparator<Student> nac = new Comparator<Student>()
{
public int compare(Student s1, Student s2)
{
return s1.getName().compareTo(s2.getName());
}
};
public static Comparator<Student> gac = new Comparator<Student>()
{
public int compare(Student s1, Student s2)
{
return s1.getGrade() - s2.getGrade();
}
};
public static Comparator<Student> gdc = new Comparator<Student>()
{
public int compare(Student s1, Student s2)
{
return s2.getGrade() - s1.getGrade();
}
};
public static void displayStudents(HashMap<String, Student> student)
{
if (student.size() == 0)
{
System.out.println("No items to print");
}
else
{
for (String s : student.keySet())
{
System.out.print("\t");
student.get(s).printInfo();
}
}
System.out.println();
}
public static void addStudent(HashMap<String, Student> student, String name, int grade)
{
Student s = new Student(name, grade);
student.put(name, s);
modified = true;
System.out.println("\tAdded:");
System.out.print("\t ");
student.get(name).printInfo();
}
public static void renameStudent(HashMap<String, Student> student, String name, String newName)
{
Student curr = student.remove(name);
if (curr != null)
{
curr.setName(newName);
student.put(newName, curr);
modified = true;
System.out.println("\tStudent Name Updated:");
System.out.print("\t ");
curr.printInfo();
}
else
{
System.out.println("\t" + name + " not in dataset");
}
}
public static void removeStudent(HashMap<String, Student> student, String removeName)
{
Student rm = student.remove(removeName);
if (rm != null)
{
modified = true;
System.out.println("\tRemoved: ");
System.out.print("\t ");
rm.printInfo();
System.out.println();
}
else
{
System.out.println("\t" + removeName + " not in dataset");
System.out.println();
}
}
public static void updateGrade(HashMap<String, Student> student, String name, int grade)
{
Student curr = student.get(name);
if (curr != null)
{
curr.setGrade(grade);
modified = true;
System.out.println("\tGrade Updated:");
System.out.print("\t ");
curr.printInfo();
}
else
{
System.out.println("\t" + name + " not in dataset");
System.out.println();
}
}
public static void updateFile(HashMap<String, Student> student, String str)
{
if (!str.endsWith(".txt"))
{
System.out.println("Incorrect file format");
return;
}
try
{
PrintWriter p = new PrintWriter(new File(str));
for (Student stu : student.values())
{
p.println(stu.getName() + "," + stu.getGrade());
}
modified = false;
p.close();
}
catch (IOException e)
{
System.out.println("Could not write to file " + str);
return;
}
}
public static HashMap<String, Student> readFromFile(String readFile)
{
HashMap<String, Student> student = new HashMap<>();
try
{
Scanner sc = new Scanner(new File(readFile));
while (sc.hasNextLine())
{
String str = sc.nextLine();
String[] fields = str.split(",");
String name = fields[0];
int grade = Integer.parseInt(fields[1]);
Student s = new Student(name, grade);
student.put(name, s);
}
sc.close();
}
catch (IOException e)
{
System.out.println("Could not read file");
}
return student;
}
public static void main(String[] args)
{
System.out.println();
System.out.println("=== Student Database ===");
System.out.println();
System.out.println("1.) Display Students");
System.out.println("2.) Add Student");
System.out.println("3.) Rename Student");
System.out.println("4.) Remove Student");
System.out.println("5.) Update Grade");
System.out.println("6.) Save Student Data");
System.out.println("7.) Sort Students");
System.out.println("0.) Exit");
System.out.println();
HashMap<String, Student> students = readFromFile("students.txt");
Scanner sc = new Scanner(System.in);
while (true)
{
try
{
System.out.println("Please select one of the numbers above then press enter:");
int x = Integer.parseInt(sc.nextLine());
System.out.println();
if (x == 1)
{
System.out.println("Showing Student Data:");
System.out.println();
//students = readFromFile("students.txt"); // Still experimenting here
if (students.size() != 0)
{
displayStudents(students);
}
else
{
System.out.println("No entries exist for this file");
}
}
else if (x == 2)
{
System.out.println("\tEnter student name:");
System.out.print("\t ");
String stuName = sc.nextLine();
Student curr = students.get(stuName);
if (curr == null)
{
System.out.println("\tEnter student grade:");
System.out.print("\t ");
int stuGrade = sc.nextInt();
sc.nextLine();
addStudent(students, stuName, stuGrade);
System.out.println();
}
else
{
System.out.println("\t" + stuName + " is already in the system");
System.out.println();
}
}
else if (x == 3)
{
System.out.println("\tEnter student name:");
System.out.print("\t ");
String stuName = sc.nextLine();
Student curr = students.get(stuName);
if (curr != null)
{
System.out.println("\tEnter new student name:");
System.out.print("\t ");
String newStuName = sc.nextLine();
Student curr2 = students.get(newStuName);
if (curr2 == null)
{
renameStudent(students, stuName, newStuName);
System.out.println();
}
else
{
System.out.println("\t" + newStuName + " is already in the system");
System.out.println();
}
}
else
{
System.out.println("\t" + stuName + " not in dataset");
System.out.println();
}
}
else if (x == 4)
{
System.out.println("\tEnter student name:");
System.out.print("\t ");
String stuName = sc.nextLine();
removeStudent(students, stuName);
}
else if (x == 5)
{
System.out.println("\tEnter student name:");
System.out.print("\t ");
String stuName = sc.nextLine();
Student curr = students.get(stuName);
if (curr != null)
{
System.out.println("\tEnter student grade:");
System.out.print("\t ");
int stuGrade = sc.nextInt();
sc.nextLine();
updateGrade(students, stuName, stuGrade);
System.out.println();
}
else
{
System.out.println("\t" + stuName + " not in dataset");
System.out.println();
}
}
else if (x == 6)
{
System.out.println("\tStudent Data Updated");
updateFile(students, "students.txt");
System.out.println();
}
else if (x == 7)
{
System.out.println("\tSort by grades ('asc' or 'desc') or by name ('name')?");
System.out.print("\t ");
String sorting = sc.nextLine().toLowerCase();
System.out.println();
ArrayList<Student> studentsCopy = new ArrayList<Student>(students.values());
if (sorting.equals("asc"))
{
Collections.sort(studentsCopy, gac);
for (Student s : studentsCopy)
{
System.out.print("\t");
s.printInfo();
}
System.out.println();
}
else if (sorting.equals("desc"))
{
Collections.sort(studentsCopy, gdc);
for (Student s : studentsCopy)
{
System.out.print("\t");
s.printInfo();
}
System.out.println();
}
else if (sorting.equals("name"))
{
Collections.sort(studentsCopy, nac);
for (Student s : studentsCopy)
{
System.out.print("\t");
s.printInfo();
}
System.out.println();
}
else
{
System.out.println("\tInvalid sorting order, please choose 'asc', 'desc', or 'name' then press enter");
System.out.println();
}
}
else if (x == 0)
{
if (modified == true)
{
System.out.println("\tDatabase changes have not been saved");
System.out.println("\tAre you sure you want to exit?");
System.out.print("\t ");
String quit = sc.nextLine().toLowerCase();
System.out.println();
if (quit.equals("yes"))
{
System.out.println("\tThank you, good bye");
break;
}
else
{
System.out.println("\tBack to Main Menu");
System.out.println();
}
}
else
{
System.out.println("\tThank you, good bye");
break;
}
}
else
{
System.out.println("\tInvalid command, please select one of the numbers above");
}
}
catch (NumberFormatException e)
{
System.out.println();
System.out.println("\tCannot convert that input to a numeric value");
System.out.println();
}
}
}
}