Advertisement
dzocesrce

[NP] Graduation

Apr 28th, 2025
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 18.53 KB | None | 0 0
  1. //package mk.ukim.finki.vtor_kolokvium;
  2.  
  3. import java.util.*;
  4. import java.util.function.Function;
  5. import java.util.stream.Collectors;
  6. import java.util.stream.IntStream;
  7. import java.util.stream.Collectors;
  8. class OperationNotAllowedException extends Exception{
  9.     public OperationNotAllowedException(String message) {
  10.         super(message);
  11.     }
  12. }
  13. class StudentThree extends Student{
  14.     Map<Integer, List<Course>> coursesByTerm;
  15.     boolean isGraduated;
  16.     public StudentThree(String id) {
  17.         super(id);
  18.         coursesByTerm = new TreeMap<>();
  19.         for(int i=1;i<7;i++)
  20.             coursesByTerm.put(i, new ArrayList<>());
  21.         this.isGraduated = false;
  22.     }
  23.  
  24.     @Override
  25.     public String getLog() {
  26.         return String.format("Student with ID %s graduated with average grade %.2f in 3 years.",
  27.                 super.getId(),getAverageGrade());
  28.     }
  29.  
  30.     public boolean isGraduated() {
  31.         return isGraduated;
  32.     }
  33.  
  34.     public int getNumberOfExamsPassed(){
  35.         return coursesByTerm.values().stream().mapToInt(i->i.size()).sum();
  36.     }
  37.  
  38.     @Override
  39.     public double getAverageGrade() {
  40.         return coursesByTerm.values().stream().flatMap(list->list.stream()).mapToDouble(i->i.getGrade())
  41.                 .average().orElse(5);
  42.     }
  43.  
  44.     public String getDetailedReport(){
  45.         StringBuilder stringBuilder= new StringBuilder();
  46.         stringBuilder.append(String.format("Student: %s\n",super.getId()));
  47.         for(int i=1;i<7;i++){
  48.             stringBuilder.append(String.format("Term %d\n",i));
  49.             stringBuilder.append(String.format("Courses: %d\n",coursesByTerm.get(i).size()));
  50.             stringBuilder.append(String.format("Average grade for term: %.2f\n",
  51.                     coursesByTerm.get(i).stream().mapToDouble(j->j.getGrade()).average().orElse(5.0)));
  52.         }
  53.         stringBuilder.append(String.format("Average grade: %.2f\n",getAverageGrade()));
  54.         stringBuilder.append(String.format("Courses attended: %s\n",
  55.                 coursesByTerm.values().stream().flatMap(list->list.stream()).map(j->j.getName())
  56.                         .sorted(Comparator.naturalOrder()).collect(Collectors.joining(","))));
  57.         return stringBuilder.toString().trim();
  58.     }
  59.  
  60.     @Override
  61.     public void addGrade(int term, String courseName, int grade) throws OperationNotAllowedException {
  62.         if(term>6)
  63.             throw new OperationNotAllowedException(String.format
  64.                     ("Term %d is not possible for student with ID %s",term,super.getId()));
  65.         if(coursesByTerm.containsKey(term)&&coursesByTerm.get(term).size()==3)
  66.             throw new OperationNotAllowedException(String.format
  67.                     ("Student %s already has 3 grades in term %d",super.getId(),term));
  68.         if(isGraduated){
  69.             return ;
  70.         }
  71.  
  72.         coursesByTerm.putIfAbsent(term,new ArrayList<>());
  73.         coursesByTerm.get(term).add(new Course(courseName,grade));
  74.         if(getNumberOfExamsPassed()==18)
  75.             isGraduated = true;
  76.  
  77.     }
  78.  
  79.     @Override
  80.     public String toString() {
  81.         return String.format("Student: %s Courses passed: %d Average grade: %.2f",
  82.                 super.getId(),getNumberOfExamsPassed(),getAverageGrade());
  83.     }
  84.  
  85.     @Override
  86.     public int compareTo(Student o) {
  87.         return Comparator.comparing(Student::getNumberOfExamsPassed,Comparator.reverseOrder())
  88.                 .thenComparing(Student::getAverageGrade,Comparator.reverseOrder())
  89.                 .thenComparing(Student::getId,Comparator.reverseOrder())
  90.                 .compare(this,o);
  91.     }
  92. }
  93.  
  94. class StudentFour extends Student{
  95.     Map<Integer, List<Course>> coursesByTerm;
  96.     boolean isGraduated;
  97.     public StudentFour(String id) {
  98.         super(id);
  99.         coursesByTerm = new TreeMap<>();
  100.         for(int i=1;i<9;i++)
  101.             coursesByTerm.put(i, new ArrayList<>());
  102.         isGraduated= false;
  103.     }
  104.  
  105.     @Override
  106.     public String getLog() {
  107.         return String.format("Student with ID %s graduated with average grade %.2f in 4 years.",
  108.                 super.getId(),getAverageGrade());
  109.     }
  110.  
  111.     @Override
  112.     public boolean isGraduated() {
  113.         return isGraduated;
  114.     }
  115.  
  116.  
  117.     public int getNumberOfExamsPassed(){
  118.         return coursesByTerm.values().stream().mapToInt(i->i.size()).sum();
  119.     }
  120.  
  121.     @Override
  122.     public double getAverageGrade() {
  123.         return coursesByTerm.values().stream().flatMap(list->list.stream()).mapToDouble(i->i.getGrade())
  124.                 .average().orElse(5);
  125.     }
  126.  
  127.     @Override
  128.     public void addGrade(int term, String courseName, int grade) throws OperationNotAllowedException {
  129.         if(term>8)
  130.             throw new OperationNotAllowedException(String.format
  131.                     ("Term %d is not possible for student with ID %s",term,super.getId()));
  132.         if(coursesByTerm.containsKey(term)&&coursesByTerm.get(term).size()==3)
  133.             throw new OperationNotAllowedException(String.format
  134.                     ("Student %s already has 3 grades in term %d",super.getId(),term));
  135.         if(isGraduated){
  136.             return ;
  137.         }
  138.  
  139.         coursesByTerm.putIfAbsent(term,new ArrayList<>());
  140.         coursesByTerm.get(term).add(new Course(courseName,grade));
  141.         if(getNumberOfExamsPassed()==24)
  142.             isGraduated = true;
  143.     }
  144.  
  145.     @Override
  146.     public int compareTo(Student o) {
  147.         return Comparator.comparing(Student::getNumberOfExamsPassed,Comparator.reverseOrder())
  148.                 .thenComparing(Student::getAverageGrade,Comparator.reverseOrder())
  149.                 .thenComparing(Student::getId,Comparator.reverseOrder())
  150.                 .compare(this,o);
  151.     }
  152.  
  153.  
  154.     public String getDetailedReport(){
  155.         StringBuilder stringBuilder= new StringBuilder();
  156.         stringBuilder.append(String.format("Student: %s\n",super.getId()));
  157.         for(int i=1;i<9;i++){
  158.             stringBuilder.append(String.format("Term %d\n",i));
  159.             stringBuilder.append(String.format("Courses: %d\n",coursesByTerm.get(i).size()));
  160.             stringBuilder.append(String.format("Average grade for term: %.2f\n",
  161.                     coursesByTerm.get(i).stream().mapToDouble(j->j.getGrade()).average().orElse(5.0)));
  162.         }
  163.         stringBuilder.append(String.format("Average grade: %.2f\n",getAverageGrade()));
  164.         stringBuilder.append(String.format("Courses attended: %s\n",
  165.                 coursesByTerm.values().stream().flatMap(list->list.stream()).map(j->j.getName())
  166.                         .sorted(Comparator.naturalOrder()).collect(Collectors.joining(","))));
  167.         return stringBuilder.toString().trim();
  168.     }
  169.  
  170.     @Override
  171.     public String toString() {
  172.         return String.format("Student: %s Courses passed: %d Average grade: %.2f",
  173.                 super.getId(),getNumberOfExamsPassed(),getAverageGrade());
  174.     }
  175. }
  176.  
  177. abstract class Student implements Comparable<Student> {
  178.     private String id;
  179.  
  180.     public Student(String id) {
  181.         this.id = id;
  182.     }
  183.  
  184.     public String getId() {
  185.         return id;
  186.     }
  187.  
  188.     public abstract String getLog();
  189.  
  190.     public abstract boolean isGraduated();
  191.  
  192.     public abstract String getDetailedReport();
  193.  
  194.     public abstract int getNumberOfExamsPassed();
  195.  
  196.     public abstract double getAverageGrade();
  197.  
  198.     public abstract void addGrade(int term, String courseName, int grade) throws OperationNotAllowedException;
  199. }
  200. class Faculty {
  201.     List<Student> logs;
  202.     Map<String,Student> students;
  203.     Map<String,List<Integer>> studentsOnCourses;
  204.     public Faculty() {
  205.     students= new HashMap<>();
  206.     logs= new ArrayList<>();
  207.     studentsOnCourses= new HashMap<>();
  208.     }
  209.  
  210.     void addStudent(String id, int yearsOfStudies) {
  211.         if(yearsOfStudies==3)
  212.             students.putIfAbsent(id,(new StudentThree(id)));
  213.         else{
  214.             students.putIfAbsent(id,(new StudentFour(id)));
  215.         }
  216.     }
  217.  
  218.     void addGradeToStudent(String studentId, int term, String courseName, int grade) throws OperationNotAllowedException {
  219.         students.get(studentId).addGrade(term,courseName,grade);
  220.         studentsOnCourses.putIfAbsent(courseName,new ArrayList<>());
  221.         studentsOnCourses.get(courseName).add(grade);
  222.  
  223.     }
  224.  
  225.  
  226.     String getFacultyLogs() {
  227.  
  228.         logs = students.values().stream().filter(i->i.isGraduated()).collect(Collectors.toList());
  229.         students= students.values().stream().filter(i->!i.isGraduated()).collect(Collectors.toMap(
  230.                 value->value.getId(),
  231.                 value->value
  232.         ));
  233.         return logs.stream().sorted(Comparator.comparing(Student::getId))
  234.                 .map(i-> i.getLog()).collect(Collectors.joining("\n"));
  235.     }
  236.  
  237.     String getDetailedReportForStudent(String id) {
  238.         return students.get(id).getDetailedReport();
  239.  
  240.     }
  241.  
  242.     void printFirstNStudents(int n) {
  243.         students.values().stream().sorted().limit(n).forEach(System.out::println);
  244.  
  245.     }
  246.     void printCourses() {
  247.         studentsOnCourses.entrySet().stream().sorted(Comparator
  248.                         .comparing((Map.Entry<String, List<Integer>> entry) ->
  249.                                         entry.getValue().size(),Comparator.naturalOrder())
  250.                         .thenComparing(entry ->
  251.                                 entry.getValue().stream().mapToDouble(j->j)
  252.                                         .average().orElse(5.0),Comparator.naturalOrder())
  253.                         .thenComparing(entry->entry.getKey()))
  254.  
  255.                 .forEach(i-> System.out.println(String.format("%s %d %.2f",
  256.                         i.getKey(),i.getValue().size(),
  257.                         i.getValue().stream().mapToDouble(j->j).average().orElse(5.0))));
  258.  
  259.     }
  260. }
  261.  
  262.  
  263. class Course {
  264.     String name;
  265.     int grade;
  266.  
  267.     public Course(String name, int grade) {
  268.         this.name = name;
  269.         this.grade = grade;
  270.     }
  271.  
  272.     public String getName() {
  273.         return name;
  274.     }
  275.  
  276.     public int getGrade() {
  277.         return grade;
  278.     }
  279. }
  280.  
  281.  
  282. public class FacultyTest {
  283.  
  284.     public static void main(String[] args) {
  285.         Scanner sc = new Scanner(System.in);
  286.         int testCase = sc.nextInt();
  287.  
  288.         if (testCase == 1) {
  289.             System.out.println("TESTING addStudent AND printFirstNStudents");
  290.             Faculty faculty = new Faculty();
  291.             for (int i = 0; i < 10; i++) {
  292.                 faculty.addStudent("student" + i, (i % 2 == 0) ? 3 : 4);
  293.             }
  294.             faculty.printFirstNStudents(10);
  295.  
  296.         } else if (testCase == 2) {
  297.             System.out.println("TESTING addGrade and exception");
  298.             Faculty faculty = new Faculty();
  299.             faculty.addStudent("123", 3);
  300.             faculty.addStudent("1234", 4);
  301.             try {
  302.                 faculty.addGradeToStudent("123", 7, "NP", 10);
  303.             } catch (OperationNotAllowedException e) {
  304.                 System.out.println(e.getMessage());
  305.             }
  306.             try {
  307.                 faculty.addGradeToStudent("1234", 9, "NP", 8);
  308.             } catch (OperationNotAllowedException e) {
  309.                 System.out.println(e.getMessage());
  310.             }
  311.         } else if (testCase == 3) {
  312.             System.out.println("TESTING addGrade and exception");
  313.             Faculty faculty = new Faculty();
  314.             faculty.addStudent("123", 3);
  315.             faculty.addStudent("1234", 4);
  316.             for (int i = 0; i < 4; i++) {
  317.                 try {
  318.                     faculty.addGradeToStudent("123", 1, "course" + i, 10);
  319.                 } catch (OperationNotAllowedException e) {
  320.                     System.out.println(e.getMessage());
  321.                 }
  322.             }
  323.             for (int i = 0; i < 4; i++) {
  324.                 try {
  325.                     faculty.addGradeToStudent("1234", 1, "course" + i, 10);
  326.                 } catch (OperationNotAllowedException e) {
  327.                     System.out.println(e.getMessage());
  328.                 }
  329.             }
  330.         } else if (testCase == 4) {
  331.             System.out.println("Testing addGrade for graduation");
  332.             Faculty faculty = new Faculty();
  333.             faculty.addStudent("123", 3);
  334.             faculty.addStudent("1234", 4);
  335.             int counter = 1;
  336.             for (int i = 1; i <= 6; i++) {
  337.                 for (int j = 1; j <= 3; j++) {
  338.                     try {
  339.                         faculty.addGradeToStudent("123", i, "course" + counter, (i % 2 == 0) ? 7 : 8);
  340.                     } catch (OperationNotAllowedException e) {
  341.                         System.out.println(e.getMessage());
  342.                     }
  343.                     ++counter;
  344.                 }
  345.             }
  346.             counter = 1;
  347.             for (int i = 1; i <= 8; i++) {
  348.                 for (int j = 1; j <= 3; j++) {
  349.                     try {
  350.                         faculty.addGradeToStudent("1234", i, "course" + counter, (j % 2 == 0) ? 7 : 10);
  351.                     } catch (OperationNotAllowedException e) {
  352.                         System.out.println(e.getMessage());
  353.                     }
  354.                     ++counter;
  355.                 }
  356.             }
  357.             System.out.println("LOGS");
  358.             System.out.println(faculty.getFacultyLogs());
  359.             System.out.println("PRINT STUDENTS (there shouldn't be anything after this line!");
  360.             faculty.printFirstNStudents(2);
  361.         } else if (testCase == 5 || testCase == 6 || testCase == 7) {
  362.             System.out.println("Testing addGrade and printFirstNStudents (not graduated student)");
  363.             Faculty faculty = new Faculty();
  364.             for (int i = 1; i <= 10; i++) {
  365.                 faculty.addStudent("student" + i, ((i % 2) == 1 ? 3 : 4));
  366.                 int courseCounter = 1;
  367.                 for (int j = 1; j < ((i % 2 == 1) ? 6 : 8); j++) {
  368.                     for (int k = 1; k <= ((j % 2 == 1) ? 3 : 2); k++) {
  369.                         try {
  370.                             faculty.addGradeToStudent("student" + i, j, ("course" + courseCounter), i % 5 + 6);
  371.                         } catch (OperationNotAllowedException e) {
  372.                             System.out.println(e.getMessage());
  373.                         }
  374.                         ++courseCounter;
  375.                     }
  376.                 }
  377.             }
  378.             if (testCase == 5)
  379.                 faculty.printFirstNStudents(10);
  380.             else if (testCase == 6)
  381.                 faculty.printFirstNStudents(3);
  382.             else
  383.                 faculty.printFirstNStudents(20);
  384.         } else if (testCase == 8 || testCase == 9) {
  385.             System.out.println("TESTING DETAILED REPORT");
  386.             Faculty faculty = new Faculty();
  387.             faculty.addStudent("student1", ((testCase == 8) ? 3 : 4));
  388.             int grade = 6;
  389.             int counterCounter = 1;
  390.             for (int i = 1; i < ((testCase == 8) ? 6 : 8); i++) {
  391.                 for (int j = 1; j < 3; j++) {
  392.                     try {
  393.                         faculty.addGradeToStudent("student1", i, "course" + counterCounter, grade);
  394.                     } catch (OperationNotAllowedException e) {
  395.                         e.printStackTrace();
  396.                     }
  397.                     grade++;
  398.                     if (grade == 10)
  399.                         grade = 5;
  400.                     ++counterCounter;
  401.                 }
  402.             }
  403.             System.out.println(faculty.getDetailedReportForStudent("student1"));
  404.         } else if (testCase==10) {
  405.             System.out.println("TESTING PRINT COURSES");
  406.             Faculty faculty = new Faculty();
  407.             for (int i = 1; i <= 10; i++) {
  408.                 faculty.addStudent("student" + i, ((i % 2) == 1 ? 3 : 4));
  409.                 int courseCounter = 1;
  410.                 for (int j = 1; j < ((i % 2 == 1) ? 6 : 8); j++) {
  411.                     for (int k = 1; k <= ((j % 2 == 1) ? 3 : 2); k++) {
  412.                         int grade = sc.nextInt();
  413.                         try {
  414.                             faculty.addGradeToStudent("student" + i, j, ("course" + courseCounter), grade);
  415.                         } catch (OperationNotAllowedException e) {
  416.                             System.out.println(e.getMessage());
  417.                         }
  418.                         ++courseCounter;
  419.                     }
  420.                 }
  421.             }
  422.             faculty.printCourses();
  423.         } else if (testCase==11) {
  424.             System.out.println("INTEGRATION TEST");
  425.             Faculty faculty = new Faculty();
  426.             for (int i = 1; i <= 10; i++) {
  427.                 faculty.addStudent("student" + i, ((i % 2) == 1 ? 3 : 4));
  428.                 int courseCounter = 1;
  429.                 for (int j = 1; j <= ((i % 2 == 1) ? 6 : 8); j++) {
  430.                     for (int k = 1; k <= ((j % 2 == 1) ? 2 : 3); k++) {
  431.                         int grade = sc.nextInt();
  432.                         try {
  433.                             faculty.addGradeToStudent("student" + i, j, ("course" + courseCounter), grade);
  434.                         } catch (OperationNotAllowedException e) {
  435.                             System.out.println(e.getMessage());
  436.                         }
  437.                         ++courseCounter;
  438.                     }
  439.                 }
  440.  
  441.             }
  442.  
  443.             for (int i=11;i<15;i++) {
  444.                 faculty.addStudent("student" + i, ((i % 2) == 1 ? 3 : 4));
  445.                 int courseCounter = 1;
  446.                 for (int j = 1; j <= ((i % 2 == 1) ? 6 : 8); j++) {
  447.                     for (int k = 1; k <= 3; k++) {
  448.                         int grade = sc.nextInt();
  449.                         try {
  450.                             faculty.addGradeToStudent("student" + i, j, ("course" + courseCounter), grade);
  451.                         } catch (OperationNotAllowedException e) {
  452.                             System.out.println(e.getMessage());
  453.                         }
  454.                         ++courseCounter;
  455.                     }
  456.                 }
  457.             }
  458.             System.out.println("LOGS");
  459.             System.out.println(faculty.getFacultyLogs());
  460.             System.out.println("DETAILED REPORT FOR STUDENT");
  461.             System.out.println(faculty.getDetailedReportForStudent("student2"));
  462.             try {
  463.                 System.out.println(faculty.getDetailedReportForStudent("student11"));
  464.                 System.out.println("The graduated students should be deleted!!!");
  465.             } catch (NullPointerException e) {
  466.                 System.out.println("The graduated students are really deleted");
  467.             }
  468.             System.out.println("FIRST N STUDENTS");
  469.             faculty.printFirstNStudents(10);
  470.             System.out.println("COURSES");
  471.             faculty.printCourses();
  472.         }
  473.     }
  474. }
  475.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement