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"
]
}
30 changes: 30 additions & 0 deletions Barrel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
public class Barrel {

private int capacity;
private int fluidLevel;

public Barrel(int capacity) {
this.capacity = capacity;
fluidLevel = 0;
}

public int getCapacity() {
return capacity;
}

public int getFluidLevel() {
return fluidLevel;
}

public void addFluid(int value) throws BarrelOverflowException {
if (fluidLevel + value > capacity) {
fluidLevel = capacity;
throw new BarrelOverflowException();
}
fluidLevel += value;
}

public String toString() {
return "Barrel [capacity=" + capacity + "] [fluidlevel=" + fluidLevel + "]";
}
}
6 changes: 6 additions & 0 deletions BarrelOverflowException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class BarrelOverflowException extends Exception {

public BarrelOverflowException() {
super("Das war der Tropfen, der das Fass zum Ueberlaufen gebracht hat");
}
}
27 changes: 26 additions & 1 deletion Exercise.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
import java.util.Scanner;

public class Exercise {

public static void main(String[] args) {
// implement exercise here
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);

System.out.print("Gib bitte die Kapazitaet des Fasses ein: ");
int capacity = scanner.nextInt();

Barrel barrel = new Barrel(capacity);

boolean loop = true;
while (loop) {
try {
System.out.print("Gib bitte die Menge der hinzuzufuegenden Fluessigkeit ein: ");
int value = scanner.nextInt();
if (value <= 0) {
loop = false;
} else {
barrel.addFluid(value);
System.out.println(barrel.toString());
}
} catch (BarrelOverflowException e) {
System.err.println(e.getMessage());
loop = false;
}
}
}
}
Loading