Didart

Students 2.0

Oct 27th, 2021
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class Students2Point0 {
  6.     static class Student {
  7.         String firstName;
  8.         String lastName;
  9.         String age;
  10.         String town;
  11.  
  12.         public Student(String firstName, String lastName, String age, String town) {
  13.             this.firstName = firstName;
  14.             this.lastName = lastName;
  15.             this.age = age;
  16.             this.town = town;
  17.         }
  18.  
  19.         public String getFirstName() {
  20.             return firstName;
  21.         }
  22.  
  23.         public String getLastName() {
  24.             return lastName;
  25.         }
  26.  
  27.         public String getAge() {
  28.             return age;
  29.         }
  30.  
  31.         public String getTown() {
  32.             return town;
  33.         }
  34.  
  35.         public void setAge(String age) {
  36.             this.age = age;
  37.         }
  38.  
  39.         public void setTown(String town) {
  40.             this.town = town;
  41.         }
  42.  
  43.         @Override
  44.         public String toString() {
  45.             return String.format("%s %s is %s years old%n",
  46.                     this.getFirstName(),
  47.                     this.getLastName(),
  48.                     this.getAge());
  49.         }
  50.     }
  51.  
  52.     public static void main(String[] args) {
  53.         Scanner scanner = new Scanner(System.in);
  54.  
  55.         List<Student> studentsList = new ArrayList<>();
  56.  
  57.         String input = scanner.nextLine();
  58.         while (!input.equals("end")) {
  59.             String[] inputLine = input.split(" ");
  60.             String firstName = inputLine[0];
  61.             String lastName = inputLine[1];
  62.             String age = inputLine[2];
  63.             String town = inputLine[3];
  64.  
  65.             Student student = new Student(firstName, lastName, age, town);
  66.  
  67.             int existingIndex = findStudentIndex(studentsList, student.getFirstName(), student.getLastName());
  68.             if (existingIndex != -1) {
  69.                 studentsList.get(existingIndex).setTown(student.town);
  70.                 studentsList.get(existingIndex).setAge(student.age);
  71.             } else {
  72.                 studentsList.add(student);
  73.             }
  74.             input = scanner.nextLine();
  75.         }
  76.  
  77.         String searchTownName = scanner.nextLine();
  78.  
  79.         for (Student student : studentsList) {
  80.             if (student.getTown().equals(searchTownName)) {
  81.                 System.out.print(student);
  82.             }
  83.         }
  84.     }
  85.  
  86.     static int findStudentIndex(List<Student> studentsList, String firstName, String lastName) {
  87.         for (int i = 0; i < studentsList.size(); i++) {
  88.             String firstNameList = studentsList.get(i).getFirstName();
  89.             String lastNameList = studentsList.get(i).getLastName();
  90.  
  91.             if (firstNameList.equals(firstName) && lastNameList.equals(lastName)) {
  92.                 return i;
  93.             }
  94.         }
  95.         return -1;
  96.     }
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment