Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.time.LocalDateTime;
- import java.util.*;
- import java.util.function.Function;
- import java.util.stream.Collectors;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Students students = new Students();
- int requiredAge = Integer.parseInt(scanner.nextLine());
- //System.out.println(requiredAge);
- while (scanner.hasNextLine()) {
- String[] line = scanner.nextLine().split(" ");
- if (line[0].isEmpty()) {
- break;
- }
- try {
- students.addStudent(line[0], Integer.parseInt(line[1]), Integer.parseInt(line[2]), requiredAge,
- LocalDateTime.of(2016, 12, 29, 0, 0, 0).minusDays(Integer.parseInt(line[3])));
- } catch (StudentRegistrationError studentRegistrationError) {
- System.out.println(studentRegistrationError.getMessage());
- }
- }
- System.out.println("====== REGISTERED STUDENTS ======");
- students.registeredStudents().forEach(System.out::println);
- System.out.println("====== STUDENTS REGISTRATION DATE ======");
- while (scanner.hasNextLine()) {
- String line = scanner.nextLine();
- if (line.isEmpty()) {
- break;
- }
- students.studentRegistrationDate(Integer.parseInt(line))
- .entrySet().stream().sorted(Comparator.comparing(Map.Entry::getKey)).forEach(l -> {
- System.out.println(l.getKey());
- l.getValue()
- .stream()
- .sorted()
- .forEach(student -> System.out.println("--- " + student));
- });
- }
- System.out.println("====== NOT REGISTERED ======");
- students.notRegisteredStudents();
- System.out.println("====== ADD FACULTY ======");
- while (scanner.hasNextLine()) {
- String[] line = scanner.nextLine().split(" ");
- if (line[0].isEmpty()) {
- break;
- }
- try {
- students.addFaculty(line[0], Integer.parseInt(line[1]), line[2]);
- } catch (FacultyAlreadyAdded facultyAlreadyAdded) {
- System.out.println(facultyAlreadyAdded.getMessage());
- }
- }
- System.out.println("====== FACULTY LIST ======");
- students.getFacultyListSorted()
- .forEach(System.out::println);
- System.out.println("====== STUDENTS IN FACULTY ======");
- students.studentFaculty().entrySet()
- .stream().sorted(Comparator.comparing(Map.Entry::getKey))
- .forEach(l ->{
- System.out.println(l.getKey());
- l.getValue()
- .stream()
- .sorted()
- .forEach(val -> System.out.println("---- " + val.getName() + " " + val.getAge()));
- });
- }
- }
- class Students {
- private List<Student> studentList;
- private List<Student> notRegisteredStudentsList;
- private List<Faculty> facultyList;
- Students() {
- studentList = new ArrayList<>();
- notRegisteredStudentsList = new ArrayList<>();
- facultyList = new ArrayList<>();
- }
- void addStudent(String name, int age, int faksId, int requiredAge, LocalDateTime localDateTime) throws StudentRegistrationError {
- if (age < requiredAge) {
- notRegisteredStudentsList.add(new Student(name, age, 0, null));
- throw new StudentRegistrationError("Can't register student " + name);
- } else {
- studentList.add(new Student(name, age, faksId, localDateTime));
- }
- }
- /**
- * avoid duplicates using only List !!
- */
- void addFaculty(String ime, int id, String city) throws FacultyAlreadyAdded {
- if (facultyList.stream().anyMatch(faculty -> faculty.getName().equals(ime) && faculty.getId() == id && faculty.getCity().equals(city))) {
- throw new FacultyAlreadyAdded(ime + " already added");
- }
- facultyList.add(new Faculty(ime, id, city));
- }
- void notRegisteredStudents() {
- notRegisteredStudentsList
- .stream()
- .forEach(System.out::println);
- }
- List<Student> registeredStudents() {
- return studentList
- .stream()
- .sorted()
- .collect(Collectors.toList());
- }
- Map<String, List<Student>> studentRegistrationDate(int age) {
- return studentList
- .stream()
- .filter(student -> student.getAge() > age)
- .collect(Collectors.groupingBy(student ->
- student.getLocalDateTime().getYear() + " " + student.getLocalDateTime().getMonth() + " " + student.getLocalDateTime().getDayOfMonth(),
- Collectors.toList()));
- }
- List<Faculty> getFacultyListSorted() {
- return facultyList
- .stream()
- .sorted()
- .collect(Collectors.toList());
- }
- Map<Faculty, List<Student>> studentFaculty() {
- return facultyList
- .stream()
- .collect(Collectors.toMap(Function.identity(), f -> studentList.stream().filter(student -> student.getFaksId() == f.getId()).collect(Collectors.toList())));
- }
- }
- class Student implements Comparable<Student> {
- private String name;
- private int age;
- private int faksId;
- private LocalDateTime localDateTime;
- public Student(String name, int age, int faksId, LocalDateTime localDateTime) {
- this.name = name;
- this.age = age;
- this.faksId = faksId;
- this.localDateTime = localDateTime;
- }
- public int getFaksId() {
- return faksId;
- }
- public String getName() {
- return name;
- }
- public int getAge() {
- return age;
- }
- public LocalDateTime getLocalDateTime() {
- return localDateTime;
- }
- @Override
- public String toString() {
- switch (faksId) {
- case 0:
- return String.format("%-10s%5d", name, age);
- default:
- return String.format("%-10s%5d%5d", name, age, faksId);
- }
- }
- @Override
- public int compareTo(Student student) {
- return Comparator.comparing(Student::getName)
- .thenComparingInt(Student::getAge)
- .compare(this, student);
- }
- }
- class Faculty implements Comparable<Faculty> {
- private String name;
- private int id;
- private String city;
- public Faculty(String name, int id, String city) {
- this.name = name;
- this.id = id;
- this.city = city;
- }
- public String getName() {
- return name;
- }
- public int getId() {
- return id;
- }
- public String getCity() {
- return city;
- }
- @Override
- public boolean equals(Object o) {
- if (o == null) {
- return false;
- }
- if (o.getClass() != getClass()) {
- return false;
- }
- Faculty faculty = (Faculty) o;
- return name.equals(faculty.name) && id == faculty.id && city.equals(faculty.city);
- }
- @Override
- public int hashCode() {
- int result = 17;
- result = 31 * result + name.hashCode();
- result = 31 * result + Integer.hashCode(id);
- result = 31 * result + city.hashCode();
- return result;
- }
- @Override
- public int compareTo(Faculty faculty) {
- return Integer.compare(id,faculty.getId());
- }
- @Override
- public String toString() {
- return String.format("%-10s%5d\t%5s", name, id, city);
- }
- }
- class StudentRegistrationError extends Exception {
- private String message;
- public StudentRegistrationError(String message) {
- this.message = message;
- }
- @Override
- public String getMessage() {
- return message;
- }
- }
- class FacultyAlreadyAdded extends Exception {
- private String message;
- public FacultyAlreadyAdded(String message) {
- this.message = message;
- }
- @Override
- public String getMessage() {
- return message;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement