1
1
package com .company .samsara .box ;
2
2
3
-
4
3
import java .util .HashMap ;
5
4
import java .util .Map ;
6
5
import java .util .PriorityQueue ;
@@ -28,46 +27,62 @@ public class Box {
28
27
private Map <Integer , Valuable > contentMap ;
29
28
30
29
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" );
30
+ if (sizeBar <= 0 ) {
31
+ throw new IllegalArgumentException (
32
+ "Size bar of a box cannot be smaller thant or equal to zero" );
33
33
}
34
34
this .sizeBar = sizeBar ;
35
35
this .currentSize = 0 ;
36
- maxValuables = new PriorityQueue <>((a ,b )-> (b .size - a .size ));
36
+ maxValuables = new PriorityQueue <>((a , b ) -> (b .size - a .size ));
37
37
contentMap = new HashMap <>();
38
38
}
39
39
40
40
public void addValuable (Valuable valuable ) {
41
- if (valuable == null ) {
41
+ if (valuable == null ) {
42
42
throw new IllegalArgumentException ("added valuable cannot be null" );
43
43
}
44
44
45
- if (contentMap .containsKey (valuable .id )) {
45
+ if (contentMap .containsKey (valuable .id )) {
46
46
throw new IllegalArgumentException ("valuable is already in the box" );
47
47
}
48
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 ));
49
+ if (currentSize + valuable .size > sizeBar ) {
50
+ throw new IllegalArgumentException (
51
+ String .format (
52
+ "adding this valuable with size %d will exceeds the box' size bar %d" ,
53
+ valuable .size , sizeBar ));
51
54
}
52
55
53
56
contentMap .put (valuable .id , valuable );
54
57
maxValuables .offer (valuable );
55
58
currentSize += valuable .size ;
56
- System .out .println ("Added " + valuable .toString () + ", current box size: " + currentSize + " maxValueSize: " + getMaxValuableSize ());
59
+ System .out .println (
60
+ "Added "
61
+ + valuable .toString ()
62
+ + ", current box size: "
63
+ + currentSize
64
+ + " maxValueSize: "
65
+ + getMaxValuableSize ());
57
66
}
58
67
59
68
public void removeValuable (int id ) {
60
- if (!contentMap .containsKey (id )) {
69
+ if (!contentMap .containsKey (id )) {
61
70
throw new IllegalArgumentException ("valuable is no in the box" );
62
71
}
63
72
Valuable removed = contentMap .remove (id );
64
73
maxValuables .remove (removed );
65
74
currentSize -= removed .size ;
66
- System .out .println ("Removed " + removed .toString () + ", current box size: " + currentSize + " maxValueSize: " + getMaxValuableSize ());
75
+ System .out .println (
76
+ "Removed "
77
+ + removed .toString ()
78
+ + ", current box size: "
79
+ + currentSize
80
+ + " maxValueSize: "
81
+ + getMaxValuableSize ());
67
82
}
68
83
69
84
public int getMaxValuableSize () {
70
- if (maxValuables .isEmpty ()) {
85
+ if (maxValuables .isEmpty ()) {
71
86
return 0 ;
72
87
} else {
73
88
return maxValuables .peek ().size ;
@@ -84,10 +99,10 @@ public static void main(String[] args) {
84
99
Valuable v9 = new Valuable (6 , "valuable 9" , 9 );
85
100
86
101
Box aBox = new Box (30 );
87
- try {
102
+ try {
88
103
aBox .addValuable (v1 );
89
104
aBox .addValuable (v10 );
90
- // aBox.addValuable(v20);
105
+ // aBox.addValuable(v20);
91
106
aBox .removeValuable (v10 .id );
92
107
aBox .addValuable (v29 );
93
108
aBox .removeValuable (v29 .id );
@@ -97,33 +112,6 @@ public static void main(String[] args) {
97
112
} catch (IllegalArgumentException e ) {
98
113
System .out .println (e .getMessage ());
99
114
}
100
-
101
115
}
102
-
103
116
}
104
117
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