TurkuBasicOOPinJava / Week 4: Writing classes /09b. Create Storage set methods
KaiquanMah's picture
Create 09b. Create Storage set methods
88117e3 verified
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