-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPeopleBag.java
More file actions
116 lines (97 loc) · 2.55 KB
/
PeopleBag.java
File metadata and controls
116 lines (97 loc) · 2.55 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
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class PeopleBag implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
List<Person> personList;
List<Student> studentList;
List<Faculty> facultyList;
private String result; // return value of Display
public PeopleBag() {
personList = new ArrayList<>();
studentList = new ArrayList<>();
facultyList = new ArrayList<>();
result = "";
}
public void add(Person person) {personList.add(person);}
public void add(Student student) {studentList.add(student);}
public void add(Faculty faculty) {facultyList.add(faculty);}
public String display() {
// Person
result += "Person\n";
for (Person p: personList)
result += p.toString() + "\n";
// Students
result += "\nStudent\n";
for (Student s: studentList)
result += s.toString() + "\n";
// Faculty
result += "\nFaculty\n";
for (Faculty f: facultyList)
result += f.toString() + "\n";
return result;
}
@SuppressWarnings("unchecked")
public <T> T find(Integer id) {
for (Person p: personList) {
if (id.equals(p.getID())){
return (T)p;
}
}
for (Student s: studentList) {
if (id.equals(s.getID()))
return (T)s;
}
for (Faculty f: facultyList) {
if (id.equals(f.getID()))
return (T)f;
}
return null;
}
public boolean delete(Integer id) {
for (Person p: personList) {
if (id.equals(p.getID())){
personList.remove(p);
return true;
}
}
for (Student s: studentList) {
if (id.equals(s.getID())){
studentList.remove(s);
return true;
}
}
for (Faculty f: facultyList) {
if (id.equals(f.getID())){
facultyList.remove(f);
return true;
}
}
return false;
}
public void save(String name) throws IOException {
// write object to file
FileOutputStream fos = new FileOutputStream(name);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(this);
oos.close();
}
public void load(String name) throws IOException, ClassNotFoundException {
// read object from file
FileInputStream fis = new FileInputStream(name);
ObjectInputStream ois = new ObjectInputStream(fis);
PeopleBag result = (PeopleBag) ois.readObject();
this.personList = result.personList;
this.studentList = result.studentList;
this.facultyList = result.facultyList;
ois.close();
}
}