Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package christmas;
- import java.util.ArrayList;
- import java.util.List;
- public class Bag {
- private String color;
- private int capacity;
- private List<Present> data;
- public Bag(String color, int capacity) {
- this.color = color;
- this.capacity = capacity;
- this.data = new ArrayList<>();
- }
- public String getColor() {
- return color;
- }
- public int getCapacity() {
- return capacity;
- }
- public int count() {
- return this.data.size();
- }
- public void add(Present present) {
- if (this.data.size() < this.getCapacity())
- this.data.add(present);
- }
- public boolean remove(String name){
- return this.data.removeIf(e-> e.getName().equals(name));
- }
- public Present heaviestPresent(){
- double mexWeight = Double.MIN_VALUE;
- Present heviestPresent = null;
- for (Present x : data) {
- if (x.getWeight() > mexWeight){
- mexWeight = x.getWeight();
- heviestPresent = x;
- }
- }
- return heviestPresent;
- }
- public Present getPresent(String name){
- Present toReturn = null;
- for (Present p : data) {
- if(p.getName().equals(name)){
- toReturn = p;
- break;
- }
- }
- return toReturn;
- }
- public String report(){
- StringBuilder sb = new StringBuilder();
- sb.append(this.getColor()).append(" bag contains:").append(System.lineSeparator());
- for (Present pres : data) {
- sb.append(pres).append(System.lineSeparator());
- }
- return sb.toString().trim();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement