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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
In the attached program, the class Storage is defined.
|
| 2 |
+
|
| 3 |
+
Implement the missing setting methods for all attributes in the warehouse.
|
| 4 |
+
|
| 5 |
+
The state of the object is subject to the following constraints, which must be taken into account in the setting methods:
|
| 6 |
+
identifier can be any string
|
| 7 |
+
capacity must not be negative or zero
|
| 8 |
+
objects must not be negative or greater than the capacity
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
import java.util.Random;
|
| 15 |
+
|
| 16 |
+
public class Test {
|
| 17 |
+
public static void main(String[] args) {
|
| 18 |
+
final Random r = new Random();
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
System.out.println("Testing the class...");
|
| 22 |
+
|
| 23 |
+
Warehouse w1 = new Warehouse("Cellar", 20, 0);
|
| 24 |
+
Warehouse w2 = new Warehouse("Attic", 30, 0);
|
| 25 |
+
Warehouse w3 = new Warehouse("RootCellar", 10, 0);
|
| 26 |
+
|
| 27 |
+
Warehouse[] warehouses = {w1, w2, w3};
|
| 28 |
+
|
| 29 |
+
for (Warehouse warehouse : warehouses) {
|
| 30 |
+
System.out.println("Created warehouse with the statement");
|
| 31 |
+
System.out.println("" + warehouse);
|
| 32 |
+
|
| 33 |
+
String identifier = warehouse.getIdentifier() + (r.nextInt(10) + 1);
|
| 34 |
+
System.out.println("Setting identifier to " + identifier);
|
| 35 |
+
warehouse.setIdentifier(identifier);
|
| 36 |
+
System.out.println("Identifier now: " + warehouse.getIdentifier());
|
| 37 |
+
System.out.println("");
|
| 38 |
+
|
| 39 |
+
int capacity = r.nextInt(40) + 10;
|
| 40 |
+
System.out.println("Setting a valid value for capacity: " + capacity);
|
| 41 |
+
warehouse.setCapacity(capacity);
|
| 42 |
+
System.out.println("Capacity now: " + warehouse.getCapacity());
|
| 43 |
+
System.out.println("");
|
| 44 |
+
|
| 45 |
+
int negCapa = 0 - r.nextInt(40) - 1;
|
| 46 |
+
System.out.println("Setting a forbidden value for capacity: " + negCapa);
|
| 47 |
+
warehouse.setCapacity(negCapa);
|
| 48 |
+
System.out.println("Capacity now: " + warehouse.getCapacity());
|
| 49 |
+
System.out.println("");
|
| 50 |
+
|
| 51 |
+
int items = warehouse.getCapacity() - r.nextInt(warehouse.getCapacity());
|
| 52 |
+
System.out.println("Setting a valid value for items: " + items);
|
| 53 |
+
warehouse.setItems(items);
|
| 54 |
+
System.out.println("Items now: " + warehouse.getItems());
|
| 55 |
+
System.out.println("");
|
| 56 |
+
|
| 57 |
+
items = warehouse.getCapacity() + r.nextInt(10) + 1;
|
| 58 |
+
System.out.println("Setting a forbidden value for items: " + items);
|
| 59 |
+
warehouse.setItems(items);
|
| 60 |
+
System.out.println("Items now: " + warehouse.getItems());
|
| 61 |
+
System.out.println("");
|
| 62 |
+
}
|
| 63 |
+
}
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
class Warehouse {
|
| 67 |
+
private String identifier;
|
| 68 |
+
private int capacity;
|
| 69 |
+
private int items;
|
| 70 |
+
|
| 71 |
+
public Warehouse(String identifier, int capacity, int items) {
|
| 72 |
+
this.identifier = identifier;
|
| 73 |
+
this.capacity = capacity;
|
| 74 |
+
this.items = items;
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
public String getIdentifier() {
|
| 78 |
+
return identifier;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
public int getCapacity() {
|
| 82 |
+
return capacity;
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
public int getItems() {
|
| 86 |
+
return items;
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
//ADD HERE
|
| 92 |
+
public void setIdentifier(String identifier) {
|
| 93 |
+
this.identifier = identifier;
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
public void setCapacity(int capacity) {
|
| 97 |
+
if (capacity > 0) {
|
| 98 |
+
this.capacity = capacity;
|
| 99 |
+
}
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
public void setItems(int items) {
|
| 103 |
+
if ((items >= 0) && (items <= capacity)) {
|
| 104 |
+
this.items = items;
|
| 105 |
+
}
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
@Override
|
| 116 |
+
public String toString() {
|
| 117 |
+
return "Warehouse(" + identifier + ", " + capacity + ", " + items + ")";
|
| 118 |
+
}
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
Testing the class...
|
| 128 |
+
Created warehouse with the statement
|
| 129 |
+
Warehouse(Cellar, 20, 0)
|
| 130 |
+
Setting identifier to Cellar2
|
| 131 |
+
Identifier now: Cellar2
|
| 132 |
+
|
| 133 |
+
Setting a valid value for capacity: 42
|
| 134 |
+
Capacity now: 42
|
| 135 |
+
|
| 136 |
+
Setting a forbidden value for capacity: -8
|
| 137 |
+
Capacity now: 42
|
| 138 |
+
|
| 139 |
+
Setting a valid value for items: 23
|
| 140 |
+
Items now: 23
|
| 141 |
+
|
| 142 |
+
Setting a forbidden value for items: 50
|
| 143 |
+
Items now: 23
|
| 144 |
+
|
| 145 |
+
Created warehouse with the statement
|
| 146 |
+
Warehouse(Attic, 30, 0)
|
| 147 |
+
Setting identifier to Attic9
|
| 148 |
+
Identifier now: Attic9
|
| 149 |
+
|
| 150 |
+
Setting a valid value for capacity: 26
|
| 151 |
+
Capacity now: 26
|
| 152 |
+
|
| 153 |
+
Setting a forbidden value for capacity: -40
|
| 154 |
+
Capacity now: 26
|
| 155 |
+
|
| 156 |
+
Setting a valid value for items: 15
|
| 157 |
+
Items now: 15
|
| 158 |
+
|
| 159 |
+
Setting a forbidden value for items: 27
|
| 160 |
+
Items now: 15
|
| 161 |
+
|
| 162 |
+
Created warehouse with the statement
|
| 163 |
+
Warehouse(RootCellar, 10, 0)
|
| 164 |
+
Setting identifier to RootCellar6
|
| 165 |
+
Identifier now: RootCellar6
|
| 166 |
+
|
| 167 |
+
Setting a valid value for capacity: 30
|
| 168 |
+
Capacity now: 30
|
| 169 |
+
|
| 170 |
+
Setting a forbidden value for capacity: -19
|
| 171 |
+
Capacity now: 30
|
| 172 |
+
|
| 173 |
+
Setting a valid value for items: 18
|
| 174 |
+
Items now: 18
|
| 175 |
+
|
| 176 |
+
Setting a forbidden value for items: 33
|
| 177 |
+
Items now: 18
|
| 178 |
+
|
| 179 |
+
|