-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomework4Programming1
More file actions
128 lines (118 loc) · 3.16 KB
/
Copy pathHomework4Programming1
File metadata and controls
128 lines (118 loc) · 3.16 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
public class ProgramOfStudy implements Iterable<Course>, Serializable
{
private List<Course> list;
/**
* Constructs an initially empty Program of Study.
*/
public ProgramOfStudy()
{
list = new LinkedList<Course>();
}
/**
* Adds the specified course to the end of the course list.
*
* @param course the course to add
*/
public void addCourse(Course course)
{
if (course != null)
list.add(course);
}
/**
* Finds and returns the course matching the specified prefix and number.
*
* @param prefix the prefix of the target course
* @param number the number of the target course
* @return the course, or null if not found
*/
public Course find(String prefix, int number)
{
for (Course course : list)
if (prefix.equals(course.getPrefix()) &&
number == course.getNumber())
return course;
return null;
}
/**
* Adds the specified course after the target course. Does nothing if
* either course is null or if the target is not found.
*
* @param target the course after which the new course will be added
* @param newCourse the course to add
*/
public void addCourseAfter(Course target, Course newCourse)
{
if (target == null || newCourse == null)
return;
int targetIndex = list.indexOf(target);
if (targetIndex != -1)
list.add(targetIndex + 1, newCourse);
}
/**
* Replaces the specified target course with the new course. Does nothing if
* either course is null or if the target is not found.
*
* @param target the course to be replaced
* @param newCourse the new course to add
*/
public void replace(Course target, Course newCourse)
{
if (target == null || newCourse == null)
return;
int targetIndex = list.indexOf(target);
if (targetIndex != -1)
list.set(targetIndex, newCourse);
}
/**
* Creates and returns a string representation of this Program of Study.
*
* @return a string representation of the Program of Study
*/
public String toString()
{
String result = "";
for (Course course : list)
result += course + "\n";
return result;
}
/**
* Returns an iterator for this Program of Study.
*
* @return an iterator for the Program of Study
*/
public Iterator<Course> iterator()
{
return list.iterator();
}
/**
* Saves a serialized version of this Program of Study to the specified
* file name.
*
* @param fileName the file name under which the POS will be stored
* @throws IOException
*/
public void save(String fileName) throws IOException
{
FileOutputStream fos = new FileOutputStream(fileName);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(this);
oos.flush();
oos.close();
}
/**
* Loads a serialized Program of Study from the specified file.
*
* @param fileName the file from which the POS is read
* @return the loaded Program of Study
* @throws IOException
* @throws ClassNotFoundException
*/
public static ProgramOfStudy load(String fileName) throws IOException, ClassNotFoundException
{
FileInputStream fis = new FileInputStream(fileName);
ObjectInputStream ois = new ObjectInputStream(fis);
ProgramOfStudy pos = (ProgramOfStudy) ois.readObject();
ois.close();
return pos;
}
}