Skip to content
This repository has been archived by the owner on Aug 5, 2023. It is now read-only.

Commit

Permalink
Summierung von Beträgen unterschiedlicher Währungen verbieten
Browse files Browse the repository at this point in the history
  • Loading branch information
axelerator committed Jun 28, 2015
1 parent 70ef544 commit 8854ebb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
4 changes: 3 additions & 1 deletion DerPreisIstHeiss.java
Expand Up @@ -25,6 +25,9 @@ public boolean equals(Object other) {
}

public Price sum(Price other) {
if (other.currency != this.currency) {
throw new RuntimeException("Währungen sind nicht gleich");
}
return new Price(this.amount + other.amount, this.currency);
}
}
Expand All @@ -37,7 +40,6 @@ public static void main(String[] args) {

Price deuro = oneEuro.sum(oneDollar);

System.out.println(oneEuro + " equals " + oneDollar + ": " + oneEuro.equals(oneDollar));
System.out.println(oneEuro + " + " + oneDollar + "= " + deuro);

}
Expand Down
9 changes: 6 additions & 3 deletions README.md
Expand Up @@ -26,6 +26,9 @@ class DerPreisIstHeiss {
}

public Price sum(Price other) {
if (other.currency != this.currency) {
throw new RuntimeException("Währungen sind nicht gleich");
}
return new Price(this.amount + other.amount, this.currency);
}
}
Expand All @@ -38,7 +41,6 @@ class DerPreisIstHeiss {

Price deuro = oneEuro.sum(oneDollar);

System.out.println(oneEuro + " equals " + oneDollar + ": " + oneEuro.equals(oneDollar));
System.out.println(oneEuro + " + " + oneDollar + "= " + deuro);

}
Expand All @@ -47,6 +49,7 @@ class DerPreisIstHeiss {
Ausgabe:
========
```
1.00 € equals 1.00 $: false
1.00 € + 1.00 $= 2.00 €
Exception in thread "main" java.lang.RuntimeException: Währungen sind nicht gleich
at DerPreisIstHeiss$Price.sum(DerPreisIstHeiss.java:29)
at DerPreisIstHeiss.main(DerPreisIstHeiss.java:41)
```

0 comments on commit 8854ebb

Please sign in to comment.