Skip to content

Commit 8930ead

Browse files
committed
Exercise Optionals 03
0 parents  commit 8930ead

File tree

8 files changed

+118
-0
lines changed

8 files changed

+118
-0
lines changed

.github/workflows/format.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: format and commit all java files
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- 'exercises/**'
8+
- 'solutions/**'
9+
10+
jobs:
11+
formatting:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v2
15+
- uses: actions/setup-java@v3
16+
with:
17+
java-version: '17'
18+
distribution: 'temurin'
19+
- uses: axel-op/googlejavaformat-action@v3

.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Compiled class file
2+
*.class
3+
4+
# Log file
5+
*.log
6+
7+
# BlueJ files
8+
*.ctxt
9+
10+
# Mobile Tools for Java (J2ME)
11+
.mtj.tmp/
12+
13+
# Package Files #
14+
*.jar
15+
*.war
16+
*.nar
17+
*.ear
18+
*.zip
19+
*.tar.gz
20+
*.rar
21+
22+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23+
hs_err_pid*

BeerBottle.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public class BeerBottle extends Bottle {
2+
3+
public void chugALug() {
4+
System.out.println("Ex und Hopp");
5+
}
6+
}

Bottle.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
public abstract class Bottle {}

Crate.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
public class Crate<T> {
2+
3+
private T box1;
4+
private T box2;
5+
private T box3;
6+
private T box4;
7+
private T box5;
8+
private T box6;
9+
10+
public void insertBottle(T bottle, int box) throws CrateIndexOutOfBoundsException {
11+
if (box < 1 || box > 6) {
12+
throw new CrateIndexOutOfBoundsException();
13+
}
14+
15+
switch (box) {
16+
case 1 -> box1 = bottle;
17+
case 2 -> box2 = bottle;
18+
case 3 -> box3 = bottle;
19+
case 4 -> box4 = bottle;
20+
case 5 -> box5 = bottle;
21+
case 6 -> box6 = bottle;
22+
}
23+
}
24+
25+
public T takeBottle(int box) throws CrateIndexOutOfBoundsException {
26+
if (box < 1 || box > 6) {
27+
throw new CrateIndexOutOfBoundsException();
28+
}
29+
30+
return switch (box) {
31+
case 1 -> box1;
32+
case 2 -> box2;
33+
case 3 -> box3;
34+
case 4 -> box4;
35+
case 5 -> box5;
36+
default -> box6;
37+
};
38+
}
39+
}

CrateIndexOutOfBoundsException.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
public class CrateIndexOutOfBoundsException extends Exception {
2+
3+
private static final long serialVersionUID = 1L;
4+
5+
public CrateIndexOutOfBoundsException() {
6+
super("Der angegebene Index befindet sich ausserhalb der Grenzen");
7+
}
8+
}

Exercise.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
public class Exercise {
2+
3+
public static void main(String[] args) {
4+
Crate<Bottle> crate = new Crate<Bottle>();
5+
6+
try {
7+
crate.insertBottle(new BeerBottle(), 1);
8+
crate.insertBottle(new BeerBottle(), 2);
9+
crate.insertBottle(new BeerBottle(), 3);
10+
crate.insertBottle(new WineBottle(), 4);
11+
crate.insertBottle(new WineBottle(), 5);
12+
crate.insertBottle(new WineBottle(), 6);
13+
14+
if (crate.takeBottle(3) instanceof BeerBottle beerBottle) {
15+
beerBottle.chugALug();
16+
}
17+
} catch (CrateIndexOutOfBoundsException e) {
18+
System.err.println(e.getMessage());
19+
}
20+
}
21+
}

WineBottle.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
public class WineBottle extends Bottle {}

0 commit comments

Comments
 (0)