Advertisement
Guest User

Untitled

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