Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //package mk.ukim.finki.vtor_kolokvium;
- import java.util.*;
- import java.util.function.Function;
- import java.util.stream.Collectors;
- import java.util.stream.IntStream;
- import java.util.stream.Collectors;
- class OperationNotAllowedException extends Exception{
- public OperationNotAllowedException(String message) {
- super(message);
- }
- }
- class StudentThree extends Student{
- Map<Integer, List<Course>> coursesByTerm;
- boolean isGraduated;
- public StudentThree(String id) {
- super(id);
- coursesByTerm = new TreeMap<>();
- for(int i=1;i<7;i++)
- coursesByTerm.put(i, new ArrayList<>());
- this.isGraduated = false;
- }
- @Override
- public String getLog() {
- return String.format("Student with ID %s graduated with average grade %.2f in 3 years.",
- super.getId(),getAverageGrade());
- }
- public boolean isGraduated() {
- return isGraduated;
- }
- public int getNumberOfExamsPassed(){
- return coursesByTerm.values().stream().mapToInt(i->i.size()).sum();
- }
- @Override
- public double getAverageGrade() {
- return coursesByTerm.values().stream().flatMap(list->list.stream()).mapToDouble(i->i.getGrade())
- .average().orElse(5);
- }
- public String getDetailedReport(){
- StringBuilder stringBuilder= new StringBuilder();
- stringBuilder.append(String.format("Student: %s\n",super.getId()));
- for(int i=1;i<7;i++){
- stringBuilder.append(String.format("Term %d\n",i));
- stringBuilder.append(String.format("Courses: %d\n",coursesByTerm.get(i).size()));
- stringBuilder.append(String.format("Average grade for term: %.2f\n",
- coursesByTerm.get(i).stream().mapToDouble(j->j.getGrade()).average().orElse(5.0)));
- }
- stringBuilder.append(String.format("Average grade: %.2f\n",getAverageGrade()));
- stringBuilder.append(String.format("Courses attended: %s\n",
- coursesByTerm.values().stream().flatMap(list->list.stream()).map(j->j.getName())
- .sorted(Comparator.naturalOrder()).collect(Collectors.joining(","))));
- return stringBuilder.toString().trim();
- }
- @Override
- public void addGrade(int term, String courseName, int grade) throws OperationNotAllowedException {
- if(term>6)
- throw new OperationNotAllowedException(String.format
- ("Term %d is not possible for student with ID %s",term,super.getId()));
- if(coursesByTerm.containsKey(term)&&coursesByTerm.get(term).size()==3)
- throw new OperationNotAllowedException(String.format
- ("Student %s already has 3 grades in term %d",super.getId(),term));
- if(isGraduated){
- return ;
- }
- coursesByTerm.putIfAbsent(term,new ArrayList<>());
- coursesByTerm.get(term).add(new Course(courseName,grade));
- if(getNumberOfExamsPassed()==18)
- isGraduated = true;
- }
- @Override
- public String toString() {
- return String.format("Student: %s Courses passed: %d Average grade: %.2f",
- super.getId(),getNumberOfExamsPassed(),getAverageGrade());
- }
- @Override
- public int compareTo(Student o) {
- return Comparator.comparing(Student::getNumberOfExamsPassed,Comparator.reverseOrder())
- .thenComparing(Student::getAverageGrade,Comparator.reverseOrder())
- .thenComparing(Student::getId,Comparator.reverseOrder())
- .compare(this,o);
- }
- }
- class StudentFour extends Student{
- Map<Integer, List<Course>> coursesByTerm;
- boolean isGraduated;
- public StudentFour(String id) {
- super(id);
- coursesByTerm = new TreeMap<>();
- for(int i=1;i<9;i++)
- coursesByTerm.put(i, new ArrayList<>());
- isGraduated= false;
- }
- @Override
- public String getLog() {
- return String.format("Student with ID %s graduated with average grade %.2f in 4 years.",
- super.getId(),getAverageGrade());
- }
- @Override
- public boolean isGraduated() {
- return isGraduated;
- }
- public int getNumberOfExamsPassed(){
- return coursesByTerm.values().stream().mapToInt(i->i.size()).sum();
- }
- @Override
- public double getAverageGrade() {
- return coursesByTerm.values().stream().flatMap(list->list.stream()).mapToDouble(i->i.getGrade())
- .average().orElse(5);
- }
- @Override
- public void addGrade(int term, String courseName, int grade) throws OperationNotAllowedException {
- if(term>8)
- throw new OperationNotAllowedException(String.format
- ("Term %d is not possible for student with ID %s",term,super.getId()));
- if(coursesByTerm.containsKey(term)&&coursesByTerm.get(term).size()==3)
- throw new OperationNotAllowedException(String.format
- ("Student %s already has 3 grades in term %d",super.getId(),term));
- if(isGraduated){
- return ;
- }
- coursesByTerm.putIfAbsent(term,new ArrayList<>());
- coursesByTerm.get(term).add(new Course(courseName,grade));
- if(getNumberOfExamsPassed()==24)
- isGraduated = true;
- }
- @Override
- public int compareTo(Student o) {
- return Comparator.comparing(Student::getNumberOfExamsPassed,Comparator.reverseOrder())
- .thenComparing(Student::getAverageGrade,Comparator.reverseOrder())
- .thenComparing(Student::getId,Comparator.reverseOrder())
- .compare(this,o);
- }
- public String getDetailedReport(){
- StringBuilder stringBuilder= new StringBuilder();
- stringBuilder.append(String.format("Student: %s\n",super.getId()));
- for(int i=1;i<9;i++){
- stringBuilder.append(String.format("Term %d\n",i));
- stringBuilder.append(String.format("Courses: %d\n",coursesByTerm.get(i).size()));
- stringBuilder.append(String.format("Average grade for term: %.2f\n",
- coursesByTerm.get(i).stream().mapToDouble(j->j.getGrade()).average().orElse(5.0)));
- }
- stringBuilder.append(String.format("Average grade: %.2f\n",getAverageGrade()));
- stringBuilder.append(String.format("Courses attended: %s\n",
- coursesByTerm.values().stream().flatMap(list->list.stream()).map(j->j.getName())
- .sorted(Comparator.naturalOrder()).collect(Collectors.joining(","))));
- return stringBuilder.toString().trim();
- }
- @Override
- public String toString() {
- return String.format("Student: %s Courses passed: %d Average grade: %.2f",
- super.getId(),getNumberOfExamsPassed(),getAverageGrade());
- }
- }
- abstract class Student implements Comparable<Student> {
- private String id;
- public Student(String id) {
- this.id = id;
- }
- public String getId() {
- return id;
- }
- public abstract String getLog();
- public abstract boolean isGraduated();
- public abstract String getDetailedReport();
- public abstract int getNumberOfExamsPassed();
- public abstract double getAverageGrade();
- public abstract void addGrade(int term, String courseName, int grade) throws OperationNotAllowedException;
- }
- class Faculty {
- List<Student> logs;
- Map<String,Student> students;
- Map<String,List<Integer>> studentsOnCourses;
- public Faculty() {
- students= new HashMap<>();
- logs= new ArrayList<>();
- studentsOnCourses= new HashMap<>();
- }
- void addStudent(String id, int yearsOfStudies) {
- if(yearsOfStudies==3)
- students.putIfAbsent(id,(new StudentThree(id)));
- else{
- students.putIfAbsent(id,(new StudentFour(id)));
- }
- }
- void addGradeToStudent(String studentId, int term, String courseName, int grade) throws OperationNotAllowedException {
- students.get(studentId).addGrade(term,courseName,grade);
- studentsOnCourses.putIfAbsent(courseName,new ArrayList<>());
- studentsOnCourses.get(courseName).add(grade);
- }
- String getFacultyLogs() {
- logs = students.values().stream().filter(i->i.isGraduated()).collect(Collectors.toList());
- students= students.values().stream().filter(i->!i.isGraduated()).collect(Collectors.toMap(
- value->value.getId(),
- value->value
- ));
- return logs.stream().sorted(Comparator.comparing(Student::getId))
- .map(i-> i.getLog()).collect(Collectors.joining("\n"));
- }
- String getDetailedReportForStudent(String id) {
- return students.get(id).getDetailedReport();
- }
- void printFirstNStudents(int n) {
- students.values().stream().sorted().limit(n).forEach(System.out::println);
- }
- void printCourses() {
- studentsOnCourses.entrySet().stream().sorted(Comparator
- .comparing((Map.Entry<String, List<Integer>> entry) ->
- entry.getValue().size(),Comparator.naturalOrder())
- .thenComparing(entry ->
- entry.getValue().stream().mapToDouble(j->j)
- .average().orElse(5.0),Comparator.naturalOrder())
- .thenComparing(entry->entry.getKey()))
- .forEach(i-> System.out.println(String.format("%s %d %.2f",
- i.getKey(),i.getValue().size(),
- i.getValue().stream().mapToDouble(j->j).average().orElse(5.0))));
- }
- }
- class Course {
- String name;
- int grade;
- public Course(String name, int grade) {
- this.name = name;
- this.grade = grade;
- }
- public String getName() {
- return name;
- }
- public int getGrade() {
- return grade;
- }
- }
- public class FacultyTest {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int testCase = sc.nextInt();
- if (testCase == 1) {
- System.out.println("TESTING addStudent AND printFirstNStudents");
- Faculty faculty = new Faculty();
- for (int i = 0; i < 10; i++) {
- faculty.addStudent("student" + i, (i % 2 == 0) ? 3 : 4);
- }
- faculty.printFirstNStudents(10);
- } else if (testCase == 2) {
- System.out.println("TESTING addGrade and exception");
- Faculty faculty = new Faculty();
- faculty.addStudent("123", 3);
- faculty.addStudent("1234", 4);
- try {
- faculty.addGradeToStudent("123", 7, "NP", 10);
- } catch (OperationNotAllowedException e) {
- System.out.println(e.getMessage());
- }
- try {
- faculty.addGradeToStudent("1234", 9, "NP", 8);
- } catch (OperationNotAllowedException e) {
- System.out.println(e.getMessage());
- }
- } else if (testCase == 3) {
- System.out.println("TESTING addGrade and exception");
- Faculty faculty = new Faculty();
- faculty.addStudent("123", 3);
- faculty.addStudent("1234", 4);
- for (int i = 0; i < 4; i++) {
- try {
- faculty.addGradeToStudent("123", 1, "course" + i, 10);
- } catch (OperationNotAllowedException e) {
- System.out.println(e.getMessage());
- }
- }
- for (int i = 0; i < 4; i++) {
- try {
- faculty.addGradeToStudent("1234", 1, "course" + i, 10);
- } catch (OperationNotAllowedException e) {
- System.out.println(e.getMessage());
- }
- }
- } else if (testCase == 4) {
- System.out.println("Testing addGrade for graduation");
- Faculty faculty = new Faculty();
- faculty.addStudent("123", 3);
- faculty.addStudent("1234", 4);
- int counter = 1;
- for (int i = 1; i <= 6; i++) {
- for (int j = 1; j <= 3; j++) {
- try {
- faculty.addGradeToStudent("123", i, "course" + counter, (i % 2 == 0) ? 7 : 8);
- } catch (OperationNotAllowedException e) {
- System.out.println(e.getMessage());
- }
- ++counter;
- }
- }
- counter = 1;
- for (int i = 1; i <= 8; i++) {
- for (int j = 1; j <= 3; j++) {
- try {
- faculty.addGradeToStudent("1234", i, "course" + counter, (j % 2 == 0) ? 7 : 10);
- } catch (OperationNotAllowedException e) {
- System.out.println(e.getMessage());
- }
- ++counter;
- }
- }
- System.out.println("LOGS");
- System.out.println(faculty.getFacultyLogs());
- System.out.println("PRINT STUDENTS (there shouldn't be anything after this line!");
- faculty.printFirstNStudents(2);
- } else if (testCase == 5 || testCase == 6 || testCase == 7) {
- System.out.println("Testing addGrade and printFirstNStudents (not graduated student)");
- Faculty faculty = new Faculty();
- for (int i = 1; i <= 10; i++) {
- faculty.addStudent("student" + i, ((i % 2) == 1 ? 3 : 4));
- int courseCounter = 1;
- for (int j = 1; j < ((i % 2 == 1) ? 6 : 8); j++) {
- for (int k = 1; k <= ((j % 2 == 1) ? 3 : 2); k++) {
- try {
- faculty.addGradeToStudent("student" + i, j, ("course" + courseCounter), i % 5 + 6);
- } catch (OperationNotAllowedException e) {
- System.out.println(e.getMessage());
- }
- ++courseCounter;
- }
- }
- }
- if (testCase == 5)
- faculty.printFirstNStudents(10);
- else if (testCase == 6)
- faculty.printFirstNStudents(3);
- else
- faculty.printFirstNStudents(20);
- } else if (testCase == 8 || testCase == 9) {
- System.out.println("TESTING DETAILED REPORT");
- Faculty faculty = new Faculty();
- faculty.addStudent("student1", ((testCase == 8) ? 3 : 4));
- int grade = 6;
- int counterCounter = 1;
- for (int i = 1; i < ((testCase == 8) ? 6 : 8); i++) {
- for (int j = 1; j < 3; j++) {
- try {
- faculty.addGradeToStudent("student1", i, "course" + counterCounter, grade);
- } catch (OperationNotAllowedException e) {
- e.printStackTrace();
- }
- grade++;
- if (grade == 10)
- grade = 5;
- ++counterCounter;
- }
- }
- System.out.println(faculty.getDetailedReportForStudent("student1"));
- } else if (testCase==10) {
- System.out.println("TESTING PRINT COURSES");
- Faculty faculty = new Faculty();
- for (int i = 1; i <= 10; i++) {
- faculty.addStudent("student" + i, ((i % 2) == 1 ? 3 : 4));
- int courseCounter = 1;
- for (int j = 1; j < ((i % 2 == 1) ? 6 : 8); j++) {
- for (int k = 1; k <= ((j % 2 == 1) ? 3 : 2); k++) {
- int grade = sc.nextInt();
- try {
- faculty.addGradeToStudent("student" + i, j, ("course" + courseCounter), grade);
- } catch (OperationNotAllowedException e) {
- System.out.println(e.getMessage());
- }
- ++courseCounter;
- }
- }
- }
- faculty.printCourses();
- } else if (testCase==11) {
- System.out.println("INTEGRATION TEST");
- Faculty faculty = new Faculty();
- for (int i = 1; i <= 10; i++) {
- faculty.addStudent("student" + i, ((i % 2) == 1 ? 3 : 4));
- int courseCounter = 1;
- for (int j = 1; j <= ((i % 2 == 1) ? 6 : 8); j++) {
- for (int k = 1; k <= ((j % 2 == 1) ? 2 : 3); k++) {
- int grade = sc.nextInt();
- try {
- faculty.addGradeToStudent("student" + i, j, ("course" + courseCounter), grade);
- } catch (OperationNotAllowedException e) {
- System.out.println(e.getMessage());
- }
- ++courseCounter;
- }
- }
- }
- for (int i=11;i<15;i++) {
- faculty.addStudent("student" + i, ((i % 2) == 1 ? 3 : 4));
- int courseCounter = 1;
- for (int j = 1; j <= ((i % 2 == 1) ? 6 : 8); j++) {
- for (int k = 1; k <= 3; k++) {
- int grade = sc.nextInt();
- try {
- faculty.addGradeToStudent("student" + i, j, ("course" + courseCounter), grade);
- } catch (OperationNotAllowedException e) {
- System.out.println(e.getMessage());
- }
- ++courseCounter;
- }
- }
- }
- System.out.println("LOGS");
- System.out.println(faculty.getFacultyLogs());
- System.out.println("DETAILED REPORT FOR STUDENT");
- System.out.println(faculty.getDetailedReportForStudent("student2"));
- try {
- System.out.println(faculty.getDetailedReportForStudent("student11"));
- System.out.println("The graduated students should be deleted!!!");
- } catch (NullPointerException e) {
- System.out.println("The graduated students are really deleted");
- }
- System.out.println("FIRST N STUDENTS");
- faculty.printFirstNStudents(10);
- System.out.println("COURSES");
- faculty.printCourses();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement