-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCourseSection.java
348 lines (298 loc) · 7.37 KB
/
CourseSection.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.io.*;
/**
* <h1>Student</h1>
* The program represents a student.
*
* @authors Javier Campos & Alberto Pizano
* @version 1.0
* @since 2018-04-18
*/
/**
* <h1>Course Section</h1>
* The program represents a course section.
*
* @authors Alberto Pizano
* @version 1.0
* @since 2018-04-18
*/
public class CourseSection implements Iterable<Student> {
private static String assignsLabel;
private static String highestMarks;
private ArrayList<Student> students;
/**
* Constructor for CourseSection class.
*
*/
public CourseSection()
{
assignsLabel = "ID A1 A2 A3 A4 Midterm Final";
highestMarks = " 35 35 35 30 30 100";
students = new ArrayList<Student>();
}
/**
* @param n
*/
public CourseSection(String n) {
students = new ArrayList<Student>();
}
/* (non-Javadoc)
* @see java.lang.Iterable#iterator()
*/
public Iterator<Student> iterator() { return this.students.iterator(); }
/** Get Method to return ArrayList of Students objects.
* @return ArrayList of Student objects.
*/
public ArrayList<Student> getStudents() { return this.students; }
/**
* Get method to return string assignsLabel
* @return string assignsLabel
*/
public static String getassignsLabel() { return assignsLabel; }
/**
* Get method to return string highestMarks
* @return String highestMarks
*/
public static String gethighestMarks() { return highestMarks; }
/**
* Method to add a new Student object to the students ArrayList with the given int and float values that are passed as parameters.
* @param id id of the Student object.
* @param a1 assignment 1 grade of Student object.
* @param a2 assignment 2 grade of Student object.
* @param a3 assignment 3 grade of Student object.
* @param a4 assignment 4 grade of Student object.
* @param midterm midterm grade of Student object.
* @param finalExam final exam grade of Student object.
*/
public void addStudents(int id, float a1, float a2, float a3, float a4, float midterm, float finalExam) {
Student s = new Student(id,a1,a2,a3,a4,midterm,finalExam);
students.add(s);
}
/**
* Will add a new student to the ArrayList.
* @param s A Student object.
*/
public void addStudent(Student s) {
students.add(s);
}
public void removeStudent(int index) {
Iterator studentIterator = students.iterator();
while(studentIterator.hasNext()){
if(students.indexOf(studentIterator.next()) == index)
studentIterator.remove();
}
}
/**
*
*/
public void listStudents() {
for(Student s: students){
System.out.println(s.getID());
}
}
/**
* @param index
* @return
*/
public String pullID(int index) {
int i;
String id = "";
Student s = new Student();
for(i=0; i<students.size(); i++){
if(index == i)
{
s = students.get(i);
id = String.valueOf(s.getID());
}
}
return id;
}
/**
* @param index
* @return
*/
public String pullA1(int index) {
int i;
String a1 = "";
Student s = new Student();
for(i=0; i<students.size(); i++){
if(index == i)
{
s = students.get(i);
a1 = String.valueOf(s.getA1());
}
}
return a1;
}
/**
* @param index
* @return
*/
public String pullA2(int index) {
int i;
String a2 = "";
Student s = new Student();
for(i=0; i<students.size(); i++){
if(index == i)
{
s = students.get(i);
a2 = String.valueOf(s.getA2());
}
}
return a2;
}
public String pullA3(int index){
int i;
String a3 = "";
Student s = new Student();
for(i=0; i<students.size(); i++){
if(index == i)
{
s = students.get(i);
a3 = String.valueOf(s.getA3());
}
}
return a3;
}
public String pullA4(int index){
int i;
String a4 = "";
Student s = new Student();
for(i=0; i<students.size(); i++){
if(index == i)
{
s = students.get(i);
a4 = String.valueOf(s.getA4());
}
}
return a4;
}
public String pullMidterm(int index) {
int i;
String midterm = "";
Student s = new Student();
for(i=0; i<students.size(); i++){
if(index == i)
{
s = students.get(i);
midterm = String.valueOf(s.getMidterm());
}
}
return midterm;
}
public String pullFinalExam(int index) {
int i;
String finalExam = "";
Student s = new Student();
for(i=0; i<students.size(); i++){
if(index == i)
{
s = students.get(i);
finalExam = String.valueOf(s.getFinalExam());
}
}
return finalExam;
}
public String pullFinalGrade(int index){
int i;
float finalGrade = 0f;
Student s = new Student();
for(i=0; i<students.size(); i++){
if(index == i)
{
s = students.get(i);
finalGrade = s.getFinalGrade();
}
}
return calcGrade(finalGrade);
}
public static String calcGrade(float n) {
if(n >= 90)
return "A";
else if ((n < 90) && (n >= 70))
return "B";
else if((n < 70) && (n >= 60))
return "C";
else if((n < 60) && (n >= 50))
return "D";
else
return "F";
}
/**
* Method to convert original Student ArrayList to a single Integer array of student IDs.
* @param students
* @return An Integer array of consisting of students IDs
*/
public Integer[] idList(ArrayList<Student> students) {
Integer[] ids = new Integer[students.size()];
for(int i=0; i<students.size(); i++) {
Student s = students.get(i);
ids[i] = s.getID();
}
return ids;
}
/**
* Method to compute the size of ArrayList.
* @return int value of the size of ArrayList.
*/
public int getSize() {
int size = 0 ;
int i;
for(i = 0; i <students.size(); i++) {
size++;
}
return size;
}
public void saveTo(PrintWriter aFile) {
aFile.println(assignsLabel);
aFile.println(highestMarks);
for (Student s: students) {
s.saveTo(aFile);
}
}
/**
* Reads aFile except for the header and continuously creates new student objects
* for a new CourseSection.
* @param aFile File to read from.
* @return CourseSection New CourseSection read from aFile
*/
public static CourseSection loadFrom(BufferedReader aFile) throws IOException {
CourseSection course = new CourseSection(aFile.readLine());
aFile.readLine(); // skips line
while (aFile.ready()) //read until no more available (i.e., not ready)
{
course.addStudent(Student.loadFromST(aFile)); //read & add the student
}
return course;
}
/**
* Method that removes the Student object from ArrayList at the index that is passed as parameter.
* @param index
*/
public void realRemove(int index) {
for(int i=0; i<students.size(); i++) {
Student s = students.get(i);
if(index == students.indexOf(s))
students.remove(index);
}
}
/**
* Method to search through student IDs inside students ArrayList and returns int value of index of ID being searched for.
* @param id of student you are searching for.
* @return the int value of the index containing the id passed as parameter.
*/
public int realSearch(int id) {
int ans = 0;
for(int i = 0; i<students.size(); i++) {
Student s = students.get(i);
if(s.getID() == id) {
ans = students.indexOf(s);
return ans;
}
}
return -1;
}
}