Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2019
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class OStudents {
  6.  
  7. static class Student{
  8.  
  9. private String firstName;
  10. private String lastName;
  11. private int age;
  12. private String hometown;
  13.  
  14. public Student(String firstName, String lastName, int age, String hometown) {
  15.  
  16. this.firstName = firstName;
  17. this.lastName = lastName;
  18. this.age = age;
  19. this.hometown = hometown;
  20. }
  21.  
  22. public String getFirstName() {
  23. return firstName;
  24. }
  25.  
  26. public String getLastName() {
  27. return lastName;
  28. }
  29.  
  30. public String getHometown() {
  31. return hometown;
  32. }
  33.  
  34. public int getAge() {
  35. return age;
  36. }
  37.  
  38. public void setAge(int age) {
  39. this.age = age;
  40. }
  41. public void setHometown(String hometown) {
  42. this.hometown = hometown;
  43. }
  44. }
  45.  
  46.  
  47. static Student findStudent (String firstName, String lastName, List<Student> students){
  48. for (Student student : students) {
  49. if (student.getFirstName().equalsIgnoreCase(firstName)
  50. && student.getLastName().equalsIgnoreCase(lastName)){
  51. return student;
  52.  
  53. }
  54.  
  55. }
  56.  
  57. return null;
  58. }
  59. public static void main(String[] args) {
  60. Scanner scanner = new Scanner(System.in);
  61.  
  62. List<Student> students = new ArrayList<>();
  63.  
  64.  
  65. String input = scanner.next();
  66. while (input.equalsIgnoreCase("end")){
  67. String firstName = input;
  68. String lastName = scanner.next();
  69. int age = scanner.nextInt();
  70. String hometown = scanner.next();
  71.  
  72.  
  73. Student existingStudent = findStudent(firstName, lastName,students);
  74. if (existingStudent != null){
  75. existingStudent.setAge(age);
  76. existingStudent.setHometown(hometown);
  77. }else {
  78. Student student = new Student(firstName, lastName, age, hometown);
  79. students.add(student);
  80. }
  81. input = scanner.next();
  82. }
  83.  
  84. String cityName = scanner.next();
  85.  
  86. for (Student student : students) {
  87. if (student.getHometown().equals(cityName)){
  88. System.out.printf("%s %s is %d years old.%n",
  89. student.getFirstName(),student.getLastName(),student.getAge());
  90.  
  91. }
  92.  
  93. }
  94.  
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement