RGAlex

Cage-Rabbits/2019

Jun 20th, 2021
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.81 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. public class Cage {
  5.  
  6.     private String name;
  7.     private int capacity;
  8.     private List<Rabbit> data;
  9.  
  10.     public Cage(String name, int capacity) {
  11.         this.name = name;
  12.         this.capacity = capacity;
  13.         this.data = new ArrayList<>();
  14.     }
  15.  
  16.     public void removeSpecies(String species){
  17.         this.data=this.data.stream().filter(e->!e.getSpecies().equals(species)).collect(Collectors.toList());
  18.     }
  19.  
  20.     public Rabbit sellRabbit(String name){
  21.         this.data.stream().filter(e->e.getName().equals(name)).findFirst().ifPresent(v->v.setAvailable(false));
  22.         return this.data.stream().filter(e->e.getName().equals(name)).findFirst().orElse(null);
  23.     }
  24.  
  25.     public List<Rabbit> sellRabbitBySpecies(String species){
  26.         this.data.stream().filter(e->e.getSpecies().equals(species)).forEach(v->v.setAvailable(false));
  27.         return this.data.stream().filter(e->e.getSpecies().equals(species)).collect(Collectors.toList());
  28.     }
  29.  
  30.     public String report(){
  31.         StringBuilder output=new StringBuilder("Rabbits available at ");
  32.         output.append(getName()).append(":").append(System.lineSeparator());
  33.         this.data.stream().filter(Rabbit::isAvailable).forEach(v->output.append(v).append(System.lineSeparator()));
  34.         return output.toString();
  35.     }
  36.  
  37.  
  38.     public boolean removeRabbit(String name){
  39.         return this.data.removeIf(element->element.getName().equals(name));
  40.     }
  41.  
  42.     public int count(){
  43.         return this.data.size();
  44.     }
  45.  
  46.     public void add(Rabbit rabbit){
  47.         if(this.data.size()<this.capacity){
  48.             this.data.add(rabbit);
  49.         }
  50.     }
  51.  
  52.     public String getName() {
  53.         return name;
  54.     }
  55.  
  56.     public int getCapacity() {
  57.         return capacity;
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment