Advertisement
ivana_andreevska

AV5 Student i Course

Aug 11th, 2022
865
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.27 KB | None | 0 0
  1. package AV5;
  2.  
  3. public class Student implements Comparable<Student>{
  4.  
  5.     private String lastName;
  6.     private String firstName;
  7.     private int exam1;
  8.     private int exam2;
  9.     private int exam3;
  10.     private char grade;
  11.  
  12.     public Student(String lastName, String firstName, int exam1 , int exam2 , int exam3) {
  13.         this.lastName = lastName;
  14.         this.firstName = firstName;
  15.         this.exam1 = exam1;
  16.         this.exam2 = exam2;
  17.         this.exam3 = exam3;
  18.         setGrade();
  19.     }
  20.  
  21.    public static Student createStudent(String line)
  22.    {
  23.        String[] parts=line.split("\\s+");
  24.        return new Student(parts[0],parts[1], Integer.parseInt(parts[2]) , Integer.parseInt(parts[3]) , Integer.parseInt(parts[4]));
  25.    }
  26.  
  27.     public char getGrade() {
  28.         return grade;
  29.     }
  30.  
  31.     public double totalPoints() {
  32.         return 0.25 * exam1 + 0.3 * exam2 + 0.45 * exam3;
  33.     }
  34.  
  35.     public void setGrade() {
  36.         double points = totalPoints();
  37.  
  38.         if (points >= 90) {
  39.             this.grade = 'A';
  40.         } else if (points >= 80) {
  41.             this.grade = 'B';
  42.         } else if (points >= 70) {
  43.             this.grade = 'C';
  44.         } else if (points >= 60) {
  45.             this.grade = 'D';
  46.         } else if (points >= 50) {
  47.             this.grade = 'E';
  48.         } else
  49.             this.grade = 'F';
  50.     }
  51.  
  52.     @Override
  53.     public String toString() {
  54.         return lastName + " " + firstName + " " + getGrade();
  55.     }
  56.  
  57.     public String printFullInfo() {
  58.         return String.format("%s %s %d %d %d %.2f %c", lastName, firstName, exam1, exam2, exam3, totalPoints(),grade);
  59.     }
  60.  
  61.     @Override
  62.     public int compareTo(Student other) {
  63.         return Character.compare(this.grade, other.grade);
  64.     }
  65. }
  66. package AV5;
  67.  
  68. import java.io.*;
  69. import java.util.ArrayList;
  70. import java.util.List;
  71. import java.util.stream.Collectors;
  72.  
  73. public class Course {
  74.     private List<Student> students;
  75.  
  76.     public Course() {
  77.         students = new ArrayList<>();
  78.     }
  79.  
  80.     public void readData(InputStream inputStream) {
  81.         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  82.  
  83.         this.students = bufferedReader.lines()
  84.                 .map(line -> Student.createStudent(line))
  85.                 .collect(Collectors.toList());
  86.     }
  87.  
  88.     public void printSortedData(OutputStream outputStream) {
  89.         PrintWriter printWriter = new PrintWriter(outputStream);
  90.  
  91.         this.students.stream().sorted().forEach(s -> printWriter.println(s));
  92.         printWriter.flush();
  93.     }
  94.  
  95.     public void printDetailsData(OutputStream outputStream) {
  96.         PrintWriter printWriter = new PrintWriter(outputStream);
  97.  
  98.         this.students.stream().sorted().forEach(s -> printWriter.println(s.printFullInfo()));
  99.         printWriter.flush();
  100.     }
  101.  
  102.     public void printDistributedData(OutputStream outputStream) {
  103.         PrintWriter printWriter = new PrintWriter(outputStream);
  104.         int[] gradeDistribution = new int[6];
  105.         for (Student s : students) {
  106.             gradeDistribution[s.getGrade() - 'A']++;
  107.  
  108.             for (int i = 0; i < 6; i++) {
  109.                 printWriter.printf("%c->%d\n", i + 'A', gradeDistribution[i]);
  110.                 printWriter.flush();
  111.             }
  112.         }
  113.     }
  114. }
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement