Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package vetClinic;
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.List;
- public class Clinic {
- private int capacity;
- private List<Pet> data;
- public Clinic(int capacity) {
- this.capacity = capacity;
- this.data = new ArrayList<>();
- }
- public void add(Pet pet) {
- if (this.data.size() < capacity) {
- this.data.add(pet);
- }
- }
- public boolean remove(String name) {
- if (!this.data.isEmpty()) {
- for (Pet pet : this.data) {
- if (pet.getName().equals(name)) {
- this.data.remove(pet);
- return true;
- }
- }
- }
- return false;
- }
- /* public void remove(String name){
- if(this.data.contains()){
- this.data.get(name) }
- }*/
- public Pet getOldestPet() {
- // int oldest = 0;
- // String petName = "";
- // if (!this.data.isEmpty()) {
- // for (Pet pet : this.data) {
- // if (pet.getAge() > oldest) {
- // oldest = pet.getAge();
- // petName = pet.getName();
- // }
- // }
- //
- // for (Pet oldestPet : this.data) {
- // if (oldestPet.getName().equals(petName)) {
- // return oldestPet;
- // }
- //
- // }
- // }
- // return null;
- return this.data.stream().max(Comparator.comparingInt(Pet::getAge)).orElseThrow();
- }
- public Pet getPet(String name, String owner) {
- if (!this.data.isEmpty()) {
- for (Pet pet : this.data) {
- if (pet.getName().equals(name) && pet.getOwner().equals(owner)) {
- return pet;
- }
- }
- }
- return null;
- }
- public int getCount() {
- return this.data.size();
- }
- public String getStatistics() {
- StringBuilder statistics = new StringBuilder();
- statistics.append("The clinic has the following patients:").append(System.lineSeparator());
- for (Pet pet : this.data) {
- statistics.append(pet.getName() + " " + pet.getOwner());
- statistics.append(System.lineSeparator());
- }
- return statistics.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement