Advertisement
StJimmy

StudentFacultySystem

Jan 21st, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.32 KB | None | 0 0
  1. import java.time.LocalDateTime;
  2. import java.util.*;
  3. import java.util.function.Function;
  4. import java.util.stream.Collectors;
  5.  
  6. public class Main {
  7.  
  8.     public static void main(String[] args) {
  9.  
  10.         Scanner scanner = new Scanner(System.in);
  11.         Students students = new Students();
  12.         int requiredAge = Integer.parseInt(scanner.nextLine());
  13.  
  14.         //System.out.println(requiredAge);
  15.         while (scanner.hasNextLine()) {
  16.  
  17.             String[] line = scanner.nextLine().split(" ");
  18.             if (line[0].isEmpty()) {
  19.                 break;
  20.             }
  21.             try {
  22.                 students.addStudent(line[0], Integer.parseInt(line[1]), Integer.parseInt(line[2]), requiredAge,
  23.                         LocalDateTime.of(2016, 12, 29, 0, 0, 0).minusDays(Integer.parseInt(line[3])));
  24.             } catch (StudentRegistrationError studentRegistrationError) {
  25.                 System.out.println(studentRegistrationError.getMessage());
  26.             }
  27.         }
  28.  
  29.         System.out.println("====== REGISTERED STUDENTS ======");
  30.         students.registeredStudents().forEach(System.out::println);
  31.         System.out.println("====== STUDENTS REGISTRATION DATE ======");
  32.         while (scanner.hasNextLine()) {
  33.             String line = scanner.nextLine();
  34.             if (line.isEmpty()) {
  35.                 break;
  36.             }
  37.  
  38.             students.studentRegistrationDate(Integer.parseInt(line))
  39.                     .entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey)).forEach(l -> {
  40.                 System.out.println(l.getKey());
  41.                 l.getValue()
  42.                         .stream()
  43.                         .sorted()
  44.                         .forEach(student -> System.out.println("--- " + student));
  45.             });
  46.  
  47.         }
  48.  
  49.         System.out.println("====== NOT REGISTERED ======");
  50.  
  51.         students.notRegisteredStudents();
  52.  
  53.         System.out.println("====== ADD FACULTY ======");
  54.  
  55.         while (scanner.hasNextLine()) {
  56.             String[] line = scanner.nextLine().split(" ");
  57.  
  58.             if (line[0].isEmpty()) {
  59.                 break;
  60.             }
  61.             try {
  62.                 students.addFaculty(line[0], Integer.parseInt(line[1]), line[2]);
  63.             } catch (FacultyAlreadyAdded facultyAlreadyAdded) {
  64.                 System.out.println(facultyAlreadyAdded.getMessage());
  65.             }
  66.         }
  67.  
  68.         System.out.println("====== FACULTY LIST ======");
  69.         students.getFacultyListSorted()
  70.                 .forEach(System.out::println);
  71.  
  72.         System.out.println("====== STUDENTS IN FACULTY ======");
  73.  
  74.         students.studentFaculty().entrySet()
  75.         .stream().sorted(Comparator.comparing(Map.Entry::getKey))
  76.         .forEach(l ->{
  77.             System.out.println(l.getKey());
  78.             l.getValue()
  79.                     .stream()
  80.                     .sorted()
  81.                     .forEach(val -> System.out.println("---- " + val.getName() + " " + val.getAge()));
  82.         });
  83.  
  84.     }
  85. }
  86.  
  87.  
  88. class Students {
  89.  
  90.     private List<Student> studentList;
  91.     private List<Student> notRegisteredStudentsList;
  92.     private List<Faculty> facultyList;
  93.  
  94.     Students() {
  95.  
  96.         studentList = new ArrayList<>();
  97.         notRegisteredStudentsList = new ArrayList<>();
  98.         facultyList = new ArrayList<>();
  99.     }
  100.  
  101.     void addStudent(String name, int age, int faksId, int requiredAge, LocalDateTime localDateTime) throws StudentRegistrationError {
  102.  
  103.         if (age < requiredAge) {
  104.             notRegisteredStudentsList.add(new Student(name, age, 0, null));
  105.             throw new StudentRegistrationError("Can't register student " + name);
  106.         } else {
  107.             studentList.add(new Student(name, age, faksId, localDateTime));
  108.         }
  109.     }
  110.  
  111.     /**
  112.      * avoid duplicates using only List !!
  113.      */
  114.     void addFaculty(String ime, int id, String city) throws FacultyAlreadyAdded {
  115.  
  116.         if (facultyList.stream().anyMatch(faculty -> faculty.getName().equals(ime) && faculty.getId() == id && faculty.getCity().equals(city))) {
  117.             throw new FacultyAlreadyAdded(ime + " already added");
  118.         }
  119.         facultyList.add(new Faculty(ime, id, city));
  120.     }
  121.  
  122.     void notRegisteredStudents() {
  123.  
  124.         notRegisteredStudentsList
  125.                 .stream()
  126.                 .forEach(System.out::println);
  127.     }
  128.  
  129.     List<Student> registeredStudents() {
  130.  
  131.         return studentList
  132.                 .stream()
  133.                 .sorted()
  134.                 .collect(Collectors.toList());
  135.     }
  136.  
  137.     Map<String, List<Student>> studentRegistrationDate(int age) {
  138.  
  139.         return studentList
  140.                 .stream()
  141.                 .filter(student -> student.getAge() > age)
  142.                 .collect(Collectors.groupingBy(student ->
  143.                                 student.getLocalDateTime().getYear() + " " + student.getLocalDateTime().getMonth() + " " + student.getLocalDateTime().getDayOfMonth(),
  144.                         Collectors.toList()));
  145.     }
  146.  
  147.     List<Faculty> getFacultyListSorted() {
  148.  
  149.         return facultyList
  150.                 .stream()
  151.                 .sorted()
  152.                 .collect(Collectors.toList());
  153.     }
  154.  
  155.     Map<Faculty, List<Student>> studentFaculty() {
  156.  
  157.         return facultyList
  158.                 .stream()
  159.                 .collect(Collectors.toMap(Function.identity(), f -> studentList.stream().filter(student -> student.getFaksId() == f.getId()).collect(Collectors.toList())));
  160.     }
  161.  
  162.  
  163. }
  164.  
  165. class Student implements Comparable<Student> {
  166.  
  167.     private String name;
  168.     private int age;
  169.     private int faksId;
  170.     private LocalDateTime localDateTime;
  171.  
  172.  
  173.     public Student(String name, int age, int faksId, LocalDateTime localDateTime) {
  174.         this.name = name;
  175.         this.age = age;
  176.         this.faksId = faksId;
  177.         this.localDateTime = localDateTime;
  178.     }
  179.  
  180.  
  181.     public int getFaksId() {
  182.         return faksId;
  183.     }
  184.  
  185.     public String getName() {
  186.         return name;
  187.     }
  188.  
  189.     public int getAge() {
  190.         return age;
  191.     }
  192.  
  193.     public LocalDateTime getLocalDateTime() {
  194.         return localDateTime;
  195.     }
  196.  
  197.     @Override
  198.     public String toString() {
  199.         switch (faksId) {
  200.             case 0:
  201.                 return String.format("%-10s%5d", name, age);
  202.             default:
  203.                 return String.format("%-10s%5d%5d", name, age, faksId);
  204.         }
  205.     }
  206.  
  207.     @Override
  208.     public int compareTo(Student student) {
  209.  
  210.         return Comparator.comparing(Student::getName)
  211.                 .thenComparingInt(Student::getAge)
  212.                 .compare(this, student);
  213.     }
  214. }
  215.  
  216. class Faculty implements Comparable<Faculty> {
  217.  
  218.     private String name;
  219.     private int id;
  220.     private String city;
  221.  
  222.     public Faculty(String name, int id, String city) {
  223.         this.name = name;
  224.         this.id = id;
  225.         this.city = city;
  226.     }
  227.  
  228.     public String getName() {
  229.         return name;
  230.     }
  231.  
  232.     public int getId() {
  233.         return id;
  234.     }
  235.  
  236.     public String getCity() {
  237.         return city;
  238.     }
  239.  
  240.     @Override
  241.     public boolean equals(Object o) {
  242.         if (o == null) {
  243.             return false;
  244.         }
  245.         if (o.getClass() != getClass()) {
  246.             return false;
  247.         }
  248.  
  249.         Faculty faculty = (Faculty) o;
  250.  
  251.         return name.equals(faculty.name) && id == faculty.id && city.equals(faculty.city);
  252.     }
  253.  
  254.     @Override
  255.     public int hashCode() {
  256.         int result = 17;
  257.         result = 31 * result + name.hashCode();
  258.         result = 31 * result + Integer.hashCode(id);
  259.         result = 31 * result + city.hashCode();
  260.  
  261.         return result;
  262.     }
  263.  
  264.     @Override
  265.     public int compareTo(Faculty faculty) {
  266.         return Integer.compare(id,faculty.getId());
  267.     }
  268.  
  269.     @Override
  270.     public String toString() {
  271.         return String.format("%-10s%5d\t%5s", name, id, city);
  272.     }
  273. }
  274.  
  275. class StudentRegistrationError extends Exception {
  276.     private String message;
  277.  
  278.     public StudentRegistrationError(String message) {
  279.         this.message = message;
  280.     }
  281.  
  282.     @Override
  283.     public String getMessage() {
  284.         return message;
  285.     }
  286. }
  287.  
  288. class FacultyAlreadyAdded extends Exception {
  289.     private String message;
  290.  
  291.     public FacultyAlreadyAdded(String message) {
  292.         this.message = message;
  293.     }
  294.  
  295.     @Override
  296.     public String getMessage() {
  297.         return message;
  298.     }
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement