- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJavaPriorityQueue.java
More file actions
Latest commit
103 lines (84 loc) · 2.45 KB
/
Copy pathJavaPriorityQueue.java
File metadata and controls
103 lines (84 loc) · 2.45 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
packageDataStructure;
importjava.util.ArrayList;
importjava.util.List;
importjava.util.Scanner;
importjava.util.Comparator;
importjava.util.PriorityQueue;
classStudent3 {
finalprivateintid;
finalprivateStringname;
finalprivatedoublecgpa;
publicStudent3(intid, Stringname, doublecgpa) {
super();
this.id = id;
this.name = name;
this.cgpa = cgpa;
}
publicintgetID() {
returnid;
}
publicStringgetName() {
returnname;
}
publicdoublegetCGPA() {
returncgpa;
}
}
classChecker3implementsComparator<Student3> {
@Override
publicintcompare(Student3s1, Student3s2) {
if (s1.getCGPA() < s2.getCGPA()) {
return1;
}
if (s1.getCGPA() == s2.getCGPA()) {
intcomp = s1.getName().compareTo(s2.getName());
if (comp == 0) {
returns1.getID() > s2.getID() ? 1 : -1;
}
returncomp;
}
return -1;
}
}
classPriorities {
publicList<Student3> getStudents(List<String> events) {
PriorityQueue<Student3> queue = newPriorityQueue<>(newChecker3());
for (Strings : events) {
if ("SERVED".equals(s)) {
queue.poll();
} else {
String[] parts = s.split(" ");
queue.add(newStudent3(
Integer.parseInt(parts[3]),
parts[1],
Double.parseDouble(parts[2])
));
}
}
List<Student3> students = newArrayList<>();
while (!queue.isEmpty()) {
students.add(queue.poll());
}
returnstudents;
}
}
publicclassJavaPriorityQueue {
privatefinalstaticScannerscan = newScanner(System.in);
privatefinalstaticPrioritiespriorities = newPriorities();
publicstaticvoidmain(String[] args) {
inttotalEvents = Integer.parseInt(scan.nextLine());
List<String> events = newArrayList<>();
while (totalEvents-- != 0) {
Stringevent = scan.nextLine();
events.add(event);
}
List<Student3> students = priorities.getStudents(events);
if (students.isEmpty()) {
System.out.println("EMPTY");
} else {
for (Student3st : students) {
System.out.println(st.getName());
}
}
}
}