diff --git a/.devcontainer.json b/.devcontainer.json new file mode 100644 index 0000000..bfbeb0d --- /dev/null +++ b/.devcontainer.json @@ -0,0 +1,3 @@ +{ + "image": "mcr.microsoft.com/devcontainers/java:21" +} \ No newline at end of file diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..add4f4e --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,5 @@ +{ + "recommendations": [ + "vscjava.vscode-java-pack" + ] +} \ No newline at end of file diff --git a/BeerBottle.java b/BeerBottle.java new file mode 100644 index 0000000..13ed078 --- /dev/null +++ b/BeerBottle.java @@ -0,0 +1,6 @@ +public class BeerBottle extends Bottle { + + public void chugALug() { + System.out.println("Ex und Hopp"); + } +} diff --git a/Bottle.java b/Bottle.java new file mode 100644 index 0000000..2c3e7c5 --- /dev/null +++ b/Bottle.java @@ -0,0 +1 @@ +public abstract class Bottle {} diff --git a/Crate.java b/Crate.java new file mode 100644 index 0000000..1e380b0 --- /dev/null +++ b/Crate.java @@ -0,0 +1,39 @@ +public class Crate { + + private Bottle box1; + private Bottle box2; + private Bottle box3; + private Bottle box4; + private Bottle box5; + private Bottle box6; + + public void insertBottle(Bottle bottle, int box) throws CrateIndexOutOfBoundsException { + if (box < 1 || box > 6) { + throw new CrateIndexOutOfBoundsException(); + } + + switch (box) { + case 1 -> box1 = bottle; + case 2 -> box2 = bottle; + case 3 -> box3 = bottle; + case 4 -> box4 = bottle; + case 5 -> box5 = bottle; + case 6 -> box6 = bottle; + } + } + + public Bottle takeBottle(int box) throws CrateIndexOutOfBoundsException { + if (box < 1 || box > 6) { + throw new CrateIndexOutOfBoundsException(); + } + + return switch (box) { + case 1 -> box1; + case 2 -> box2; + case 3 -> box3; + case 4 -> box4; + case 5 -> box5; + default -> box6; + }; + } +} diff --git a/CrateIndexOutOfBoundsException.java b/CrateIndexOutOfBoundsException.java new file mode 100644 index 0000000..cc188cb --- /dev/null +++ b/CrateIndexOutOfBoundsException.java @@ -0,0 +1,8 @@ +public class CrateIndexOutOfBoundsException extends Exception { + + private static final long serialVersionUID = 1L; + + public CrateIndexOutOfBoundsException() { + super("Der angegebene Index befindet sich ausserhalb der Grenzen"); + } +} diff --git a/Exercise.java b/Exercise.java index 3c092f9..36de0b3 100644 --- a/Exercise.java +++ b/Exercise.java @@ -1,6 +1,21 @@ public class Exercise { public static void main(String[] args) { - // implement exercise here + Crate crate = new Crate(); + + try { + crate.insertBottle(new BeerBottle(), 1); + crate.insertBottle(new BeerBottle(), 2); + crate.insertBottle(new BeerBottle(), 3); + crate.insertBottle(new WineBottle(), 4); + crate.insertBottle(new WineBottle(), 5); + crate.insertBottle(new WineBottle(), 6); + + if (crate.takeBottle(3) instanceof BeerBottle beerBottle) { + beerBottle.chugALug(); + } + } catch (CrateIndexOutOfBoundsException e) { + System.err.println(e.getMessage()); + } } } diff --git a/WineBottle.java b/WineBottle.java new file mode 100644 index 0000000..67420ab --- /dev/null +++ b/WineBottle.java @@ -0,0 +1 @@ +public class WineBottle extends Bottle {}