NadezhdaGeorgieva

Cage

Feb 18th, 2021
806
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package rabbits;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. public class Cage {
  7.     private String name;
  8.     private int capacity;
  9.     private List<Rabbit> data;
  10.  
  11.     public Cage(String name, int capacity) {
  12.         this.name = name;
  13.         this.capacity = capacity;
  14.         this.data = new ArrayList<>();
  15.     }
  16.  
  17.     public String getName(){
  18.         return this.name;
  19.     }
  20.  
  21.     public int getCapacity(){
  22.         return this.capacity;
  23.     }
  24.  
  25.     public void add(Rabbit rabbit){
  26.         if (this.capacity > this.data.size()){
  27.             this.data.add(rabbit);
  28.         }
  29.     }
  30.  
  31.     public boolean removeRabbit(String name){
  32.         return this.data.removeIf(rabbit -> rabbit.getName().equals(name));
  33.     }
  34.  
  35.     public void removeSpecies(String species){
  36.         this.data.removeIf(rabbit -> rabbit.getSpecies().equals(species));
  37.     }
  38.  
  39.     public Rabbit sellRabbit(String name){
  40.         for (Rabbit rabbit : this.data) {
  41.             if (rabbit.getName().equals(name)){
  42.                 rabbit.setAvailable(false);
  43.                 return rabbit;
  44.             }
  45.         }
  46.         return null;
  47.     }
  48.  
  49.     public List<Rabbit> sellRabbitBySpecies(String species){
  50.         List<Rabbit> soldRabbits = new ArrayList<>();
  51.         for (Rabbit rabbit : this.data) {
  52.             if (rabbit.getSpecies().equals(species)){
  53.                 rabbit.setAvailable(false);
  54.                 soldRabbits.add(rabbit);
  55.             }
  56.         }
  57.         return soldRabbits;
  58.     }
  59.  
  60.     public int count(){
  61.         return this.data.size();
  62.     }
  63.  
  64.     public String report(){
  65.         StringBuilder sb = new StringBuilder(String.format("Rabbits available at %s", this.name))
  66.                 .append(System.lineSeparator());
  67.         data.stream().filter(Rabbit::isAvailable)
  68.                 .forEach(rabbit -> sb.append(rabbit).append(System.lineSeparator()));
  69.         return sb.toString().trim();
  70.     }
  71. }
  72.  
RAW Paste Data