-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProject_SortingAlgorithms.java
275 lines (263 loc) · 10.2 KB
/
Project_SortingAlgorithms.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
package project_sortingalgorithms;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
*
* @author hannahgsimon
*/
public class Project_SortingAlgorithms
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
ArrayList<Student> students = new ArrayList<>();
ArrayList<Faculty> faculty = new ArrayList<>();
int sel = 9; //arbitrary
while(true)
{
sel = 9; //arbitrary
System.out.println("""
1) Author Info
2) Add Student
3) Add Faculty
4) Sort by ID (selection sort)
5) Sort by ID (radix sort)
6) Sort Students by GPA (merge sort)
7) Sort Faculty by Course load (insertion sort)
8) Print data
0) Exit""");
try
{
sel = input.nextInt();
if ((sel < -1) || (sel > 8))
{
System.err.println("Invalid selection; Please enter an integer 0-8.");
}
}
catch (InputMismatchException e)
{
System.err.println("Invalid selection; Please enter an integer 0-8.");
input.next();
}
if (sel == 0)
{
break;
}
else if (sel == 1)
{
System.out.println("\nAuthor Info: Hannah Simon\n");
}
else if (sel == 2)
{
students.add(new Student(input));
}
else if (sel == 3)
{
System.out.println("\nEnter Name");
String name = input.next();
System.out.println("\nEnter id");
int id = 0;
boolean gotCorrect = false;
while (!gotCorrect)
{
try
{
id = input.nextInt();
if (id < 0)
{
System.err.println("Error; Please enter a positive integer number.");
}
else
{
System.out.println("id " + id + " has successfully been added to faculty " + name + "\n");
gotCorrect = true;
}
}
catch (InputMismatchException e)
{
System.err.println("Error; Please enter an integer number.");
input.next();
}
}
input.skip(".*");
System.out.println("Enter Course Load");
int courseLoad = 0;
gotCorrect = false;
while (!gotCorrect)
{
try
{
courseLoad = input.nextInt();
if (courseLoad < 0)
{
System.err.println("Error; Please enter a positive integer number.");
}
else
{
System.out.println("Courseload " + courseLoad + " has successfully been added to faculty " + name + "\n");
gotCorrect = true;
}
}
catch (InputMismatchException e)
{
System.err.println("Error; Please enter an integer number.");
input.next();
}
}
faculty.add(new Faculty(name, id, courseLoad));
}
else if (sel == 4)
{
System.out.println("1) Sort Students\n2) Sort Faculty\n");
boolean gotCorrect = false;
while (!gotCorrect)
{
try
{
sel = input.nextInt();
if (sel != 1 && sel != 2)
{
System.err.println("Error; Please enter 1 or 2.");
}
else
{
gotCorrect = true;
}
}
catch (InputMismatchException e)
{
System.err.println("Error; Please enter an integer number.");
input.next();
}
}
input.skip(".*");
if (sel == 1)
{
new SortMethods<Student>().selectionSortAscendingId(students);
}
else if (sel == 2)
{
new SortMethods<Faculty>().selectionSortAscendingId(faculty);
}
}
else if (sel == 5)
{
//name
System.out.println("1) Sort Students\n2) Sort Faculty\n");
boolean gotCorrect = false;
while (!gotCorrect)
{
try
{
sel = input.nextInt();
if (sel != 1 && sel != 2)
{
System.err.println("Error; Please enter 1 or 2.");
}
else
{
gotCorrect = true;
}
}
catch (InputMismatchException e)
{
System.err.println("Error; Please enter an integer number.");
input.next();
}
}
input.skip(".*");
if (sel == 1)
{
if (students.isEmpty())
{
System.out.printf("No students entered in database, no data to sort.\n\n");
}
else
{
students = new SortMethods<Student>().radixSortDescendingId(students);
}
}
else if (sel == 2)
{
if (faculty.isEmpty())
{
System.out.printf("No faculty entered in database, no data to sort.\n\n");
}
else
{
faculty = new SortMethods<Faculty>().radixSortDescendingId(faculty);
}
}
}
else if (sel == 6)
{
//gpa
//new SortMethods<Student>().bubbleSortComparator(students, new Comparator<Student>(){
new SortMethods<Student>().mergeSortAscendingGpa(students, new Comparator<Student>(){
@Override
public int compare(Student o1, Student o2) {
if (o1.getGPA() == o2.getGPA())
{
return(0);
}
else if (o1.getGPA() < o2.getGPA())
{
return(-1);
}
else
{
return(1);
}
}
});
}
else if (sel == 7)
{
//course load
new SortMethods<Faculty>().insertionSortAscendingCouseLoad(faculty, (Comparator<Faculty>) (Faculty o1, Faculty o2) -> {
if (o1.getCourseLoad() == o2.getCourseLoad())
{
return(0);
}
else if (o1.getCourseLoad() < o2.getCourseLoad())
{
return(-1);
}
else
{
return(1);
}
});
}
else if (sel == 8)
{
System.out.printf("\nStudent Data: (Name: ID GPA)\n%s\n\nFaculty Data: (Name: ID Courseload)\n%s\n\n", students, faculty);
}
else if (sel == -1)
{
faculty.add(new Faculty("Omega", 2, 1));
faculty.add(new Faculty("Gamma", 20, 3));
faculty.add(new Faculty("Alpha", 12, 8));
faculty.add(new Faculty("Beta", 50, 6));
faculty.add(new Faculty("Beta", 30, 19));
faculty.add(new Faculty("Beta", 49, 15));
faculty.add(new Faculty("Beta", 48, 20));
faculty.add(new Faculty("Beta", 33, 11));
faculty.add(new Faculty("Beta", 38, 32));
faculty.add(new Faculty("Beta", 35, 23));
students.add(new Student("Omega", 56, 8));
students.add(new Student("Gamma", 19, 6));
students.add(new Student("Gamma", 24, 13.4));
students.add(new Student("Alpha", 92, 3.2));
students.add(new Student("Beta", 54, 22.7));
students.add(new Student("Omega", 36, 18.6));
students.add(new Student("Gamma", 31, 39.1));
students.add(new Student("Gamma", 77, 42.9));
students.add(new Student("Alpha", 22, 33.2));
students.add(new Student("Beta", 43, 53.8));
}
}
}
}