-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClassDriver.java
More file actions
300 lines (237 loc) · 9.67 KB
/
Copy pathClassDriver.java
File metadata and controls
300 lines (237 loc) · 9.67 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
// --== CS400 File Header Information ==--
// Name: Michael Kornely
// Email: mkornely@wisc.edu
// Team: BA
// Role: Front End
// TA: Brianna Cochran
// Lecturer: Florian
// Notes to Grader: Decided to remove the rename student function
import java.util.Scanner;
import java.util.NoSuchElementException;
import java.io.*;
import java.nio.file.*;
import java.util.InputMismatchException;
public class ClassDriver {
// runs the driver
public static void main(String[] args) {
run();
}
/**
* Runs the driver
*
*
*/
public static void run() {
Scanner scan = new Scanner(System.in);
RedBlackTree<Student> rbtree = new RedBlackTree<Student>();
RosterEditor roster = new RosterEditor(rbtree);
System.out.println("Welcome to the Student Class Roster ");
boolean exit = false;
//keeps going till exit signal
while (!exit) {
//inputs
System.out.println(" Enter {A} to add a student to the roster");
System.out.println(" Enter {S} to search for a student in the roster");
// System.out.println(" Enter {R} to rename a student in the roster");
System.out.println(" Enter {G} to change the grade of a student in the roster");
System.out.println(" Enter {P} to print the roster");
System.out.println(" Enter {O} to output the roster to a text file");
System.out.println(" Enter {C} to clear the roster");
System.out.println(" Enter {I} to import the roster from a text file");
System.out.println(
"\n Don't know what to do???? Enter {H} to get examples on using the application");
System.out.println(" Enter {Q} to quit application");
String stringchoice = null;
char choice = 'H';
boolean selectionstring = false;
//makes sure char is not a empty string
while (!selectionstring) {
System.out.print("\n Your Selection: ");
stringchoice = scan.nextLine();
if (stringchoice.length() == 0) {
System.out.println("\n Please enter a valid character!");
} else {
choice = stringchoice.toLowerCase().charAt(0);
selectionstring = true;
}
}
System.out.println("__________________________________________________________ \n");
//picks a choice
switch (choice) {
case 'a':
String name = null;;
boolean repeatname = false;
System.out.println("Adding a new student...");
//makes sure a name isnt repeated
while (!repeatname)
try {
System.out.println("Student name: ");
name = scan.nextLine().trim().toLowerCase();
roster.search(name);
System.out.println("This name is already in the roster. Please try again!");
} catch (NoSuchElementException e) {
repeatname = true;
}
int id = 0;
boolean correctidInput = false;
//makes sure it is a valid id
while (!correctidInput) {
try {
System.out.println("Student ID: ");
id = scan.nextInt();
correctidInput = true;
} catch (InputMismatchException e) {
System.out.println("Invalid input. Enter a valid integer for the ID");
}
}
double grade = 0.0;
boolean correctgradeInput = false;
//make sure its a valid grade
while (!correctgradeInput) {
try {
System.out.print("Student grade: ");
grade = scan.nextDouble();
if (grade < 0.00 || grade > 100.00)
throw new InputMismatchException();
correctgradeInput = true;
} catch (InputMismatchException e) {
System.out.println(
"Invalid input. Enter a valid grade (can be any positive rational number between 0 and 100");
}
}
rbtree.insert(new Student(name, id, grade));
System.out.println("Student added.");
break;
case 's':
System.out.println("Enter the name of a student to find");
String studentname = scan.nextLine().toLowerCase();
try {
roster.search(studentname);
} catch (NoSuchElementException e) {
e.getMessage();
}
break;
/*
* case 'r':
* System.out.println("Enter the name of a current student to change their name: "); String
* oldname = scan.nextLine().trim();
*
* try { roster.search(oldname.toLowerCase()); } catch (NoSuchElementException e) {
* e.getMessage(); break; } System.out.println("Enter the new name for Student " + oldname);
* String newname = scan.nextLine().trim();
*
* try { roster.adjustName(oldname.toLowerCase(), newname.toLowerCase()); } catch
* (NoSuchElementException e) {
*
* System.out.println("Name not sucessfully changed"); break; }
* System.out.println("Name sucessfully changed! Student " + oldname + " is now " +
* newname);
*
* break;
*/
case 'g':
System.out.println("Enter the name of a current student to change their grade: ");
String gradename = scan.nextLine().trim();
double oldgrade = 0.00;
//finds the grade if the student exists
try {
oldgrade = roster.search(gradename.toLowerCase()).getGrade();
} catch (NoSuchElementException e) {
e.getMessage();
break;
}
boolean correctinput = false;
double newgrade = 0.00;
//makes sure grade is correct and in the scope
while (!correctinput) {
try {
System.out.println("Enter the new grade for Student " + gradename);
newgrade = scan.nextDouble();
if (newgrade > 100.00 || newgrade < 0.00)
throw new InputMismatchException();
correctinput = true;
} catch (InputMismatchException e) {
System.out.println(
"Please try again. You must enter a number that is between 0 and 100. For example: 78.5, 98, 56.97");
}
}
try {
roster.adjustGrade(gradename, newgrade);
} catch (NoSuchElementException e) {
System.out.println("Grade was not sucessfully changed");
break;
}
System.out.println("Grade was sucessfully changed! Student " + gradename
+ " old grade was " + oldgrade + " and is now " + newgrade);
break;
case 'p':
try {
System.out.println("The roster is as follows: " + rbtree.toString());
} catch (NullPointerException e) {
System.out.println("Roster is empty!");
}
break;
case 'o':
System.out.println("Enter the file name or the file path to be exported: ");
String outputfile = scan.nextLine();
if (!DataHandler.exportData(rbtree, outputfile)) {
System.out.println("Unable to output the tree to the file.");
}
case 'c':
roster.clear();
System.out.println("Roster is cleared.");
break;
case 'i':
System.out.println("Enter the file name or the file path: ");
String filename = scan.nextLine();
RedBlackTree<Student> importedtree = null;
try {
importedtree = DataHandler.importData(filename);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
break;
}
if (importedtree == null) {
System.out.println("Tree cannot be formed from data provided");
} else {
rbtree = importedtree;
roster = new RosterEditor(rbtree);
}
break;
case 'h':
System.out.println("Add: Provide a valid name, integer ID, and grade to "
+ "add a new student to the roster. These will be prompted to you one by one "
+ "\n Example: Adam Rodgers, 9087861, 87.90");
System.out.println("\nSearch: Provide the name of the student that you would like to "
+ "search for. If they are in the roster, their information will be printed.");
// System.out.println(
// "\nChange name: Provide the name of the student that you would like to rename; "
// + "you will be prompted for the desired new name.");
System.out.println(
"\nChange grade: Provide the name of the student whose grade you would like to change; "
+ "you will be prompted for the new grade.");
System.out
.println("\nPrint roster: Prints details of each student that is in the roster.");
System.out.println("\nClear roster: Removes every student from the roster.");
System.out.println("\nLoad file: Provide the name of a text file to load students into "
+ "the roster. The file must be in the same directory as this application."
+ "\n Example: roster.txt");
System.out.println(
"\nOutput file: Exports contents of the roster to a new text file; you will be prompted for the file name or path.");
break;
case 'q':
System.out.println("Closing the interface.");
exit = true;
scan.close();
break;
default:
System.out.println(
"Invalid input. Only enter a specified letter (not case sensitive). Try again");
break;
}
System.out.println("\n--Press Enter to continue--");
scan.nextLine();
}
System.out.println("Bye!");
}
}