-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudent.java
More file actions
171 lines (144 loc) · 4.55 KB
/
Student.java
File metadata and controls
171 lines (144 loc) · 4.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
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
public class Student extends Person implements Comparable <Person> {
private static int numStudents = 0;
private int studentID;
private Course[] coursesTaken;
private int numCoursesTaken;
private boolean isGraduate;
private String major;
//----------------------CONSTRUCTORS-------------------------------------------------------
public Student( ) {
super();
this.coursesTaken = new Course[50];
this.numCoursesTaken = 0;
this.isGraduate = false;
this.major = "undeclared";
this.studentID = ++numStudents;
}
public Student(boolean isGraduate) {
super();
this.coursesTaken = new Course[50];
this.numCoursesTaken = 0;
this.isGraduate = isGraduate;
this.major = "undeclared";
this.studentID = ++numStudents;
}
public Student(String major, boolean isGraduate) {
super();
this.coursesTaken = new Course[50];
this.numCoursesTaken = 0;
this.isGraduate = isGraduate;
this.major = major;
this.studentID = ++numStudents;
}
public Student(String name, int birthYear, String major, boolean isGraduate) {
super(name, birthYear);
this.coursesTaken = new Course[50];
this.numCoursesTaken = 0;
this.isGraduate = isGraduate;
this.major = major;
this.studentID = ++numStudents;
}
//------------------GETTERS AND SETTERS-------------------------------------------------------
public boolean isGraduate() {
return this.isGraduate;
}
public int getNumCoursesTaken() {
return this.numCoursesTaken;
}
public static int getNumStudents() {
return numStudents;
}
public int getStudentID() {
return this.studentID;
}
public String getMajor() {
return this.major;
}
public void setIsGraduate(boolean isGraduate) {
this.isGraduate = isGraduate;
}
public void setMajor(String major) {
this.major = major;
}
public Course[] getCoursesTaken() {
return this.coursesTaken;
}
//-------------------LOCAL METHODS-------------------------------------------------------------
public void addCourseTaken(Course course) {
if (numCoursesTaken < 50) {
coursesTaken[numCoursesTaken] = course;
++numCoursesTaken;
}
}
public void addCoursesTaken(Course [] course)
{
int j = course.length;
if ((numCoursesTaken + course.length) < coursesTaken.length)
{
for (int i = numCoursesTaken; i < (numCoursesTaken + course.length); ++i)
{
coursesTaken[i] = course[course.length - j];
--j;
}
}
}
public Course getCourseTaken(int index) {
if (index < 0 || index >= coursesTaken.length) return null;
else {
return coursesTaken[index];
}
}
public String getCourseTakenAsString(int index) {
if (index < 0 || index >= coursesTaken.length) return "";
else
{
return coursesTaken[index].getCourseDept() + "-" + coursesTaken[index].getCourseNum();
}
}
public String getAllCoursesTakenAsString() {
String s = "";
for (int i = 0; i < numCoursesTaken; ++i){
if (i == numCoursesTaken - 1) s += getCourseTakenAsString(i);
else s += getCourseTakenAsString(i) + ", ";
}
return s;
}
//-----------------------@Override-------------------------------------------------------------------------------------
@Override
public boolean equals(Object obj) {
if (obj instanceof Student) {
Student s = (Student) obj;
if (this.numCoursesTaken == s.getNumCoursesTaken()
&& this.isGraduate == s.isGraduate()
&& this.getAllCoursesTakenAsString().equals(s.getAllCoursesTakenAsString())
&& this.major.equals(s.getMajor())
&& this.studentID == s.getStudentID()
&& super.equals(s))
return true;
}
return false;
}
@Override
public String toString() {
String grad = "";
if (this.isGraduate) grad = "Graduate"; else grad = "Undergraduate";
return String.format( super.toString() + " Student: studentID: %04d | Major %20s | %14s | Number of Courses Taken: %3d | Courses Taken: %s"
,studentID, major, grad, numCoursesTaken, this.getAllCoursesTakenAsString());
}
@Override
public int compareTo(Person p) {
Student p1 = (Student) p;
if (this.getNumCredits(this.coursesTaken, this.numCoursesTaken) > p1.getNumCredits(p1.getCoursesTaken(), p1.getNumCoursesTaken()))
return 1;
else if (this.getNumCredits(this.coursesTaken, this.numCoursesTaken) == p1.getNumCredits(p1.getCoursesTaken(), p1.getNumCoursesTaken()))
return 0;
else return -1;
}
public int getNumCredits(Course [] c, int numCoursesTaken) {
int sum = 0;
for (int i = 0; i < numCoursesTaken; ++i) {
sum += c[i].getNumCredits();
}
return sum;
}
}