From 45bffe50bbd0caa686818609d8fbe4bee0d9fa51 Mon Sep 17 00:00:00 2001 From: Ethan Robert <ethan.rbrt90@gmail.com> Date: Mon, 12 May 2025 10:27:59 +0200 Subject: [PATCH] tpQU-04::exo-boite --- src/tpQU/TP04/Container.java | 17 ++++++++++ src/tpQU/TP04/ExtendedException.java | 6 ++++ src/tpQU/TP04/Ting.java | 48 ++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 src/tpQU/TP04/Container.java create mode 100644 src/tpQU/TP04/ExtendedException.java create mode 100644 src/tpQU/TP04/Ting.java diff --git a/src/tpQU/TP04/Container.java b/src/tpQU/TP04/Container.java new file mode 100644 index 0000000..79d294f --- /dev/null +++ b/src/tpQU/TP04/Container.java @@ -0,0 +1,17 @@ +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 diff --git a/src/tpQU/TP04/ExtendedException.java b/src/tpQU/TP04/ExtendedException.java new file mode 100644 index 0000000..743679d --- /dev/null +++ b/src/tpQU/TP04/ExtendedException.java @@ -0,0 +1,6 @@ +package tpQU.tp04; + +public class ExtendedException extends Exception { + public ExtendedException () {} + public ExtendedException (String msg) {super(msg);} +} \ No newline at end of file diff --git a/src/tpQU/TP04/Ting.java b/src/tpQU/TP04/Ting.java new file mode 100644 index 0000000..3364479 --- /dev/null +++ b/src/tpQU/TP04/Ting.java @@ -0,0 +1,48 @@ +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 -- GitLab