Skip to content
Snippets Groups Projects
Commit 45bffe50 authored by Ethan Robert's avatar Ethan Robert
Browse files

tpQU-04::exo-boite

parent 74d27bd5
No related branches found
No related tags found
No related merge requests found
package tpQU.tp04;
public enum Container {
BLISTER(1), BOX(10), CRATE(50);
private int capacity;
private Container(int capacity) {
this.capacity = capacity;
}
public int getCapacity() {
return this.capacity;
}
}
\ No newline at end of file
package tpQU.tp04;
public class ExtendedException extends Exception {
public ExtendedException () {}
public ExtendedException (String msg) {super(msg);}
}
\ No newline at end of file
package tpQU.tp04;
public class Thing {
private String name;
private int quantity;
private Container container;
public Thing(String name, int quantity, Container container) {
if (container.getCapacity() >= quantity) {
this.name = name;
this.quantity = quantity;
this.container = container;
} else {
throw new ExtendedException("Unable to set the proper quantity");
}
}
public void setQuantity(int newQuantity) {
if (this.container.getCapacity() >= newQuantity) {
this.quantity = newQuantity;
} else {
throw new ExtendedException("Unable to set the proper quantity : the current container is too small");
}
}
public boolean transferToLargerContainer() {
for (int i = 0; i < Container.values().length; i++) {
if (Container.values()[i].equals(this.container)) {
this.container = Container.values()[i+1];
}
}
}
public boolean add(int quantity) {
try {
this.setQuantity(this.quantity + quantity);
} catch (ExtendedException e) {
this.transferToLargerContainer();
this.setQuantity(this.quantity + quantity);
}
}
public String toString() {
return "Thing [name=" + this.name + ", quantity=" + this.quantity + ", container=" + this.container.toString() + "]";
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment