Advertisement
IvaAnd

VetClinic/Clinic

Oct 18th, 2020
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. package vetClinic;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Comparator;
  5. import java.util.List;
  6.  
  7. public class Clinic {
  8. private int capacity;
  9. private List<Pet> data;
  10.  
  11. public Clinic(int capacity) {
  12. this.capacity = capacity;
  13. this.data = new ArrayList<>();
  14. }
  15.  
  16. public void add(Pet pet) {
  17. if (this.data.size() < capacity) {
  18. this.data.add(pet);
  19. }
  20. }
  21.  
  22. public boolean remove(String name) {
  23. if (!this.data.isEmpty()) {
  24. for (Pet pet : this.data) {
  25. if (pet.getName().equals(name)) {
  26. this.data.remove(pet);
  27. return true;
  28. }
  29.  
  30. }
  31. }
  32.  
  33. return false;
  34. }
  35.  
  36.  
  37. /* public void remove(String name){
  38. if(this.data.contains()){
  39. this.data.get(name) }
  40. }*/
  41.  
  42.  
  43. public Pet getOldestPet() {
  44. // int oldest = 0;
  45. // String petName = "";
  46. // if (!this.data.isEmpty()) {
  47. // for (Pet pet : this.data) {
  48. // if (pet.getAge() > oldest) {
  49. // oldest = pet.getAge();
  50. // petName = pet.getName();
  51. // }
  52. // }
  53. //
  54. // for (Pet oldestPet : this.data) {
  55. // if (oldestPet.getName().equals(petName)) {
  56. // return oldestPet;
  57. // }
  58. //
  59. // }
  60. // }
  61. // return null;
  62. return this.data.stream().max(Comparator.comparingInt(Pet::getAge)).orElseThrow();
  63. }
  64.  
  65. public Pet getPet(String name, String owner) {
  66. if (!this.data.isEmpty()) {
  67. for (Pet pet : this.data) {
  68. if (pet.getName().equals(name) && pet.getOwner().equals(owner)) {
  69. return pet;
  70. }
  71.  
  72. }
  73. }
  74. return null;
  75. }
  76.  
  77. public int getCount() {
  78. return this.data.size();
  79. }
  80.  
  81. public String getStatistics() {
  82. StringBuilder statistics = new StringBuilder();
  83. statistics.append("The clinic has the following patients:").append(System.lineSeparator());
  84.  
  85. for (Pet pet : this.data) {
  86. statistics.append(pet.getName() + " " + pet.getOwner());
  87. statistics.append(System.lineSeparator());
  88. }
  89.  
  90. return statistics.toString();
  91. }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement