veronikaaa86

06. Students 2.0

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