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