Advertisement
Guest User

румар

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