-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudent.java
More file actions
235 lines (197 loc) · 6.44 KB
/
Student.java
File metadata and controls
235 lines (197 loc) · 6.44 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
import java.util.ArrayList;
import java.util.Scanner;
import java.io.Serializable;
public class Student extends User implements StudentInterface, Serializable {
private static final long serialVersionUID = 1L;
private ArrayList<Course> myCourses = new ArrayList<Course>();
static Scanner input = new Scanner(System.in);
int studentID;
/**
* Constructs a Student Object
* @param username
* @param password
* @param firstName
* @param lastName
*/
public Student(String username, String password, String firstName, String lastName) {
// TODO Auto-generated constructor stub
super(username, password, firstName, lastName);
}
//GETTERS
/**
* @return First name of the student.
*/
public String getFirstName() {
String fName = this.firstName;
return fName;
}
/**
* @return Last name of the Student.
*/
public String getLastName() {
String lName = this.lastName;
return lName;
}
/**
* @return User name of the student.
*/
public String getUsername() {
String userName = this.username;
return userName;
}
/**
* @return Password of the student.
*/
public String getPassword() {
String passWord = this.password;
return passWord;
}
/**
* @return Course list of the student.
*/
public ArrayList<Course> getCourseList() {
ArrayList<Course> courseList = this.myCourses;
return courseList;
}
/**
* @return Unique student ID.
*/
public int getStudentID() {
int studentID = this.studentID;
return studentID;
}
//SETTERS
/**
* Sets studentID.
*/
public void setStudentID(int studentID) {
this.studentID = studentID;
}
//STUDENT MENU METHODS
/**
* Prints all courses in the system.
* @param allCourses
*/
public void viewAllCourses(ArrayList<Course> allCourses) {
System.out.println("--View All Courses--");
System.out.println("");
System.out.println("");
System.out.println("Course Name ----- Section Number");
System.out.println("");
//iterate through the list of all course objects
for(int i=0; i < allCourses.size(); i++) {
//print Course Name & Section Number
System.out.println(allCourses.get(i).getCourseName() + " ------ " + allCourses.get(i).getSectionNumber());
}
}
/**
* Prints all 'open' courses in the system
* @param allCourses
*/
public void viewAllOpenCourses(ArrayList<Course> allCourses) {
System.out.println("--View All Open Courses--");
System.out.println("");
for(int i=0; i < allCourses.size(); i++) {
if (allCourses.get(i).getMaxNumOfStudents() > allCourses.get(i).getNumOfEnrolledStudents()) {
System.out.println(allCourses.get(i).getCourseName());
}
}
}
/**
* Register a student to a given course.
* Adds student's first and last name to appropriate course's student list.
* Adds the course to student's course list.
* @param allCourses
* @param allStudents
*/
public void registerToCourse(ArrayList<Course> allCourses, ArrayList<Student> allStudents) {
System.out.println("--Register to a Course--");
System.out.println("");
System.out.println("Enter Course Name: ");
String courseName = input.nextLine();
System.out.println("Enter Section Number #: ");
while(!input.hasNextInt()) {
System.out.println("Please enter an integer: ");
input.next();
}
int courseSection = input.nextInt();
//skip to next line
input.nextLine();
System.out.println("Enter first name: ");
String firstName = input.nextLine();
System.out.println("Enter last name: ");
String lastName = input.nextLine();
//get student object from student first and last name
for (int i=0; i < allStudents.size(); i++) {
Student stu = allStudents.get(i);
if((firstName.equals(stu.getFirstName())) && (lastName.equals(stu.getLastName()))) {
//get course object from course name and section number
for (int j=0; j < allCourses.size(); j++) {
Course crs = allCourses.get(j);
if ((courseName.equals(crs.getCourseName()) && (courseSection == crs.getSectionNumber()))) {
//add student first and last name to course's student object list
if (crs.getMaxNumOfStudents() > crs.getNumOfEnrolledStudents()) {
crs.getListOfStudents().add(stu);
crs.setNumOfEnrolledStudents(crs.getNumOfEnrolledStudents()+1);
this.myCourses.add(crs);
}
else {
System.out.println("Sorry, this course is full :(");
}
}
}
}
}
}
/**
* Withdraw a student from a given course.
* Removes Student object from course's student list.
* Removes Course from student's course list.
* @param allCourses
*/
public void withdrawFromCourse(ArrayList<Course> allCourses) {
System.out.println("--Withdraw from a Course--");
System.out.println("");
System.out.println("Enter course name: ");
String courseName = input.nextLine();
System.out.println("Enter first name: ");
String firstName = input.nextLine();
System.out.println("Enter last name: ");
String lastName = input.nextLine();
//get course object from section number
for (int i=0; i < allCourses.size(); i++) {
if (courseName.equals(allCourses.get(i).getCourseName())) {
Course course = allCourses.get(i);
//iterate through course's list of students
for (int j=0; j < course.getListOfStudents().size(); j++) {
//if found remove first and last name from list of student objects
if ((course.getListOfStudents().get(j).getFirstName().equals(firstName)) && (course.getListOfStudents().get(j).getLastName().equals(lastName))) {
Student stu = course.getListOfStudents().get(j);
course.getListOfStudents().remove(j);
//decrease enrolled student number by 1
course.setNumOfEnrolledStudents(course.getNumOfEnrolledStudents() - 1);
//remove course object from student's list of course objects
for (int k =0; k < stu.getCourseList().size(); k++) {
if(stu.getCourseList().get(k).getCourseName().equals(courseName)) {
stu.getCourseList().remove(k);
}
}
}
}
}
}
input.close();
}
/**
* Prints all courses a student is registered in.
*/
public void viewMyCourses() {
System.out.println("--View My Courses--");
System.out.println("");
//get student's list of course objects
for (int i=0; i < this.getCourseList().size(); i++) {
System.out.println(this.getCourseList().get(i).getCourseName());
}
System.out.println("");
}
}