Advertisement
desislava_topuzakova

07. Grooming Salon

Oct 29th, 2022
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. клас Pet:
  2.  
  3. package groomingSalon;
  4.  
  5. public class Pet {
  6. private String name;
  7. private int age;
  8. private String owner;
  9.  
  10. public Pet(String name, int age, String owner) {
  11. this.name = name;
  12. this.age = age;
  13. this.owner = owner;
  14. }
  15.  
  16. public String getName() {
  17. return name;
  18. }
  19.  
  20. public void setName(String name) {
  21. this.name = name;
  22. }
  23.  
  24. public int getAge() {
  25. return age;
  26. }
  27.  
  28. public void setAge(int age) {
  29. this.age = age;
  30. }
  31.  
  32. public String getOwner() {
  33. return owner;
  34. }
  35.  
  36. public void setOwner(String owner) {
  37. this.owner = owner;
  38. }
  39.  
  40. @Override
  41. public String toString() {
  42. return String.format("%s %d - (%s)", name, age, owner);// "{name} {age} - ({owner})"
  43.  
  44.  
  45. }
  46.  
  47. }
  48.  
  49. клас GroomingSalon:
  50.  
  51. package groomingSalon;
  52.  
  53. import java.util.ArrayList;
  54. import java.util.List;
  55. import java.util.stream.Collectors;
  56.  
  57. public class GroomingSalon {
  58. private List<Pet> data;
  59. private int capacity;
  60.  
  61. public GroomingSalon(int capacity) {
  62. this.capacity = capacity;
  63. this.data = new ArrayList<>();
  64. }
  65.  
  66. public void add(Pet pet) {
  67. if (this.data.size() < this.capacity) {
  68. this.data.add(pet);
  69. }
  70. }
  71.  
  72. public int getCount() {
  73. return this.data.size();
  74. }
  75. public boolean remove(String name) {
  76. int index = findpetIndex(name);
  77.  
  78. if (index != -1) {
  79. data.remove(index);
  80. return true;
  81. }
  82.  
  83. return false;
  84. }
  85.  
  86. public Pet getPet(String name, String owner) {
  87. return this.data.stream()
  88. .filter(pet -> pet.getName().equals(name) && pet.getOwner().equals(owner))
  89. .findFirst().orElse(null);
  90. }
  91.  
  92. private int findpetIndex(String name) {
  93. for (int i = 0; i < this.data.size(); i++) {
  94. if (this.data.get(i).getName().equals(name)) {
  95. return i;
  96. }
  97. }
  98. return -1;
  99. }
  100.  
  101. public String getStatistics() {
  102. StringBuilder builder = new StringBuilder("The grooming salon has the following clients:" + System.lineSeparator());
  103.  
  104. for (Pet pet : data) {
  105. builder.append(pet.getName().toString());
  106. builder.append(" ");
  107. builder.append(pet.getOwner().toString());
  108. builder.append(System.lineSeparator());
  109. }
  110.  
  111. return builder.toString().trim();
  112. }
  113. }
  114.  
  115. клас Main:
  116.  
  117. package groomingSalon;
  118.  
  119. public class Main {
  120.  
  121. public static void main(String[] args) {
  122. // Initialize the repository
  123. GroomingSalon salon = new GroomingSalon(20);
  124.  
  125. // Initialize entity
  126. Pet dog = new Pet("Ellias", 5, "Tim");
  127.  
  128. // Print Pet
  129. System.out.println(dog); // Ellias 5 - (Tim)
  130.  
  131. // Add Pet
  132. salon.add(dog);
  133.  
  134. // Remove Pet
  135. System.out.println(salon.remove("Ellias")); // true
  136. System.out.println(salon.remove("Pufa")); // false
  137.  
  138. Pet cat = new Pet("Bella", 2, "Mia");
  139. Pet bunny = new Pet("Zak", 4, "Jon");
  140.  
  141. salon.add(cat);
  142. salon.add(bunny);
  143.  
  144. // Get Pet
  145. Pet pet = salon.getPet("Bella", "Mia");
  146. System.out.println(pet); // Bella 2 - (Mia)
  147. //System.out.println(university.getStudent("Rihanna", "Fenty"));
  148.  
  149. // Count
  150. System.out.println(salon.getCount()); // 2
  151.  
  152. // Get Statistics
  153. System.out.println(salon.getStatistics());
  154. // The grooming salon has the following clients:
  155. //Bella Mia
  156. //Zak Jon
  157.  
  158. }
  159. }
  160.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement