|
| 1 | +package com.company.samsara.box; |
| 2 | + |
| 3 | + |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.PriorityQueue; |
| 7 | +import org.apache.commons.lang3.StringUtils; |
| 8 | + |
| 9 | +/** |
| 10 | + * PART 1: Implement a representation of valuable with two fields id and name. |
| 11 | + * |
| 12 | + * <p>PART 2: Implement a representation of box, which is used as a container of valuables. |
| 13 | + * Implement two methods. First is to add a valuable to the box (given id as input). Second is to |
| 14 | + * remove a valuable from the box(given valuable object as input) |
| 15 | + * |
| 16 | + * <p>PART3: Add an maximum size bar to the representation of box. Modify the add method so that if |
| 17 | + * the total size of valuables in the box will exceed the box size bar, do not add and print an |
| 18 | + * error message. Otherwise, add the valuable into the box. |
| 19 | + * |
| 20 | + * <p>PART 4: Implement a method to output the maximum valuable-size in the box whenever the method |
| 21 | + * is called |
| 22 | + */ |
| 23 | +public class Box { |
| 24 | + |
| 25 | + private int sizeBar; |
| 26 | + private int currentSize; |
| 27 | + private PriorityQueue<Valuable> maxValuables; |
| 28 | + private Map<Integer, Valuable> contentMap; |
| 29 | + |
| 30 | + public Box(int sizeBar) { |
| 31 | + if(sizeBar <= 0) { |
| 32 | + throw new IllegalArgumentException("Size bar of a box cannot be smaller thant or equal to zero"); |
| 33 | + } |
| 34 | + this.sizeBar = sizeBar; |
| 35 | + this.currentSize = 0; |
| 36 | + maxValuables = new PriorityQueue<>((a,b)->(b.size - a.size)); |
| 37 | + contentMap = new HashMap<>(); |
| 38 | + } |
| 39 | + |
| 40 | + public void addValuable(Valuable valuable) { |
| 41 | + if(valuable == null) { |
| 42 | + throw new IllegalArgumentException("added valuable cannot be null"); |
| 43 | + } |
| 44 | + |
| 45 | + if(contentMap.containsKey(valuable.id)) { |
| 46 | + throw new IllegalArgumentException("valuable is already in the box"); |
| 47 | + } |
| 48 | + |
| 49 | + if(currentSize + valuable.size > sizeBar) { |
| 50 | + throw new IllegalArgumentException(String.format("adding this valuable with size %d will exceeds the box' size bar %d", valuable.size, sizeBar)); |
| 51 | + } |
| 52 | + |
| 53 | + contentMap.put(valuable.id, valuable); |
| 54 | + maxValuables.offer(valuable); |
| 55 | + currentSize += valuable.size; |
| 56 | + System.out.println("Added " + valuable.toString() + ", current box size: " + currentSize + " maxValueSize: " + getMaxValuableSize()); |
| 57 | + } |
| 58 | + |
| 59 | + public void removeValuable(int id) { |
| 60 | + if(!contentMap.containsKey(id)) { |
| 61 | + throw new IllegalArgumentException("valuable is no in the box"); |
| 62 | + } |
| 63 | + Valuable removed = contentMap.remove(id); |
| 64 | + maxValuables.remove(removed); |
| 65 | + currentSize -= removed.size; |
| 66 | + System.out.println("Removed " + removed.toString() + ", current box size: " + currentSize + " maxValueSize: " + getMaxValuableSize()); |
| 67 | + } |
| 68 | + |
| 69 | + public int getMaxValuableSize() { |
| 70 | + if(maxValuables.isEmpty()) { |
| 71 | + return 0; |
| 72 | + } else { |
| 73 | + return maxValuables.peek().size; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public static void main(String[] args) { |
| 78 | + |
| 79 | + Valuable v1 = new Valuable(1, "valuable 1", 1); |
| 80 | + Valuable v10 = new Valuable(2, "valuable 10", 10); |
| 81 | + Valuable v20 = new Valuable(3, "valuable 20", 20); |
| 82 | + Valuable v29 = new Valuable(4, "valuable 29", 29); |
| 83 | + Valuable v5 = new Valuable(5, "valuable 5", 5); |
| 84 | + Valuable v9 = new Valuable(6, "valuable 9", 9); |
| 85 | + |
| 86 | + Box aBox = new Box(30); |
| 87 | + try{ |
| 88 | + aBox.addValuable(v1); |
| 89 | + aBox.addValuable(v10); |
| 90 | +// aBox.addValuable(v20); |
| 91 | + aBox.removeValuable(v10.id); |
| 92 | + aBox.addValuable(v29); |
| 93 | + aBox.removeValuable(v29.id); |
| 94 | + aBox.addValuable(v5); |
| 95 | + aBox.addValuable(v10); |
| 96 | + aBox.addValuable(v9); |
| 97 | + } catch (IllegalArgumentException e) { |
| 98 | + System.out.println(e.getMessage()); |
| 99 | + } |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | +} |
| 104 | + |
| 105 | +class Valuable { |
| 106 | + int id; |
| 107 | + String name; |
| 108 | + int size; |
| 109 | + |
| 110 | + public Valuable(int id, String name, int size) { |
| 111 | + if(StringUtils.isBlank(name)) { |
| 112 | + throw new IllegalArgumentException("Valuabe cannot be blank"); |
| 113 | + } |
| 114 | + if(size <= 0) { |
| 115 | + throw new IllegalArgumentException("Valuabe size cannot be smaller than or equal to 0"); |
| 116 | + } |
| 117 | + this.id = id; |
| 118 | + this.name = name; |
| 119 | + this.size = size; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public String toString() { |
| 124 | + return "[id = " + id + ", name = " + name + ", size = " + size + "]"; |
| 125 | + } |
| 126 | + |
| 127 | +} |
| 128 | + |
| 129 | + |
0 commit comments