Advertisement
purshink

Bag

Jun 26th, 2020
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.58 KB | None | 0 0
  1. package christmas;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Comparator;
  5. import java.util.List;
  6.  
  7. public class Bag {
  8.  
  9.     private String color;
  10.     private int capacity;
  11.     private List<Present> data;
  12.  
  13.     public Bag(String color, int capacity) {
  14.         this.color = color;
  15.         this.capacity = capacity;
  16.         this.data = new ArrayList<>();
  17.     }
  18.  
  19.     public int getCapacity() {
  20.         return capacity;
  21.     }
  22.  
  23.     public String getColor() {
  24.         return color;
  25.     }
  26.  
  27.     public  int count(){
  28.         return this.data.size();
  29.     }
  30.  
  31.     public void add(Present present) {
  32.         if (this.getCapacity() > this.data.size()){
  33.             this.data.add(present);
  34.         }
  35.     }
  36.     public boolean remove(String name) {
  37.         return data.removeIf(p -> p.getName().equals(name));
  38.     }
  39.  
  40.     public Present heaviestPresent(){
  41.         Present present2 = this.data.stream()
  42.                 .max(Comparator.comparingDouble(Present::getWeight)).orElse(null);
  43.         return present2;
  44.     }
  45.     public Present getPresent(String name){
  46.         Present present2 = this.data.stream().filter(e-> e.getName().equals(name)).findFirst().orElse(null);
  47.         return present2;
  48.     }
  49.  
  50.     public String report() {
  51.          StringBuilder out = new StringBuilder();
  52.  
  53.      out.append(this.getColor()).append(" bag contains:").append(System.lineSeparator());
  54.  
  55.         for (Present present : data) {
  56.  
  57.             out.append(present.toString())
  58.                     .append(System.lineSeparator());
  59.  
  60.         }
  61.  
  62.         return  out.toString().trim();
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement