-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranscript.java
More file actions
794 lines (716 loc) · 20.8 KB
/
Transcript.java
File metadata and controls
794 lines (716 loc) · 20.8 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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
/**
* This class generates a transcript for each student, whose information is received from
* the text file.
*
*@author Steeve Johan Otoka Eyota
*@version 1.0
*@since 2020-12-01
*/
public class Transcript {
/**
* The list of grades.
*/
private ArrayList<Object> grade = new ArrayList<Object>();
/**
* The name of the input file of the transcript.
*/
private File inputFile;
/**
* The name of the output file of the transcript.
*/
private String outputFile;
/**
* This the the constructor for Transcript class that initializes its instance
* variables and call readFie private method to read the file and construct
* this grade.
*
* @param inFile is the name of the input file.
* @param outFile is the name of the output file.
*/
public Transcript(String inFile, String outFile) {
inputFile = new File(inFile);
outputFile = outFile;
grade = new ArrayList<Object>();
this.readFile();
}// end of Transcript constructor
/**
* This method reads a text file and add each line as an entry of grade
* ArrayList.
*
* @exception It throws FileNotFoundException if the file is not found.
*/
private void readFile() {
Scanner sc = null;
try {
sc = new Scanner(inputFile);
while (sc.hasNextLine()) {
grade.add(sc.nextLine());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
sc.close();
}
} // end of readFile
/**
* This method creates and returns an ArrayList,
* whose element is an object of class Student.
* The object at each element is created by aggregating
* ALL the information, that is found for one student in
* the grade Arraylist of class Transcript. (i.e. if the
* text file contains information about 9 students, then
* the array list will have 9 elements).
*
* @return studentArray is an ArrayList of student.
*/
public ArrayList<Student> buildStudentArray() {
// create the return array
ArrayList<Student> studentArray = new ArrayList<Student>();
// access the info of the input file located in the array grade
for (Object obj : grade) {
// type cast every object into a string
String line = (String) obj;
// split all the information into an array so that we can manipulate them easily
String[] arrOfInfo = line.split(",");
// create a new student
Student s = new Student();
s.setStudentID(arrOfInfo[2]);
s.setName(arrOfInfo[arrOfInfo.length - 1]);
// add course
s.addCourse(newCourse(arrOfInfo));
// add grade
s.addGrade(listOfGrade(arrOfInfo), listOfWeight(arrOfInfo));
// add student to the list if the list of student does not contain this student
if (!studentArray.contains(s)) {
studentArray.add(s);
}
// otherwise, add a course and final grade for this student
else {
studentArray.get(studentArray.indexOf(s)).addCourse(newCourse(arrOfInfo));
studentArray.get(studentArray.indexOf(s)).addGrade(listOfGrade(arrOfInfo), listOfWeight(arrOfInfo));
}
}
return studentArray;
}
/**
* This is the method that prints the transcript to
* the given file (i.e. outputFile attribute).
*
* @param studentArray is an ArrayList of students.
*/
public void printTranscript(ArrayList<Student> studentArray) {
// create new file
try {
FileWriter myWriter = new FileWriter(outputFile);
for (Student s : studentArray) {
myWriter.write(s.getName() + "\t" + s.getStudentID() + "\r\n");
myWriter.write("--------------------" + "\r\n");
for (int i = 0; i < s.getCourseTaken().size(); i++) {
myWriter.write(s.getCourseTaken().get(i).getCode() + "\t" + s.getFinalGrade().get(i) + "\r\n");
}
myWriter.write("--------------------" + "\r\n");
myWriter.write("GPA: " + s.weightedGPA() + "\r\n");
myWriter.write("" + "\r\n");
}
myWriter.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
public ArrayList<Object> getGrade() {
return grade;
}
public void setGrade(ArrayList<Object> grade) {
this.grade = grade;
}
public File getInputFile() {
return inputFile;
}
public void setInputFile(String inputFile) {
this.inputFile = new File(inputFile);
}
public String getOutputFile() {
return outputFile;
}
public void setOutputFile(String outputFile) {
this.outputFile = outputFile;
}
// The following methods are helper methods used by the method buildStudentArray()
/**
* This is the method creates and returns a new course for a student.
*
* @param arrOfInfo is an array information for a student.
* @return a list of courses.
*/
private Course newCourse(String[] arrOfInfo) {
String code = arrOfInfo[0];
double credit = Double.parseDouble(arrOfInfo[1]);
ArrayList<Assessment> assess = arrOfAssessment(arrOfInfo);
return new Course(code, assess, credit);
} // end of newCourse
/**
* This method creates and returns a list of Assessments.
*
* @param arrOfInfo is an array information for a student.
* @return a list of assessments.
*/
private ArrayList<Assessment> arrOfAssessment(String[] arrOfInfo) {
ArrayList<Assessment> listOfAssessment = new ArrayList<Assessment>();
for (int i = 3; i < arrOfInfo.length - 1; i++) {
char type = arrOfInfo[i].charAt(0);
int weight = Integer.parseInt(arrOfInfo[i].substring(1, arrOfInfo[i].indexOf("(")));
Assessment assess = Assessment.getInstance(type, weight);
listOfAssessment.add(assess);
}
return listOfAssessment;
} // end of arrOfAssessment
/**
* This method that creates and returns a list of Assessments.
*
* @param arrOfInfo is an array information for a student.
* @return a list of assessments.
*/
private ArrayList<Double> listOfGrade(String[] arrOfInfo) {
ArrayList<Double> listOfGrade = new ArrayList<Double>();
for (int i = 3; i < arrOfInfo.length - 1; i++) {
listOfGrade.add(Double.parseDouble(arrOfInfo[i].substring(arrOfInfo[i].indexOf("(") + 1, arrOfInfo[i].indexOf(")"))));
}
return listOfGrade;
} // end of listOfGrade
/**
* This method creates and returns a list of weight.
*
* @param arrOfInfo is an array information for a student.
* @return a list of weight.
*/
private ArrayList<Integer> listOfWeight(String[] arrOfInfo) {
ArrayList<Integer> listOfWeight = new ArrayList<Integer>();
for (int i = 3; i < arrOfInfo.length - 1; i++) {
listOfWeight.add(Integer.parseInt(arrOfInfo[i].substring(1, arrOfInfo[i].indexOf("("))));
}
return listOfWeight;
} // end of listOfWeight
} // end of Transcript
/**
* This class generates a Student who has a transcript, whose information is in
* the text file.
*/
class Student {
/**
* The ID of the student.
*/
private String studentID;
/**
* The name of the student.
*/
private String name;
/**
* The list of course taken by the student.
*/
private ArrayList<Course> courseTaken;
/**
* The list of final grades received by the student for each course taken.
*/
private ArrayList<Double> finalGrade;
/**
* This is the no argument constructor for the class Student.
* Initializes this student so that it has default values.
*/
public Student() {
this("", "", new ArrayList<Course>());
this.finalGrade = new ArrayList<Double>();
}
/**
* This is the overloaded constructor for the class Student.
* Initializes this student so that it has the specified studentId, name, courseTaken, and finalGrade.
*
* @param studentID is the ID of the student.
* @param name is the name of the student.
* @param courseTaken is the list of course taken by the student.
*/
public Student(String studentID, String name, ArrayList<Course> courseTaken) {
this.studentID = studentID;
this.name = name;
this.courseTaken = courseTaken;
}
/**
* This method gets the ID of this student.
*
* @return the ID of this student.
*/
public String getStudentID() {
return studentID;
}
/**
* This method sets the ID of the student.
*
* @param studentID is the ID of the student.
*/
public void setStudentID(String studentID) {
this.studentID = studentID;
}
/**
* This method gets the name of this student.
*
* @return the name of this student.
*/
public String getName() {
return name;
}
/**
* This method sets the name of the student to the given value.
*
* @param name is the name of the student.
*/
public void setName(String name) {
this.name = name;
}
/**
* This method gets the list of courses taken by this student.
*
* @return the list of courses taken by this student.
*/
public ArrayList<Course> getCourseTaken() {
ArrayList<Course> arrOfCourse = new ArrayList<Course>();
for(Course c : this.courseTaken) {
arrOfCourse.add(new Course(c.getCode(), c.getAssignment(), c.getCredit()));
}
return arrOfCourse;
}
public void setCourseTaken(ArrayList<Course> courseTaken) {
this.courseTaken = new ArrayList<Course>();
for(Course c : courseTaken) {
this.courseTaken.add(new Course(c.getCode(), c.getAssignment(), c.getCredit()));
}
}
/**
* This method gets the list of final grades for each course taken by this student.
*
* @return the list of final grades for each course taken by this student.
*/
public ArrayList<Double> getFinalGrade() {
ArrayList<Double> finGrade = new ArrayList<Double>();
for(Double d : finalGrade) {
finGrade.add(new Double(d));
}
return finalGrade;
}
/**
* This method sets the list of final grades to the given value.
*
* @param finalGrade is the list of final grades.
*/
public void setFinalGrade(ArrayList<Double> finalGrade) {
this.finalGrade = new ArrayList<Double>();
for(Double d : finalGrade) {
this.finalGrade.add(new Double(d));
}
}
/**
* This method overrides the hashCode method from class Object.
* This method returns a hash code value for the object.
* This method is supported for the benefit of hash tables
* such as those provided by HashMap.
*
* @return a hash code value for this object.
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((studentID == null) ? 0 : studentID.hashCode());
return result;
}
/**
* This method overrides the equal method from class Object.
* Indicates whether some other object is "equal to" this one.
*
* @param obj is the reference object with which to compare.
* @return true if this object is the same as the obj argument; false otherwise.
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (studentID == null) {
if (other.studentID != null)
return false;
} else if (!studentID.equals(other.studentID))
return false;
return true;
}
/**
* This method calculates the final grade of the student for each course
* taken and adds it to the list of final grades.
*
* @param gradeArray is an array of grades received by the student.
* @param weightArray is an array of weight of the courses taken by the student.
* @throws InvalidTotalException if the total weight
* of the assessments does not add up to 100, or of
* the total grade of a student is more than 100.
*/
public void addGrade(ArrayList<Double> gradeArray, ArrayList<Integer> weightArray) {
double finGrade = 0;
int totalWeight = 0;
try {
for (int i = 0; i < gradeArray.size(); i++) {
finGrade += (gradeArray.get(i) * weightArray.get(i)) / 100;
totalWeight += weightArray.get(i);
}
finalGrade.add((double) Math.round(finGrade * 10) / 10);
if (totalWeight != 100) {
throw new InvalidTotalException("Exception: The total weight does not add up to 100");
}
if(finGrade > 100) {
throw new InvalidTotalException("Exception: The total garde of the student is more than 100");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
/**
* This method calculates the GPA of the student.
*
* @return the calculated GPA of the student.
*/
public double weightedGPA() {
int gradePoint;
double attemptedCredit = 0;
double gpa = 0;
for (int i = 0; i < finalGrade.size(); i++) {
if (finalGrade.get(i) >= 90 && finalGrade.get(i) <= 100) {
gradePoint = 9;
} else if (finalGrade.get(i) >= 80 && finalGrade.get(i) < 90) {
gradePoint = 8;
} else if (finalGrade.get(i) >= 75 && finalGrade.get(i) < 80) {
gradePoint = 7;
} else if (finalGrade.get(i) >= 70 && finalGrade.get(i) < 75) {
gradePoint = 6;
} else if (finalGrade.get(i) >= 65 && finalGrade.get(i) < 70) {
gradePoint = 5;
} else if (finalGrade.get(i) >= 60 && finalGrade.get(i) < 65) {
gradePoint = 4;
} else if (finalGrade.get(i) >= 55 && finalGrade.get(i) < 60) {
gradePoint = 3;
} else if (finalGrade.get(i) >= 50 && finalGrade.get(i) < 55) {
gradePoint = 2;
} else if (finalGrade.get(i) >= 47 && finalGrade.get(i) < 50) {
gradePoint = 1;
} else {
gradePoint = 0;
}
attemptedCredit += courseTaken.get(i).getCredit();
gpa += gradePoint * courseTaken.get(i).getCredit();
}
return (double) Math.round(gpa / attemptedCredit * 10) / 10;
}
/**
* This method adds a course to the course taken by the students.
*
* @param course is the course to be added the list of course taken.
*/
public void addCourse(Course course) {
courseTaken.add(course);
}
} // end of Student
/**
* This class generates a course taken by a student.
*/
class Course {
/**
* The code of the course.
*/
private String code;
/**
* The name of the student.
*/
private ArrayList<Assessment> assignment;
/**
* The list of assignments for each course.
*/
private double credit;
/**
* This is the no argument constructor for the class Course.
*/
public Course() {
this("", null, 0.0);
}
/**
* This is the overloaded constructor for the class course.
* Initializes this course so that it has the specified
* code, assignment, credit.
*
*@param code is the code of the course.
*@param assignment is the list of assignments of the course.
*@param credit is the credit of the course.
*/
public Course(String code, ArrayList<Assessment> assignment, double credit) {
this.code = new String(code);
this.assignment = new ArrayList<Assessment>();
for(Assessment a : assignment) {
this.assignment.add(Assessment.getInstance(a.getType(), a.getWeight()));
}
this.credit = credit;
}
/**
* This is the copy constructor for the class Course.
* Initializes this student by copying another student. This
* course will have the same attributes as the other course.
*
* @param other is the course to copy.
*/
public Course(Course other) {
this(other.code, other.assignment, other.credit);
}
/**
* This method gets the code this of course.
*
* @return the code of this course.
*/
public String getCode() {
return code;
}
/**
* This method sets the code of the course to the given value.
*
* @param code is the code of the course.
*/
public void setCode(String code) {
this.code = code;
}
/**
* This method gets the list of assessment of this course.
*
* @return the list of assessment of this course.
*/
public ArrayList<Assessment> getAssignment() {
ArrayList<Assessment> assessList = new ArrayList<Assessment>();
for(Assessment a : this.assignment) {
assessList.add(Assessment.getInstance(a.getType(), a.getWeight()));
}
return assessList;
}
/**
* This method sets the list of assessment of the course to the given value.
*
* @param assignment is the list of assessment of the course.
*/
public void setAssignment(ArrayList<Assessment> assignment) {
this.assignment = new ArrayList<Assessment>();
for(Assessment a : assignment) {
this.assignment.add(Assessment.getInstance(a.getType(), a.getWeight()));
}
}
/**
* This method gets the credit of this course.
*
* @return the credit of this course.
*/
public double getCredit() {
return credit;
}
/**
* This method sets the credit of the course to the given value.
*
* @param credit is the credit of the course.
*/
public void setCredit(double credit) {
this.credit = credit;
}
/**
* This method overrides the hashCode method from class Object.
* This method returns a hash code value for the object.
* This method is supported for the benefit of hash tables
* such as those provided by HashMap.
*
* @return a hash code value for this object.
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((assignment == null) ? 0 : assignment.hashCode());
result = prime * result + ((code == null) ? 0 : code.hashCode());
long temp;
temp = Double.doubleToLongBits(credit);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
/**
* This method overrides the equal method from class Object.
* Indicates whether some other object is "equal to" this one.
*
* @param obj is the reference object with which to compare.
* @return true if this object is the same as the obj argument; false otherwise.
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Course other = (Course) obj;
if (assignment == null) {
if (other.assignment != null)
return false;
} else if (!assignment.equals(other.assignment))
return false;
if (code == null) {
if (other.code != null)
return false;
} else if (!code.equals(other.code))
return false;
if (Double.doubleToLongBits(credit) != Double.doubleToLongBits(other.credit))
return false;
return true;
}
} // end of COurse
/**
* This class generates an Assessment.
*
*
*/
class Assessment {
/**
* The type of the assessment.
*/
private char type;
/**
* The weight of the assessment.
*/
private int weight;
/**
* This is the no argument constructor for the class Assessment.
* Initializes this assignment to default values.
*
*/
private Assessment() {
this(' ', 0);
}
/**
* This is the overloaded constructor for the class Assessment.
* Initializes this assessment so that it has the specified
* type and weight.
*
* @param type is the type of the assessment.
* @param weight is the weight of the assessment.
*/
private Assessment(char type, int weight) {
this.type = type;
this.weight = weight;
}
/**
* This is the static factory method for the class Assessment
*
* @param type the type of the assessment.
* @param weight the type of the assessment.
* @return an assessment.
*/
public static Assessment getInstance(char type, int weight) {
Assessment assessment = new Assessment(type, weight);
return assessment;
}
/**
* This method gets the type of this assessment.
*
* @return the type of this assessment.
*/
public char getType() {
return type;
}
/**
* This method sets the type to the given value.
*
* @param type is the type of the assessment.
*/
public void setType(char type) {
this.type = type;
}
/**
* This method gets the weight of this assessment.
*
* @return the type of this assessment.
*/
public int getWeight() {
return weight;
}
/**
* This method sets the weight to the given value.
*
* @param weight is the weight of the assessment.
*/
public void setWeight(int weight) {
this.weight = weight;
}
/**
* This method overrides the hashCode method from class Object.
* This method returns a hash code value for the object.
* This method is supported for the benefit of hash tables
* such as those provided by HashMap.
*
* @return a hash code value for this object.
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + type;
result = prime * result + weight;
return result;
}
/**
* This method overrides the equal method from class Object.
* Indicates whether some other object is "equal to" this one.
*
* @param obj is the reference object with which to compare.
* @return true if this object is the same as the obj argument; false otherwise.
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Assessment other = (Assessment) obj;
if (type != other.type)
return false;
if (weight != other.weight)
return false;
return true;
}
} // end of Assessment
/**
* This class is a custom exception that extends RuntimeException.
*
*
*/
class InvalidTotalException extends Exception {
/**
* Initializes an exception with no detail message.
*/
public InvalidTotalException() {
super();
}
/**
* Initializes an exception with a detail message.
*
* @param message is the message to be printed with the exception.
*/
public InvalidTotalException(String message) {
super(message);
}
} // end of InvalidTotalException