-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActiveCourse.java
More file actions
257 lines (218 loc) · 6.48 KB
/
ActiveCourse.java
File metadata and controls
257 lines (218 loc) · 6.48 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
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
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class ActiveCourse extends Course
{
/**
* Creating instance variables
* Three new variables:
* lectureStart: the time a lecture starts
* lectureDuration: how long a lecture is
* lectureDay: the day a lecture takes place
*/
public ArrayList<Student> students;
private String semester;
private int lectureStart;
private int lectureDuration;
private String lectureDay;
/**
* Constructor method that calls super() constructor in Course to initialize inherited variables, along with
* semester and enrolled students
* @param name A string containing the course name.
* @param code A string containing the course code.
* @param descr A string containing the course description.
* @param fmt A string containing how many weekly lectures and labs are taken for this course
* @param semester A string containing the semester the course is currently offered
* @param students An ArrayList of students enrolled in the course
* @param lectureStart an integer containing the start time of a lecture
* @param lectureDuration an integer containing the duration of a lecture
* @param lectureDay a string containing the day of the lecture
*/
public ActiveCourse(String name, String code, String descr, String fmt,String semester,ArrayList<Student> students)
{
super(name, code, descr, fmt);
this.semester = semester;
this.students = new ArrayList<Student>(students);
lectureStart = 0;
lectureDuration = 0;
lectureDay = "Mon";
}
/**
* a setter method that sets the lecture day to the parameter day
* @param day a string containing the lecture day
*/
public void setDay(String day)
{
lectureDay = day;
}
/**
* an accessor method that gets the lecture day
* @return a string containing the lecture day
*/
public String getDay()
{
return lectureDay;
}
/**
* a setter method that sets the lecture start time to the parameter time
* @param day a string containing the lecture start time
*/
public void setStartTime(int startTime)
{
lectureStart = startTime;
}
/**
* an accessor method that gets the lecture start time
* @return an int containing the lecture start time
*/
public int getStartTime()
{
return lectureStart;
}
/**
* a setter method that sets the lecture duration to the parameter duration
* @param day a string containing the lecture duration
*/
public void setDuration(int duration)
{
lectureDuration = duration;
}
/**
* an accessor method that gets the lecture duration
* @return an int containing the lecture duration
*/
public int getDuration()
{
return lectureDuration;
}
/**
* This accessor method returns the semester variable
* @return A string containing the semester the course is currently offered
*/
public String getSemester()
{
return semester;
}
/**
* This method prints all the students enrolled in this class
*/
public void printClassList()
{
for (int i = 0; i < students.size(); i++)
{
System.out.println(students.get(i).toString());
}
}
/**
* This method prints out all enrolled students and their grades
*/
public void printGrades()
{
for (int i = 0; i < students.size(); i++)
{
Student s = students.get(i);
System.out.println(s.getId() + " " + s.getName() + " " + s.getGrade(getCode()));
}
}
/**
* This method overrides the getDescription() method in superclass Course by calling that superclass method, and
* adding the semester and number of students currently enrolled in the course
* @return A string containing the course description plus the semester and number of students enrolled
*/
public String getDescription()
{
return super.getDescription() + " " + semester + " Enrolment: " + students.size() + "\n";
}
/**
* Accessor method to return the course description
* @return A string representing the course description
*/
public String getCourseDescription()
{
return getDescr();
}
/**
* This method returns the grade of a student for a given course. It searches the given student's
* list of credit courses for the course that matches this active course, and returns the grade stored
* in the credit course
* @param studentId, A string containing this student's ID
* @return double A double representing the student's grade for this course
*/
public double getGrade(String studentId)
{
for (int i = 0; i < students.size(); i++)
{
if (studentId.equals(students.get(i).getId()))
{
return students.get(i).getGrade(getCode());
}
}
return 0;
}
/**
* This method checks if a student is currently enrolled in this course
* @param studentId a string containing the student's id
* @return true if a student is enrolled, false otherwise
*/
public boolean enrolled(String studentId)
{
for (int i = 0; i < students.size(); i++)
{
if (studentId.equals(students.get(i).getId()))
return true;
}
return false;
}
/**
* This method removes a student from the course
* @param id a string containing the student's id
*/
public void remove(String id)
{
for (int j = 0; j < students.size(); j++)
{
Student s = students.get(j);
if (s.getId().equals(id))
{
students.remove(j);
return;
}
}
}
/**
* This method sorts the class list by alphabetically name. Makes use of the NameComparator class below
*/
public void sortByName()
{
Collections.sort(students, new NameComparator());
}
/**
* This class implements the comparator interface, and compares students based on their name.
* Calls the compareTo() method in the student class
*/
private class NameComparator implements Comparator<Student>
{
public int compare(Student a, Student b)
{
return a.getName().compareTo(b.getName());
}
}
/**
* This method sorts the class by student ID. Makes use of the IdComparator class below.
*/
public void sortById()
{
Collections.sort(students, new IdComparator());
}
/**
* This class implements the comparator interface, and compares students based on their student ID,
* in ascending order. Calls the compareTo() method in the student class
*/
private class IdComparator implements Comparator<Student>
{
public int compare(Student a, Student b)
{
return a.getId().compareTo(b.getId());
}
}
}