Advertisement
Guest User

Untitled

a guest
Mar 27th, 2019
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.01 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class Students {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         List<Student> students = new ArrayList<>();
  10.         String data = scanner.nextLine();
  11.  
  12.         while (!data.equals("end")) {
  13.             String[] tokens = data.split("\\s+");
  14.             String firstName = tokens[0];
  15.             String lastName = tokens[1];
  16.             int age = Integer.parseInt(tokens[2]);
  17.             String city = tokens[3];
  18.  
  19.             if (isStudentExisting(students, firstName, lastName)) {
  20.                 Student student = getStudent(students, firstName, lastName, age);
  21.             } else {
  22.                 Student student = new Student(firstName, lastName, age, city);
  23.                 students.add(student);
  24.             }
  25.  
  26.             data = scanner.nextLine();
  27.         }
  28.  
  29.         String filterCity = scanner.nextLine();
  30.         students.stream()
  31.                 .filter(s -> s.getCity().equals(filterCity))
  32.                 .forEach(s -> System.out.println(String.format("%s %s is %d years old", s.firstName, s.lastName, s.age)));
  33.     }
  34.  
  35.     private static boolean isStudentExisting(List<Student> students, String firstName, String lastName) {
  36.         for (Student student : students) {
  37.             if (student.getFirstName().equals(firstName) && student.lastName.equals(lastName)) {
  38.                 return true;
  39.             }
  40.         }
  41.         return false;
  42.     }
  43.  
  44.     private static Student getStudent(List<Student> students, String firstName, String lastName, int age) {
  45.         Student existingStudent = null;
  46.         for (Student student : students) {
  47.             if (student.getFirstName().equals(firstName) && student.lastName.equals(lastName)) {
  48.                 existingStudent = student;
  49.                 existingStudent.age = age;
  50.  
  51.             }
  52.         }
  53.         return existingStudent;
  54.     }
  55.  
  56.     static class Student {
  57.         private String firstName;
  58.         private String lastName;
  59.         private int age;
  60.         private String city;
  61.  
  62.         public Student(String firstName, String lastName, int age, String city) {
  63.             this.firstName = firstName;
  64.             this.lastName = lastName;
  65.             this.age = age;
  66.             this.city = city;
  67.         }
  68.  
  69.         public String getFirstName() {
  70.             return firstName;
  71.         }
  72.  
  73.         public void setFirstName(String firstName) {
  74.             this.firstName = firstName;
  75.         }
  76.  
  77.         public String getLastName() {
  78.             return lastName;
  79.         }
  80.  
  81.         public void setLastName(String lastName) {
  82.             this.lastName = lastName;
  83.         }
  84.  
  85.         public int getAge() {
  86.             return age;
  87.         }
  88.  
  89.         public void setAge(int age) {
  90.             this.age = age;
  91.         }
  92.  
  93.         public String getCity() {
  94.             return city;
  95.         }
  96.  
  97.         public void setCity(String city) {
  98.             this.city = city;
  99.         }
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement