Check this code so that The Result Processing System (RPS) is used to process the student results.
RPS will read in students and units result from two csv files: students.csv and units_result.csv.
students.csv
o This file stores the student’s information.
units_result.csv
o This file stores the student’s unit result.
(See appendix for sample students.csv and units_result.csv)
RPS will generate two csv files: result_releases.csv and unmatch_units_result.csv.
result_releases.csv
o This file contains the student id and grade for each student.
o If the student unit result is not found, it will be reported as either “No unit attempt” or “Research incomplete”.
unmatch_units_result.csv
o Some unit result in units_result.csv cannot be matched to any student id in students.csv. Such unmatched units will be stored in this file.
Also, when run rpstest, it should get an output of Sort by Student ID test passed
Result Release test passed
Unmatch units test passed. "“import java.util.;
import java.io.;

public class RPSTest {

private static ArrayList<ResultRelease> results;
private static ArrayList<Unit> unmatchUnits;
private static ResultProcessSystem RPS;

public static void main(String[] args) {
generateDataFiles();
deleteRPSGenerateFiles();

//execute ResultProcessSystem
RPS.run();

//read in result release data
readResultRelease();

//check if sort by student ID
try {
assert checkIfSortByStudentID() == true;
System.out.println(“Sort by Student ID test passed”);
}catch(AssertionError e) {
System.out.println(“Sort by Student ID test failed”);
}
//check if student result data is correct
try {
assert checkResultReleaseIsCorrect() == true;
System.out.println(“Result Release test passed”);
}catch(AssertionError e) {
System.out.println(“Result Release test failed”);
}

//check if unmatch unit is correct
readUnmatchUnits();

try {
assert checkUnmatchUnitIsCorrect() == true;
System.out.println(“Unmatch units test passed”);
}catch(AssertionError e) {
System.out.println(“Unmatch units test failed”);
}

}

private static boolean checkUnmatchUnitIsCorrect() {

//expected results
String u1 = “C444,CSIT511,55.0,45.0,48.0”;
String u2 = “R666,55.0,45.0”;

String[] uDatas = {u1,u2};

boolean result = true;
for(String s : uDatas) {
String[] data = s.split(”,“);
String id = data[0];
Unit unit = getUnmatchUnitByStudentID(id);
if(unit==null) {
result=false;
break;
}
if(!unit.getCSVString().equals(s)) {
result=false;
break;
}
}
return result;
}

private static Unit getUnmatchUnitByStudentID(String id) {
Unit toReturn = null;
for (Unit unit : unmatchUnits) {
if (unit != null && unit.equals(id)) { // Add a null check for unit before calling equals.
toReturn = unit;
break;
}
}
return toReturn;
}

private static void readUnmatchUnits() {
unmatchUnits = new ArrayList<Unit>();
try {
Scanner file = new Scanner(new File(RPS.getUnmatchUnitsResultFilename()));

while(file.hasNextLine()) {
String line = file.nextLine();
String[] data = line.split(”,“);
String id = data[0];

Unit unit = null;
if(id.startsWith(“C”)) {
String code = data[1];
double a1 = Double.parseDouble(data[2]);
double a2 = Double.parseDouble(data[3]);
double exam = Double.parseDouble(data[4]);
unit = new Unit_Coursework(id,code,a1,a2,exam);
}else if(id.startsWith(“R”)) {
double proposal = Double.parseDouble(data[1]);
double finalDissertation = Double.parseDouble(data[2]);
unit = new Unit_Research(id,proposal,finalDissertation);
}
unmatchUnits.add(unit);
}

}catch(FileNotFoundException ex) {
System.out.println(RPS.getUnmatchUnitsResultFilename()+” unable to open");
}
}

private static boolean checkResultReleaseIsCorrect() {
//expected results
String rr1 = “C111,HD”;
String rr2 = “C222,P”;
String rr3 = “C333,C”;
String rr4 = “C555,No unit attempt”;
String rr5 = “R111,F”;
String rr6 = “R222,P”;
String rr7 = “R333,P”;
String rr8 = “R444,Research incomplete”;

String[] rrDatas = {rr1,rr2,rr3,rr4,rr5,rr6,rr7,rr8};
boolean result = true;

for(String s : rrDatas) {
String[] data = s.split(“,”);
String id = data[0];
String r = data[1];
ResultRelease rr = getResultReleaseByStudentID(id);
if(rr==null || !rr.equals(id,r)) {
result = false;
break;
}
}
return result;
}


private static ResultRelease getResultReleaseByStudentID(String id) {
ResultRelease toReturn = null;
for(ResultRelease result: results) {
if(result.equals(id)) {
toReturn = result;
break;
}
}
return toReturn;
}

private static void readResultRelease() {
results = new ArrayList<ResultRelease>();
try {
Scanner file = new Scanner(new File(RPS.getResultsReleaseOutputFilename()));

while(file.hasNextLine()) {
String line = file.nextLine();
String[] data = line.split(“,”);
String studentID = data[0];
String result = data[1];
ResultRelease r = new ResultRelease(studentID,result);
results.add®;
}

}catch(FileNotFoundException ex) {
System.out.println(RPS.getResultsReleaseOutputFilename()+" unable to open");
}
}

private static boolean checkIfSortByStudentID() {
boolean sorted = true;
String[] data = {“C111”,“C222”,“C333”,“C555”,“R111”,“R222”,“R333”,“R444”};
if(results.size()==0) {
sorted = false;
}
for(int i=0;i<results.size();i++) {
if(!results.get(i).equals(data[i])) {
sorted = false;
break;
}
}
return sorted;
}


private static void deleteRPSGenerateFiles() {
File resultReleaseFile = new File(RPS.getResultsReleaseOutputFilename());
resultReleaseFile.delete();

File unmatchUnitsResult = new File(RPS.getUnmatchUnitsResultFilename());
unmatchUnitsResult.delete();
}

private static void generateDataFiles() {
//test data
String d1 = “C,Peter,Jordon,C222”;
String d2 = “C,John,Smith,C111”;
String d3 = “C,Bob,Church,C555”;
String d4 = “C,David,Jackson,C333”;
String d5 = “R,Gary,Kenny,R333”;
String d6 = “R,Eric,Trump,R222”;
String d7 = “R,Bob,Johnson,R111”;
String d8 = “R,Donald,Ray,R444”;

String[] sDatas = {d1,d2,d3,d4,d5,d6,d7,d8};

try {
PrintWriter pw = new PrintWriter(RPS.getStudentsDataFileName());
for(String s: sDatas) {
pw.println(s);
}
pw.close();
}catch(FileNotFoundException ex) {
System.out.println(RPS.getStudentsDataFileName()+" unable to open");
}

//test data
String r1 = “R222,85.0,72.0”;
String r2 = “C111,CSIT454,89.0,88.0,86.5”;
String r3 = “C333,CSIT511,75.5,72.0,58.5”;
String r4 = “R111,40.0,47.5”;
String r5 = “C222,CSIT454,76.0,56.5,65.0”;
String r6 = “R333,80.5,70.0”;
String r7 = “C444,CSIT511,55.0,45.0,48.0”;
String r8 = “R666,55.0,45.0”;
String[] rDatas = {r1,r2,r3,r4,r5,r6,r7,r8};

try {
PrintWriter pw = new PrintWriter(RPS.getUnitsResultDataFileName());
for(String s: rDatas) {
pw.println(s);
}
pw.close();
}catch(FileNotFoundException ex) {
System.out.println(RPS.getUnitsResultDataFileName()+" unable to open");
}
}
}
public class Student_Coursework extends Student {
private Unit_Coursework unit;

public Student_Coursework(String firstName, String lastName, String id) {
super(firstName, lastName, id, “C”);
}

public String getFinalGrade() {
if (unit == null) {
return “No unit attempt”;
} else {
return unit.getFinalGrade();
}
}

public void setUnit(Unit unit) {
this.unit = (Unit_Coursework) unit;
}

public String getCSVString() {
return this.getId() + “,” + this.getFinalGrade();
}
}
public class Student_Research extends Student {
private Unit_Research unit;

public Student_Research(String firstName, String lastName, String id) {
super(firstName, lastName, id, “R”);
}

public String getFinalGrade() {
if (unit == null) {
return “Research incomplete”;
} else {
return unit.getFinalGrade();
}
}

public void setUnit(Unit unit) {
this.unit = (Unit_Research) unit;
}

public String getCSVString() {
return this.getId() + “,” + this.getFinalGrade();
}
}
public class Unit_Coursework extends Unit {
private String unitCode;
private double a1;
private double a2;
private double exam;

public Unit_Coursework(String studentID, String unitCode, double a1, double a2, double exam) {
super(studentID, “CW”);
this.unitCode = unitCode;
this.a1 = a1;
this.a2 = a2;
this.exam = exam;
}

public double getOverallMarks() {
return a1 * 0.20 + a2 * 0.30 + exam * 0.50;
}

public String getFinalGrade() {
return Helper.computeCWGrade(getOverallMarks());
}

public String getCSVString() {
return studentID + “,” + unitCode + “,” + a1 + “,” + a2 + “,” + exam;
}
}
public class Unit_Research extends Unit{

private double proposal;
private double finalDissertation;

public Unit_Research(String studentID, double proposal, double finalDissertation) {
super(studentID,“R”);
this.proposal = proposal;
this.finalDissertation = finalDissertation;
}

public double getOverallMarks(){
//TODO: calculate research overall marks;
return 0;
}

public String getFinalGrade(){
return Helper.computeCWGrade(getOverallMarks());
}

public String getCSVString() {
//TODO: return a string in the following format
//studentID,proposal,finalDissertation
return “studentID,proposal,finalDissertation”;
}

}
abstract public class Student {

private String firstName;
private String lastName;
private String id;
private String enrolmentType;

public Student(String firstName, String lastName, String id,String enrolmentType) {
this.firstName = firstName;
this.lastName = lastName;
this.id = id;
this.enrolmentType = enrolmentType;
}

public String getEnrolmentType() {
return this.enrolmentType;
}

public String getFirstName() {
return this.firstName;
}

public String getLastName() {
return this.lastName;
}

public String getId() {
return this.id;
}

public boolean equals(String id){
return this.id.equals(id);
}

public boolean equals(Student student){
return this.id.equals(student.getId());
}

public boolean studentIDMoreThan(Student student){
int n = this.id.compareTo(student.getId());
if(n>0) {
return true;
}else {
return false;
}
}

public String toString() {
return this.id;
}

abstract public String getFinalGrade();

abstract public void setUnit(Unit unit);

abstract public String getCSVString();
}
abstract public class Unit {

private String unitType;
protected String studentID;

public Unit(String studentID, String unitType) {
this.studentID = studentID;
this.unitType = unitType;
}

public String getStudentID() {
return this.studentID;
}

public boolean equals(String id) {
return this.studentID.equals(id);
}

abstract double getOverallMarks();

abstract String getFinalGrade();

abstract String getCSVString();
}
public class ResultRelease {
private String studentID;
private String result;

public ResultRelease(String studentID, String result) {
this.studentID = studentID;
this.result = result;
}

public boolean equals(String studentID, String result) {
return this.studentID.equals(studentID) && this.result.equals(result);
}

public boolean equals(String studentID) {
return this.studentID.equals(studentID);
}

public String getStudentID() {
return studentID;
}

public boolean studentIDMoreThan(ResultRelease result) {
int n = this.studentID.compareTo(result.getStudentID());
if (n > 0) {
return true;
} else {
return false;
}
}
}
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class ResultProcessSystem {

//Constant for file name - do not change.
private final static String STUDENTS_DATA_FILE_NAME = “students.csv”;
private final static String UNITS_RESULT_DATA_FILE_NAME = “units_result.csv”;
private final static String RESULTS_RELEASE__OUTPUT_FILENAME = “results_release.csv”;
private final static String UNMATCH_UNITS_RESULT_FILENAME = “unmatch_units_result.csv”;

// One ArrayList to store both Coursework and Research Student and
// one ArrayList to store both Unit
// make use of Polymorphism!
private static ArrayList<Student> students;
private static ArrayList<Unit> units;

public static void run() {
students = new ArrayList<Student>();
units = new ArrayList<Unit>();
readInStudentsDataFromCSVFile();
readInStudentsUnitResultFromCSVFile();
matchUnitToStudent();
sortStudentsArrayListByStudentID();
printStudentArrayListToResultReleaseCSV();
printUnitsWithNoStudentsMatchToCSV();
}

private static void readInStudentsDataFromCSVFile() {

try {
Scanner file = new Scanner(new File(STUDENTS_DATA_FILE_NAME));
while (file.hasNextLine()) {
String line = file.nextLine();
String[] data = line.split(“,”);
Student student = null;
String id = data[2];
if (data[0].equals(“C”)) {
student = new Student_Coursework(data[1], data[2], id);
} else if (data[0].equals(“R”)) {
student = new Student_Research(data[1], data[2], id);
}
if (student != null) {
students.add(student);
}
}

} catch (FileNotFoundException ex) {
System.out.println(“Student data file not found”);
}
}

private static void readInStudentsUnitResultFromCSVFile() {
try {
Scanner file = new Scanner(new File(UNITS_RESULT_DATA_FILE_NAME));
while (file.hasNextLine()) {
String line = file.nextLine();
String[] data = line.split(“,”);
String id = data[0];

Unit unit = null;
if (id.startsWith(“C”)) {
String code = data[1];
double a1 = Double.parseDouble(data[2]);
double a2 = Double.parseDouble(data[3]);
double exam = Double.parseDouble(data[4]);
unit = new Unit_Coursework(id, code, a1, a2, exam);
} else if (id.startsWith(“R”)) {
double proposal = Double.parseDouble(data[1]);
double finalDissertation = Double.parseDouble(data[2]);
unit = new Unit_Research(id, proposal, finalDissertation);
}
units.add(unit);
}

} catch (FileNotFoundException ex) {
System.out.println(UNITS_RESULT_DATA_FILE_NAME + " file not found");
}
}

private static void matchUnitToStudent() {
Iterator<Unit> iterator = units.iterator();
while (iterator.hasNext()) {
Unit unit = iterator.next();
String studentID = unit.getStudentID();
for (Student student : students) {
if (student.equals(studentID)) {
student.setUnit(unit);
iterator.remove();
break;
}
}
}
}

private static void sortStudentsArrayListByStudentID() {
students.sort(Comparator.comparing(Student::getId));
}

private static void printStudentArrayListToResultReleaseCSV() {

try {
PrintWriter pw = new PrintWriter(RESULTS_RELEASE__OUTPUT_FILENAME);
for (Student student : students) {
pw.println(student.getCSVString());
}
pw.close();

} catch (FileNotFoundException ex) {
System.out.println("Unable to open " + RESULTS_RELEASE__OUTPUT_FILENAME);
}
}

private static void printUnitsWithNoStudentsMatchToCSV() {
try {
PrintWriter pw = new PrintWriter(UNMATCH_UNITS_RESULT_FILENAME);
for (Unit unit : units) {
pw.println(unit.getCSVString());
}
pw.close();

} catch (FileNotFoundException ex) {
System.out.println(“Unable to open " + UNMATCH_UNITS_RESULT_FILENAME);
}
}

public static String getStudentsDataFileName() {
return STUDENTS_DATA_FILE_NAME;
}

public static String getUnitsResultDataFileName() {
return UNITS_RESULT_DATA_FILE_NAME;
}

public static String getResultsReleaseOutputFilename() {
return RESULTS_RELEASE__OUTPUT_FILENAME;
}

public static String getUnmatchUnitsResultFilename() {
return UNMATCH_UNITS_RESULT_FILENAME;
}

public static void main(String[] args) {
run();
}
}
public class Helper {
public static String computeCWGrade(double score) {
String grade;

if (score >= 85) {
grade = “HD”;
} else if (score >= 75) {
grade = “D”;
} else if (score >= 65) {
grade = “C”;
} else if (score >= 50) {
grade = “P”;
} else {
grade = “F”;
}

return grade;
}

public static String computeRGrade(double score) {
String grade;

if (score >= 50) {
grade = “P”;
} else {
grade = “F”;
}

return grade;
}
}”