dimipan80

Java Stream API: Students Joined to Specialties

Aug 13th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.43 KB | None | 0 0
  1. /*
  2. * Create a new class StudentSpecialty that holds specialty name and faculty number.
  3. * Create a Student class that holds student name and faculty number.
  4. * Create a list of student specialties, where each specialty corresponds to a certain student (via the faculty number).
  5. * You will recieve several specialties in the format :
  6. *   {specialty name} {specialty name} {faculty number}
  7. * Until you reach "Students:" , you should add specialties to the collection.
  8. * After you reach "Students:", you should start reading students in the format :
  9. *   {faculty number} {student's first name} {student's second name}
  10. * You should add the students untill you recieve "END" command.
  11. * Print all student names alphabetically along with their faculty number and specialty name.
  12. * (Use something like "join" LINQ operator in C#.)
  13. * Examples:
  14. *   Student Specialties      join       Students                →          Result (Joined Students with Specialties)
  15. *   SpecialtyName   FacNum          FacNum  Name                              Name        FacNum        Specialty
  16. *   Web Developer   203314          215314  Milena Kirova                 Asya Manova     203314       Web Developer
  17. *   Web Developer   203114          203114  Stefan Popov                  Asya Manova     203314       QA Engineer
  18. *   PHP Developer   203814          203314  Asya Manova                   Diana Petrova   203914       PHP Developer
  19. *   PHP Developer   203914          203914  Diana Petrova                 Diana Petrova   203914       Web Developer
  20. *    QA Engineer    203314          203814  Ivan Ivanov                   Ivan Ivanov     203814       PHP Developer
  21. *   Web Developer   203914                                                Stefan Popov    203114       Web Developer
  22. *
  23. */
  24.  
  25. import java.io.BufferedReader;
  26. import java.io.IOException;
  27. import java.io.InputStreamReader;
  28. import java.util.ArrayList;
  29. import java.util.Comparator;
  30. import java.util.function.Consumer;
  31. import java.util.function.Predicate;
  32.  
  33. public class StudentsJoinedToSpecialties {
  34.     public static void main(String[] args) throws IOException {
  35.         ArrayList<StudentSpecialty> specialtiesList = new ArrayList<>();
  36.  
  37.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  38.         String[] specialityInfo;
  39.         String nextLine = reader.readLine();
  40.         while (!"Students:".equals(nextLine)) {
  41.             specialityInfo = nextLine.split("\\s+");
  42.             specialtiesList
  43.                     .add(new StudentSpecialty(String.format("%s %s",
  44.                             specialityInfo[0], specialityInfo[1]), specialityInfo[2]));
  45.  
  46.             nextLine = reader.readLine();
  47.         }
  48.  
  49.         ArrayList<Student> studentsList = new ArrayList<>();
  50.         String[] studentInfo;
  51.         nextLine = reader.readLine();
  52.         while (!"END".equals(nextLine)) {
  53.             studentInfo = nextLine.split("\\s+");
  54.             studentsList
  55.                     .add(new Student(String.format("%s %s", studentInfo[1], studentInfo[2]),
  56.                             studentInfo[0]));
  57.  
  58.             nextLine = reader.readLine();
  59.         }
  60.  
  61.         studentsList.stream().sorted(Comparator.naturalOrder())
  62.                 .forEachOrdered(st -> specialtiesList.stream()
  63.                         .filter(getFilterByFacultyNumberPredicate(st))
  64.                         .forEachOrdered(printStudentSpecialtyConsumer(st)));
  65.  
  66.     }
  67.  
  68.     private static Consumer<StudentSpecialty> printStudentSpecialtyConsumer(Student student) {
  69.         return spec -> System.out.printf("%s %s %s\r\n",
  70.                 student.studentName, student.facultyNumber, spec.specialtyName);
  71.     }
  72.  
  73.     private static Predicate<StudentSpecialty> getFilterByFacultyNumberPredicate(Student student) {
  74.         return spec -> student.facultyNumber.equals(spec.facultyNumber);
  75.     }
  76. }
  77.  
  78. class StudentSpecialty {
  79.     final String specialtyName;
  80.     final String facultyNumber;
  81.  
  82.     StudentSpecialty(String name, String facNumber) {
  83.         this.specialtyName = name;
  84.         this.facultyNumber = facNumber;
  85.     }
  86.  
  87.     @Override
  88.     public int hashCode() {
  89.         return this.specialtyName.hashCode() + this.facultyNumber.hashCode();
  90.     }
  91. }
  92.  
  93. class Student implements Comparable<Student> {
  94.     final String studentName;
  95.     final String facultyNumber;
  96.  
  97.     Student(String name, String facNum) {
  98.         this.studentName = name;
  99.         this.facultyNumber = facNum;
  100.     }
  101.  
  102.     @Override
  103.     public int compareTo(Student student) {
  104.         return this.studentName.compareTo(student.studentName);
  105.     }
  106.  
  107.     @Override
  108.     public int hashCode() {
  109.         return this.studentName.hashCode() + this.facultyNumber.hashCode();
  110.     }
  111. }
Add Comment
Please, Sign In to add comment