Advertisement
STANAANDREY

takeoff student manager

Mar 9th, 2023 (edited)
962
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.49 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.math.RoundingMode;
  4. import java.text.DecimalFormat;
  5. import java.util.*;
  6.  
  7. class UtilMath {
  8.     static public double roundDown(double nr, int prec) {
  9.         final String format = "." + "#".repeat(prec);
  10.         var decimalFormat = new DecimalFormat(format);
  11.         decimalFormat.setRoundingMode(RoundingMode.DOWN);
  12.         return Double.parseDouble(decimalFormat.format(nr));
  13.     }
  14. }
  15.  
  16. class Student implements Comparable<Student> {
  17.     private int id;
  18.     private String name;
  19.     private List<Double> grades;
  20.  
  21.     public ArrayList<Double> getGrades() {
  22.         return (ArrayList<Double>) grades;
  23.     }
  24.  
  25.     public Student(int id, String name) {
  26.         this.id = id;
  27.         this.name = name;
  28.         grades = new ArrayList<>();
  29.     }
  30.  
  31.     public void addGrade(double grade) {
  32.         grades.add(grade);
  33.     }
  34.     public double getGPA() {
  35.         double[] primDoubles = grades.stream().mapToDouble(d -> d).toArray();
  36.         double mean = Arrays.stream(primDoubles).average().orElse(Double.NaN);
  37.         return UtilMath.roundDown(mean, 1);
  38.     }
  39.  
  40.     public String getName() {
  41.         return name;
  42.     }
  43.  
  44.     public int getId() {
  45.         return id;
  46.     }
  47.  
  48.  
  49.  
  50.     @Override
  51.     public String toString() {
  52.         return "Student{" +
  53.                 "id=" + id +
  54.                 ", name='" + name + '\'' +
  55.                 ", grades=" + grades +
  56.                 '}';
  57.     }
  58.  
  59.     @Override
  60.     public int compareTo(Student other) {
  61.         return name.compareTo(other.name);
  62.     }
  63.     static class GpaComp implements Comparator<Student> {
  64.         @Override
  65.         public int compare(Student student1, Student student2) {
  66.             if (student1.getGPA() > student2.getGPA()) {
  67.                 return -1;
  68.             }
  69.             return 1;
  70.         }
  71.     }
  72. }
  73.  
  74.  
  75. class University implements Comparable<University> {
  76.     final static int SCHOOLARSHIP_NR = 2;
  77.  
  78.     private String name;
  79.     private List<Student> students;
  80.  
  81.     public String getName() {
  82.         return name;
  83.     }
  84.  
  85.     public University(String name) {
  86.         this.name = name;
  87.         students = new ArrayList<>();
  88.     }
  89.     void addStud(final Student student) {
  90.         students.add(student);
  91.     }
  92.     public double getGPA() {
  93.         double mean = 0;
  94.         int nr = 0;
  95.         for (var student : students) {
  96.             for (double grade : student.getGrades()) {
  97.                 mean += grade;
  98.             }
  99.             nr += student.getGrades().size();
  100.         }
  101.         mean /= 1.0 * nr;
  102.         return UtilMath.roundDown(mean, 1);
  103.     }
  104.  
  105.     public List<Student> getStudents() {
  106.         return students;
  107.     }
  108.  
  109.     public Student findStudById(int id) {
  110.         for (var student : students) {
  111.             if (student.getId() == id) {
  112.                 return student;
  113.             }
  114.         }
  115.         return null;
  116.     }
  117.  
  118.     @Override
  119.     public String toString() {
  120.         return "University{" +
  121.                 "name='" + name + '\'' +
  122.                 ", students=" + students +
  123.                 '}';
  124.     }
  125.  
  126.     public void sortStudents() {
  127.         Collections.sort(students);
  128.     }
  129.  
  130.     @Override
  131.     public int compareTo(University other) {
  132.         if (getGPA() > other.getGPA()) {
  133.             return -1;
  134.         }
  135.         return 1;
  136.     }
  137.  
  138.     List<String> findStudentsNameWithSchoolarship() {
  139.  
  140.         Set<Student> set = new TreeSet<>(new Student.GpaComp());
  141.         for (var student : students) {
  142.             set.add(student);
  143.         }
  144.         List<String> ans = new ArrayList<>();
  145.         Iterator<Student> iterator = set.iterator();
  146.         while (iterator.hasNext()) {
  147.             if (ans.size() == SCHOOLARSHIP_NR) {
  148.                 break;
  149.             }
  150.             ans.add(iterator.next().getName());
  151.         }
  152.         return ans;
  153.     }
  154.     public double getTOpGPA() {
  155.         double ans = Double.NEGATIVE_INFINITY;
  156.         for (var student : students) {
  157.             ans = Math.max(ans, student.getGPA());
  158.         }
  159.         return ans;
  160.     }
  161. }
  162.  
  163. class UniversitiesManager {
  164.     private static UniversitiesManager instance = null;
  165.     List<University> universities;
  166.     private UniversitiesManager() {
  167.         universities = new ArrayList<>();
  168.     }
  169.     public static synchronized UniversitiesManager getInstance() {
  170.         if (instance == null) {
  171.             instance = new UniversitiesManager();
  172.         }
  173.         return instance;
  174.     }
  175.  
  176.     private void readStudents(Scanner scanner, List<University> universities) {
  177.         int n = scanner.nextInt();
  178.         for (int i = 0; i < n; i++) {
  179.             int studId = scanner.nextInt();;
  180.             String name = scanner.next();
  181.             String uniName = scanner.next();
  182.             int idx = -1;
  183.             for (int j = 0; j < universities.size() && idx == -1; j++) {
  184.                 if (universities.get(j).getName().equals(uniName)) {
  185.                     idx = j;
  186.                 }
  187.             }
  188.             var student = new Student(studId, name);
  189.             if (idx != -1) {
  190.                 universities.get(idx).addStud(student);
  191.             } else {
  192.                 var university = new University(uniName);
  193.                 university.addStud(student);
  194.                 universities.add(university);
  195.             }
  196.         }
  197.     }
  198.     private void readGrades(Scanner scanner, List<University> universities) {
  199.         int m = scanner.nextInt();
  200.         for (int i = 0; i < m; i++) {
  201.             double grade = scanner.nextDouble();
  202.             int studId = scanner.nextInt();
  203.             for (var university : universities) {
  204.                 var student = university.findStudById(studId);
  205.                 if (student != null) {
  206.                     student.addGrade(grade);
  207.                     break;
  208.                 }
  209.             }
  210.         }
  211.     }
  212.     public void readData(final String FILE_PATH) throws FileNotFoundException {
  213.         var file = new File(FILE_PATH);
  214.         universities = new ArrayList<>();
  215.         var scanner = new Scanner(file);
  216.         readStudents(scanner, universities);
  217.         readGrades(scanner, universities);
  218.         scanner.close();
  219.     }
  220.  
  221.     public void task1() {
  222.         System.out.println("TASK1: ");
  223.  
  224.         for (var university : universities) {
  225.             university.sortStudents();
  226.         }
  227.         Collections.sort(universities);
  228.  
  229.         for (var university : universities) {
  230.             System.out.print(university.getName() + ": ");
  231.             for (var student : university.getStudents()) {
  232.                 System.out.print(student.getName() + " ");
  233.             }
  234.             System.out.println();
  235.         }
  236.         System.out.println();
  237.     }
  238.  
  239.     public void task2() {
  240.         System.out.println("TASK2: ");
  241.         for (var university : universities) {
  242.             String schoolarshipsStr = String.join(" ", university.findStudentsNameWithSchoolarship());
  243.             System.out.println(university.getName() + ": " + schoolarshipsStr);
  244.         }
  245.         System.out.println();
  246.     }
  247.  
  248.     public void task3() {
  249.         System.out.println("TASK3: ");
  250.  
  251.         List<Student> students = new ArrayList<>();
  252.         for (var university : universities) {
  253.             students.addAll(university.getStudents());
  254.             university.getStudents().clear();
  255.         }
  256.  
  257.         Collections.sort(students, new Student.GpaComp());
  258.         for (int i = universities.size() - 1, j = 0; i >= 0; i--, j++) {
  259.             universities.get(i).addStud(students.get(j));
  260.         }
  261.         for (int i = students.size() - 1; i >= universities.size(); i--) {
  262.             universities.get(0).addStud(students.get(i));
  263.         }
  264.  
  265.         for (var university : universities) {
  266.             System.out.print(university.getName() + ": ");
  267.             for (var student : university.getStudents()) {
  268.                 System.out.print(student.getName() + " ");
  269.             }
  270.             System.out.println();
  271.         }
  272.         //if you want to check the difference uncomment the next line
  273.         //System.out.println(universities.get(universities.size() - 1).getTOpGPA() - universities.get(0).getTOpGPA());
  274.     }
  275. }
  276.  
  277. public class Main{
  278.     final static String FILE_PATH = "data.txt";
  279.     public static void main(String args[]) throws FileNotFoundException {
  280.         var universitiesManager = UniversitiesManager.getInstance();
  281.         universitiesManager.readData(FILE_PATH);
  282.         universitiesManager.task1();
  283.         universitiesManager.task2();
  284.         universitiesManager.task3();
  285.     }
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement