Lyubohd

06. Students 2.0

Jun 23rd, 2021
709
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.     public static void main(String[] args) {
  7.         Scanner scan = new Scanner(System.in);
  8.         String input = scan.nextLine();
  9.         List<Student> students = new ArrayList<>();
  10.  
  11.         while (!"end".equals(input)) {
  12.             String[] tokens = input.split("\\s+");
  13.             String firstName = tokens[0];
  14.             String lastName = tokens[1];
  15.             int age = Integer.parseInt(tokens[2]);
  16.             String hometown = tokens[3];
  17.             if (!isAlreadyExsit(firstName, lastName, age, hometown, students)) {
  18.                 Student student = new Student(firstName, lastName, age, hometown);
  19.                 students.add(student);
  20.             }
  21.  
  22.             input = scan.nextLine();
  23.         }
  24.  
  25.         String hometown = scan.nextLine();
  26.         for (Student student : students) {
  27.             if (student.getHometown().equals(hometown)) {
  28.                 System.out.println(student);
  29.             }
  30.         }
  31.     }
  32.  
  33.     private static boolean isAlreadyExsit(String firstName, String lastName, int age, String hometown, List<Student> students) {
  34.         for (Student student : students) {
  35.             boolean isFirstNameEqual = student.getFirstName().equals(firstName);
  36.             boolean isLastNameEqual = student.getLastName().equals(lastName);
  37.             if (isFirstNameEqual && isLastNameEqual) {
  38.                 student.setAge(age);
  39.                 student.setHometown(hometown);
  40.                 return true;
  41.             }
  42.         }
  43.  
  44.         return false;
  45.     }
  46.  
  47.     public static class Student {
  48.         private String firstName;
  49.         private String lastName;
  50.         private int age;
  51.         private String hometown;
  52.  
  53.         public Student(String firstName, String lastName, int age, String hometown) {
  54.             this.firstName = firstName;
  55.             this.lastName = lastName;
  56.             this.age = age;
  57.             this.hometown = hometown;
  58.         }
  59.  
  60.         public String getHometown() {
  61.             return this.hometown;
  62.         }
  63.  
  64.         public void setHometown(String hometown) {
  65.             this.hometown = hometown;
  66.         }
  67.  
  68.         public void setAge(int age) {
  69.             this.age = age;
  70.         }
  71.  
  72.         public String getFirstName() {
  73.             return this.firstName;
  74.         }
  75.  
  76.         public String getLastName() {
  77.             return this.lastName;
  78.         }
  79.  
  80.         public String toString() {
  81.             return this.firstName + " " + this.lastName + " is " + this.age + " years old" ;
  82.         }
  83.     }
  84.  
  85. }
  86.  
RAW Paste Data