So the program I'm writing reads a 3 different text files. One text file contains names, the other two contains marks.
Now I have done everything correctly but there is one extra thing I want to add but I have no luck getting it right.
So right now the output file looks like this:
25987 Alan
IR101: 35.6 IR102: 20.7 Aggregate: 28.2
Class: Fail Outcome: Repeat Year!
-------------------------------------------------------
25954 Betty
IR101: 70.2 IR102: 63.4 Aggregate: 66.8
Class: 2.1 Outcome: Proceed to Stage 2!
-------------------------------------------------------
25654 Chris
IR101: 58.6 IR102: 35.1 Aggregate: 46.9
Class: Fail Outcome: Resit IR102!
-------------------------------------------------------
Etc
So my program prints out the order in terms of Names from the text file with names. So for example one of the text files that has all the names in it the order is: Alan /n Betty /n Chris
Now I don't want the order to be in names from text file I want the order to be descending aggregate marks. So the order should be:
25954 Betty
IR101: 70.2 IR102: 63.4 Aggregate: 66.8
Class: 2.1 Outcome: Proceed to Stage 2!
-------------------------------------------------------
25654 Chris
IR101: 58.6 IR102: 35.1 Aggregate: 46.9
Class: Fail Outcome: Resit IR102!
-------------------------------------------------------
25987 Alan
IR101: 35.6 IR102: 20.7 Aggregate: 28.2
Class: Fail Outcome: Repeat Year!
-------------------------------------------------------
I've tried many different solutions for so long but they all fail.
Code for program:
public class SORTING {
static class Student {
String id;
String name;
List<Double> marks;
public Student(String id, String name) {
this.id = id;
this.name = name;
marks = new ArrayList<Double>();
}
public void addMark(Double d) {
marks.add(d);
}
public void writeToPW(PrintWriter out) {
out.println(id + " " + name);
double d = 0;
for (int i = 0; i < marks.size(); i++) {
out.printf("IR10%d: %.1f ", (i+1), marks.get(i));
d += marks.get(i);
}
out.printf("Aggregate: %.1f ", + d / marks.size());
out.println("\n");
double aggregate = (d/marks.size());
if ((marks.get(0)<40)&&(marks.get(1)>=40)){
out.print("Class: Fail" + " Outcome: Resit IR101");
}
if ((marks.get(0)>=40)&&(marks.get(1)<40)){
out.print("Class: Fail" + " Outcome: Resit IR102!");
}
if ((marks.get(0)<40)&&(marks.get(1)<40)){
out.print("Class: Fail" + " Outcome: Repeat Year!");
}
if((marks.get(0)>40)&&(marks.get(1)>40)&&(aggregate>70)){
out.print("Class: 1st" + " Outcome: Proceed to Stage 2!");
}
if((marks.get(0)>40)&&(marks.get(1)>40)&&(aggregate>=60)&&(aggregate<=69.9)){
out.print("Class: 2.1" + " Outcome: Proceed to Stage 2!");
}
//2.2 Class degree code.
if((marks.get(0)>40)&&(marks.get(1)>40)&&(aggregate>=50)&&(aggregate<=59.9)){
out.print("Class: 2.2" + " Outcome: Proceed to Stage 2!");
}
if((marks.get(0)>40)&&(marks.get(1)>40)&&(aggregate>=40)&&(aggregate<=49.9)){
out.print("Class: 3rd" + " Outcome: Proceed to Stage 2!");
}
out.println("\n");
out.println("-------------------------------------------------------");
}
}
public static void main(String[] args) throws IOException {
//declare reader and writer
BufferedReader reader = null;
PrintWriter writer = null;
//hash maps to store the data
HashMap<String, Student> students = new HashMap<String, Student>();
// list to maintain the original order
List<Student> orderedStudents = new ArrayList<Student>();
//read the first file and store the data
reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("IRStudents.txt"))));
String line;
String[] arg;
while ((line = reader.readLine()) != null) {
if (!line.startsWith("-")) {
arg = line.split(" ");
Student student = new Student(arg[0], arg[1]);
students.put(arg[0], student);
orderedStudents.add(student);
}
}
reader.close();
//read the second file, merge the data and output the data to the out file
reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("IR101.txt"))));
while((line = reader.readLine()) != null){
arg = line.split(" ");
students.get(arg[0]).addMark(Double.parseDouble(arg[1]));
}
reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File("IR102.txt"))));
while((line = reader.readLine()) != null){
arg = line.split(" ");
students.get(arg[0]).addMark(Double.parseDouble(arg[1]));
}
// Now we can do writing.
writer = new PrintWriter(new FileOutputStream(new File("RankedList.txt")));
for (Student s: orderedStudents) {
s.writeToPW(writer);
}
writer.close();
}
}