SHARE
TWEET

Students 2.0

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