This repository was archived by the owner on Feb 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDepartment.java
More file actions
143 lines (110 loc) · 3.43 KB
/
Copy pathDepartment.java
File metadata and controls
143 lines (110 loc) · 3.43 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
/*import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileOutputStream;*/
import java.io.Serializable;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
public class Department implements Serializable {
private static final long serialVersionUID = 1L;
private String code;
private String name;
private LinkedList<Student> students = new LinkedList<Student>();
public Department(String code, String name) {
this.code = code;
this.name = name;
/* try {
ObjectInputStream objReader = new ObjectInputStream(new FileInputStream("departments.dat"));
} catch (FileNotFoundException createFile) {
System.out.println("File was not found, creating file with default values");
try {
ObjectOutputStream objCreator = new ObjectOutputStream(new FileOutputStream("departments.dat"));
objCreator.writeObject(students);
objCreator.close();
} catch (IOException e) {
}
} catch (IOException e) {
System.out.println(e.getMessage());
} */
}
static final Comparator<Student> ID_ORDER = new Comparator<Student>() {
@Override
public int compare(Student stu1, Student stu2) {
int ID1 = stu1.getId();
int ID2 = stu2.getId();
if (ID1 == ID2)
return 0;
else if (ID1 > ID2)
return 1;
else
return -1;
}
};
static final Comparator<Student> GPA_ORDER = new Comparator<Student>() {
public int compare(Student stu1, Student stu2) {
if (stu1.getGPA() > stu2.getGPA())
return 1;
else if (stu1.getGPA() < stu2.getGPA())
return -1;
else
return 0;
}
};
static final Comparator<Student> NAME_ORDER = new Comparator<Student>() {
@Override
public int compare(Student stu1, Student stu2) {
String name1 = stu1.getName();
String name2 = stu2.getName();
return name1.compareTo(name2);
}
};
public void sortStudents(int order) {
if (order == 0)
Collections.sort(students, ID_ORDER);
else if (order == 1)
Collections.sort(students, GPA_ORDER);
else if (order == 2)
Collections.sort(students, NAME_ORDER);
}
public LinkedList<Student> getStudents() {
return students;
}
public void setStudents(LinkedList<Student> students) {
this.students = students;
}
public String toString() {
String details = code + " " + name;
return details;
}
public void addStudent(Student student) {
students.add(student);
}
/*
* public static void main(String[] args) { LinkedList<Department> dpt; try {
* ObjectInputStream objReader = new ObjectInputStream(new
* FileInputStream("departments.dat"));
*
* LinkedList<Department> readObject = extracted(objReader);
* LinkedList<Department> temp = readObject;
*
* System.out.println(temp); objReader.close();
*
* } catch (FileNotFoundException createFile) {
*
* System.out.println("File was not found, creating file with default values");
*
* } catch (IOException e) { } catch (ClassNotFoundException e) { // TODO
* Auto-generated catch block e.printStackTrace(); }
*
*
*
* }
*
* private static LinkedList<Department> extracted(ObjectInputStream objReader)
* throws IOException, ClassNotFoundException { return (LinkedList<Department>)
* objReader.readObject(); }
*/
}