-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCourse.java
119 lines (81 loc) · 2.64 KB
/
Course.java
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
import java.lang.reflect.Array;
public class Course {
private String courseCode;
private String courseName;
private int currentEnrollment;
private Student[] studentsInCourse;
public Course() {
courseCode = "";
courseName = "";
currentEnrollment = 0;
studentsInCourse = new Student[50];
};
public Course (String courseCode, String courseName){
this.courseCode = courseCode;
this.courseName = courseName;
}
// methods
public void addStudent(Student s) throws StudentLimitException, DuplicateStudentException{
//do something here to add student to the array
if (currentEnrollment == 50){
throw new StudentLimitException(); }
if (currentEnrollment == 0) {
studentsInCourse[0] = s;
currentEnrollment ++; }
else {
for (int i = 0; i<currentEnrollment; i++) {
if (s.getLastName().compareTo(studentsInCourse[i].getLastName()) < 0){
for (int j = currentEnrollment; j>i; j--) {
studentsInCourse[j] = studentsInCourse[j-1];
}
currentEnrollment++;
studentsInCourse[i] = s;
break; }
else if(s.getLastName().equals(studentsInCourse[i].getLastName())) {
if(s.getStudentID() < studentsInCourse[i].getStudentID()) {
for(int j = currentEnrollment; j>i; j-- ) {
studentsInCourse[j] = studentsInCourse[j-1];
currentEnrollment++;
studentsInCourse[i] = s;
break;
}
}
else if (s.getStudentID() == studentsInCourse[i].getStudentID()){
throw new DuplicateStudentException(); }; } } }
}
public void removeStudent(int studentID) throws EmptyStudentListException, StudentNotFoundException {
if (currentEnrollment == 0) { throw new EmptyStudentListException();};
for (int i = 0; i < currentEnrollment; i++) {
if (studentID == studentsInCourse[i].getStudentID()) {
for (int j = i + 1; j < currentEnrollment; j++) {
studentsInCourse[i] = studentsInCourse[j]; }
studentsInCourse[currentEnrollment - 1] = null;
currentEnrollment --;
break;
}
else if (i == currentEnrollment - 1) {
throw new StudentNotFoundException(); }
} }
//Setter and Getters :)
public void setCourseCode(String c) {
this.courseCode = c;
}
public void setCourseName(String n) {
this.courseName = n;
}
public void setCurrentEnrollment(int s) {
this.currentEnrollment = s;
}
public String getCourseCode() {
return this.courseCode;
}
public String getCourseName() {
return this.courseName;
}
public int getCurrentEnrollment() {
return this.currentEnrollment;
}
public Student[] getStudentsInCourse() {
return this.studentsInCourse;
}
}