Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"image": "mcr.microsoft.com/devcontainers/java:21"
}
5 changes: 5 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recommendations": [
"vscjava.vscode-java-pack"
]
}
6 changes: 6 additions & 0 deletions BeerBottle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class BeerBottle extends Bottle {

public void chugALug() {
System.out.println("Ex und Hopp");
}
}
1 change: 1 addition & 0 deletions Bottle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public abstract class Bottle {}
39 changes: 39 additions & 0 deletions Crate.java
Original file line number Diff line number Diff line change
@@ -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;
};
}
}
8 changes: 8 additions & 0 deletions CrateIndexOutOfBoundsException.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
17 changes: 16 additions & 1 deletion Exercise.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
1 change: 1 addition & 0 deletions WineBottle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public class WineBottle extends Bottle {}
Loading