Advertisement
svetlozar_kirkov

Exam Score

Jan 26th, 2015
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1. import java.math.BigDecimal;
  2. import java.math.RoundingMode;
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.Comparator;
  6. import java.util.Scanner;
  7.  
  8. public class Problem14_ExamScore {
  9.    
  10.     public static class Student{
  11.         private String name;
  12.         private int examScore;
  13.         private BigDecimal grade;
  14.        
  15.         public Student(String name, int examScore, BigDecimal grade){
  16.             this.name=name;
  17.             this.examScore=examScore;
  18.             this.grade=grade;
  19.         }
  20.         public String getName(){
  21.             return name;
  22.         }
  23.         public int getScore(){
  24.             return examScore;
  25.         }
  26.         public BigDecimal getGrade(){
  27.             return grade;
  28.         }
  29.  
  30.     }
  31.  
  32.     public static void main(String[] args) {
  33.         Scanner input = new Scanner(System.in);
  34.         ArrayList<String> inputs = new ArrayList<String>();
  35.         for (int i = 0; i < 3; i++){
  36.             input.nextLine();
  37.         }
  38.         ArrayList<Student> students = new ArrayList<Student>();
  39.         while (true) {
  40.             String[] tempInput = input.nextLine().split("\\s*\\|\\s*");
  41.             if (tempInput.length<4){
  42.                 break;
  43.             }
  44.             else{
  45.                 Student temp = new Student(tempInput[1],Integer.parseInt(tempInput[2]),new BigDecimal(tempInput[3]));
  46.                 students.add(temp);
  47.             }
  48.         }
  49.         Collections.sort(students, new Comparator<Student>() {
  50.                public int compare(Student b1, Student b2) {
  51.                   if (b1.getScore()<b2.getScore()){
  52.                       return -1;
  53.                   }
  54.                   else if (b1.getScore()>b2.getScore()){
  55.                       return 1;
  56.                   }
  57.                   else{
  58.                       return 0;
  59.                   }
  60.                }
  61.             });
  62.         ArrayList<Integer> distinctScores = new ArrayList<Integer>();
  63.         for (int i = 0; i < students.size(); i++){
  64.             if (!distinctScores.contains(students.get(i).getScore())){
  65.                 distinctScores.add(students.get(i).getScore());
  66.             }
  67.         }
  68.         for (int i = 0; i < distinctScores.size(); i++){
  69.             System.out.print(distinctScores.get(i)+" -> [");
  70.             BigDecimal total = new BigDecimal("0");
  71.             BigDecimal count = new BigDecimal("0");
  72.             int stCount = 0;
  73.             ArrayList<String> names = new ArrayList<String>();
  74.             for (int j = 0; j < students.size(); j++){
  75.                 if (students.get(j).getScore()==distinctScores.get(i)){
  76.                     count = count.add(new BigDecimal("1"));
  77.                     stCount++;
  78.                     total = total.add(students.get(j).getGrade());
  79.                     names.add(students.get(j).getName());
  80.                 }
  81.             }
  82.             Collections.sort(names);
  83.             for (int x = 0; x < names.size(); x++){
  84.                 System.out.print(names.get(x));
  85.                 if (stCount > 1){
  86.                     System.out.print(", ");
  87.                     stCount--;
  88.                 }
  89.                 else {
  90.                     //System.out.print(students.get(j).getName());
  91.                     System.out.printf("]; avg=%.2f\n",(total.divide(count,2,RoundingMode.HALF_UP)));
  92.                 }
  93.             }
  94.            
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement