Advertisement
IrinaIgnatova

Exam-JavaAdvanced-17.12-BagClass

Dec 26th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.68 KB | None | 0 0
  1. package christmas;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7.  
  8. public class Bag {
  9.     private List<Present> data;
  10.     private String color;
  11.     private int capacity;
  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 String getColor(String color) {
  20.         return this.color;
  21.     }
  22.  
  23.     public int getCapacity() {
  24.         return this.capacity;
  25.     }
  26.  
  27.     public int count() {
  28.         return this.data.size();
  29.     }
  30.  
  31.     public void add(Present present) {
  32.         if (data.size() < capacity) {
  33.             this.data.add(present);
  34.         }
  35.  
  36.     }
  37.  
  38.     public String remove(String name) {
  39.         if (this.data.removeIf(present -> present.getName().equals(name))){
  40.             return "true";
  41.         }else{
  42.             return "false";
  43.         }
  44.  
  45.     }
  46.     public Present heaviestPresent(){
  47.         Present present=null;
  48.         double maxWeight=-1;
  49.         for (Present p : data) {
  50.             if (p.getWeight()>maxWeight){
  51.                 maxWeight=p.getWeight();
  52.                 present=p;
  53.             }
  54.         }
  55.         return present;
  56.     }
  57.  
  58. //    public Present getPresent(String name) {
  59. //        return (Present) this.getPresent(name);
  60. //    }
  61.     public String report(){
  62.         return String.format("%s bag contains: ",this.getColor());
  63.         for (Present present : this.data) {
  64.             System.out.println(present);
  65.  
  66.         }
  67.  
  68.     }
  69.  
  70.     public List<Present> getData() {
  71.         return data;
  72.     }
  73.  
  74.     public String getColor() {
  75.         return color;
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement