Advertisement
Guest User

Untitled

a guest
Jun 25th, 2021
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.64 KB | None | 0 0
  1. package university;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. public class University {
  7.     public int capacity;
  8.     public List<Student> students;
  9.  
  10.  
  11.     public University(int capacity) {
  12.         this.capacity = capacity;
  13.         this.students = new ArrayList<>();
  14.     }
  15.  
  16.     public int getCapacity() {
  17.         return this.capacity;
  18.     }
  19.  
  20.     public List<Student> getStudents() {
  21.         return this.students;
  22.     }
  23.  
  24.  
  25.     public int getStudentCount() {
  26.         return this.students.size();
  27.     }
  28.  
  29.     public String registerStudent(Student student) {
  30.         StringBuilder output = new StringBuilder();
  31.         if (this.students.size() < capacity) {
  32.             if (this.students.contains(student)) {
  33.                 output
  34.                         .append("Student is already in the university");
  35.             } else {
  36.                 this.students.add(student);
  37.                 output.append("Added student ")
  38.                         .append(student.firstName)
  39.                         .append(" ")
  40.                         .append(student.lastName);
  41.             }
  42.         } else {
  43.             output
  44.                     .append("No seats in the university");
  45.         }
  46.         return output.toString();
  47.     }
  48.  
  49.     public String dismissStudent(Student student) {
  50.         StringBuilder output = new StringBuilder();
  51.         if (this.students.contains(student)) {
  52.             output
  53.                     .append("Removed student ")
  54.                     .append(student.firstName)
  55.                     .append(" ")
  56.                     .append(student.lastName);
  57.             this.students.remove(student);
  58.         } else {
  59.             output
  60.                     .append("Student not found");
  61.         }
  62.         return output.toString();
  63.     }
  64.  
  65.     public Student getStudent(String firstName, String lastName) {
  66.         return this.students
  67.                 .stream()
  68.                 .filter(student -> student.firstName.equals(firstName) && student.lastName.equals(lastName))
  69.                 .findFirst()
  70.                 .orElse(null);
  71.     }
  72.  
  73.     public String getStatistics() {
  74.         StringBuilder output = new StringBuilder();
  75.         this.students
  76.                 .forEach(student -> output
  77.                         .append("==Student: First Name = ")
  78.                         .append(student.firstName)
  79.                         .append(", Last Name = ")
  80.                         .append(student.lastName)
  81.                         .append(", Best Subject = ")
  82.                         .append(student.bestSubject)
  83.                         .append(System.lineSeparator()));
  84.         return output.toString().trim();
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement