Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Create a new class StudentSpecialty that holds specialty name and faculty number.
- * Create a Student class that holds student name and faculty number.
- * Create a list of student specialties, where each specialty corresponds to a certain student (via the faculty number).
- * You will recieve several specialties in the format :
- * {specialty name} {specialty name} {faculty number}
- * Until you reach "Students:" , you should add specialties to the collection.
- * After you reach "Students:", you should start reading students in the format :
- * {faculty number} {student's first name} {student's second name}
- * You should add the students untill you recieve "END" command.
- * Print all student names alphabetically along with their faculty number and specialty name.
- * (Use something like "join" LINQ operator in C#.)
- * Examples:
- * Student Specialties join Students → Result (Joined Students with Specialties)
- * SpecialtyName FacNum FacNum Name Name FacNum Specialty
- * Web Developer 203314 215314 Milena Kirova Asya Manova 203314 Web Developer
- * Web Developer 203114 203114 Stefan Popov Asya Manova 203314 QA Engineer
- * PHP Developer 203814 203314 Asya Manova Diana Petrova 203914 PHP Developer
- * PHP Developer 203914 203914 Diana Petrova Diana Petrova 203914 Web Developer
- * QA Engineer 203314 203814 Ivan Ivanov Ivan Ivanov 203814 PHP Developer
- * Web Developer 203914 Stefan Popov 203114 Web Developer
- *
- */
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.function.Consumer;
- import java.util.function.Predicate;
- public class StudentsJoinedToSpecialties {
- public static void main(String[] args) throws IOException {
- ArrayList<StudentSpecialty> specialtiesList = new ArrayList<>();
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- String[] specialityInfo;
- String nextLine = reader.readLine();
- while (!"Students:".equals(nextLine)) {
- specialityInfo = nextLine.split("\\s+");
- specialtiesList
- .add(new StudentSpecialty(String.format("%s %s",
- specialityInfo[0], specialityInfo[1]), specialityInfo[2]));
- nextLine = reader.readLine();
- }
- ArrayList<Student> studentsList = new ArrayList<>();
- String[] studentInfo;
- nextLine = reader.readLine();
- while (!"END".equals(nextLine)) {
- studentInfo = nextLine.split("\\s+");
- studentsList
- .add(new Student(String.format("%s %s", studentInfo[1], studentInfo[2]),
- studentInfo[0]));
- nextLine = reader.readLine();
- }
- studentsList.stream().sorted(Comparator.naturalOrder())
- .forEachOrdered(st -> specialtiesList.stream()
- .filter(getFilterByFacultyNumberPredicate(st))
- .forEachOrdered(printStudentSpecialtyConsumer(st)));
- }
- private static Consumer<StudentSpecialty> printStudentSpecialtyConsumer(Student student) {
- return spec -> System.out.printf("%s %s %s\r\n",
- student.studentName, student.facultyNumber, spec.specialtyName);
- }
- private static Predicate<StudentSpecialty> getFilterByFacultyNumberPredicate(Student student) {
- return spec -> student.facultyNumber.equals(spec.facultyNumber);
- }
- }
- class StudentSpecialty {
- final String specialtyName;
- final String facultyNumber;
- StudentSpecialty(String name, String facNumber) {
- this.specialtyName = name;
- this.facultyNumber = facNumber;
- }
- @Override
- public int hashCode() {
- return this.specialtyName.hashCode() + this.facultyNumber.hashCode();
- }
- }
- class Student implements Comparable<Student> {
- final String studentName;
- final String facultyNumber;
- Student(String name, String facNum) {
- this.studentName = name;
- this.facultyNumber = facNum;
- }
- @Override
- public int compareTo(Student student) {
- return this.studentName.compareTo(student.studentName);
- }
- @Override
- public int hashCode() {
- return this.studentName.hashCode() + this.facultyNumber.hashCode();
- }
- }
Add Comment
Please, Sign In to add comment