Advertisement
Guest User

Untitled

a guest
Jun 24th, 2020
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 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. private String color;
  9. private int capacity;
  10. 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. public int count() {
  27. return this.data.size();
  28. }
  29.  
  30. public void add(Present present) {
  31. if (this.capacity > this.data.size()) {
  32. this.data.add(present);
  33. }
  34. }
  35.  
  36. public boolean remove(String name) {
  37. for (int i = 0; i < this.data.size(); i++) {
  38. Present present = this.data.get(i);
  39. if (present.getName().equals(name)) {
  40. this.data.remove(i);
  41. return true;
  42. }
  43. }
  44. return false;
  45. }
  46.  
  47. public Present heaviestPresent() {
  48. Present present2 = this.data.stream()
  49. .max(Comparator.comparingDouble(Present::getWeight)).orElse(this.data.get(0));
  50. return present2;
  51. }
  52.  
  53. public Present getPresent(String name) {
  54. Present present2 = this.data.stream().filter(e-> e.getName().equals(name)).findFirst().orElse(this.data.get(0));
  55. return present2;
  56. }
  57.  
  58. public String report() {
  59. StringBuilder str = new StringBuilder(String.format("%s bag contains:%n", this.getColor()));
  60. for (Present present : this.data) {
  61. str.append(present.toString()).append(System.lineSeparator());
  62. }
  63. return str.toString().trim();
  64. }
  65.  
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement