From 7a7de0c6524d7cf3c5821b535a6bf4828e96199a Mon Sep 17 00:00:00 2001 From: Ekarry <81385639+Ekarry@users.noreply.github.com> Date: Tue, 15 Jun 2021 21:49:54 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D1=88=D1=83=20=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=D0=B5=D1=80=D0=B8=D1=82=D1=8C=20=D0=94=D0=979?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/HWLesson_9/Course.java | 38 +++++++++++++++++++ src/main/java/HWLesson_9/Student.java | 54 +++++++++++++++++++++++++++ src/main/java/HWLesson_9/Study.java | 46 +++++++++++++++++++++++ 3 files changed, 138 insertions(+) create mode 100644 src/main/java/HWLesson_9/Course.java create mode 100644 src/main/java/HWLesson_9/Student.java create mode 100644 src/main/java/HWLesson_9/Study.java diff --git a/src/main/java/HWLesson_9/Course.java b/src/main/java/HWLesson_9/Course.java new file mode 100644 index 0000000..fb3f781 --- /dev/null +++ b/src/main/java/HWLesson_9/Course.java @@ -0,0 +1,38 @@ +package HWLesson_9; + +import java.text.MessageFormat; +import java.util.Objects; + +public class Course { + private String name; + + public Course(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return MessageFormat.format("{0}", name); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Course course = (Course) o; + return Objects.equals(name, course.name); + } + + @Override + public int hashCode() { + return Objects.hash(name); + } +} diff --git a/src/main/java/HWLesson_9/Student.java b/src/main/java/HWLesson_9/Student.java new file mode 100644 index 0000000..b05e575 --- /dev/null +++ b/src/main/java/HWLesson_9/Student.java @@ -0,0 +1,54 @@ +package HWLesson_9; + +import java.util.Collection; +import java.util.List; +import java.util.stream.Collectors; + +public class Student { + private String name; + private List courses; + + public Student(String name, List courses) { + this.name = name; + this.courses = courses; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getCourses() { + return courses; + } + + public void setCourses(List courses) { + this.courses = courses; + } + + public static List getUniqueCourses(List students){ + return students.stream() + .map(Student::getCourses) + .flatMap(Collection::stream) + .distinct() + .collect(Collectors.toList()); + } + + public static List getListOfThirstyForKnowledgeStudent(List students, int quantity){ + return students.stream() + .sorted((s1, s2) -> s2.getCourses().size() - s1.getCourses().size()) + .limit(quantity) + .map(Student::getName) + .collect(Collectors.toList()); + } + + public static List getListOfStudentVisitingCourse(List students, Course course){ + return students.stream() + .filter((s) -> s.getCourses().contains(course)) + .map(Student::getName) + .collect(Collectors.toList()); + } +} diff --git a/src/main/java/HWLesson_9/Study.java b/src/main/java/HWLesson_9/Study.java new file mode 100644 index 0000000..1cb268c --- /dev/null +++ b/src/main/java/HWLesson_9/Study.java @@ -0,0 +1,46 @@ +package HWLesson_9; + +import java.util.Arrays; +import java.util.List; +import static HWLesson_9.Student.getListOfStudentVisitingCourse; +import static HWLesson_9.Student.getListOfThirstyForKnowledgeStudent; +import static HWLesson_9.Student.getUniqueCourses; + +public class Study { + public static void main(String[] args) { + List students = Arrays.asList( + new Student("Стеша", + Arrays.asList( + new Course("Физика"), + new Course("Математическая логика"), + new Course("Теория вероятности"), + new Course("Аналитическая геометрия"), + new Course("Мат. анализ") + )), + new Student("Афанасий", + Arrays.asList( + new Course("Дискретная математика"), + new Course("Математическая логика"), + new Course("Аналитическая геометрия"), + new Course("Мат. анализ") + )), + new Student("Серафим", + Arrays.asList( + new Course("Теоретическая механика"), + new Course("Аналитическая геометрия"), + new Course("Мат. анализ") + )), + new Student("Фекла", + Arrays.asList( + new Course("Аналитическая геометрия"), + new Course("Мат. анализ") + )) + ); + Course mathLogic = new Course("Математическая логика"); + + System.out.println("Список уникальных курсов: " + getUniqueCourses(students)); + System.out.println("Список трёх самых любознательных студентов: " + + getListOfThirstyForKnowledgeStudent(students, 3)); + System.out.println("Список студентов, посещающих курс \"Математическая логика\": " + getListOfStudentVisitingCourse(students, mathLogic)); + } +}