From 8854ebbc0a25494f195982772e1ad99781ea9a9b Mon Sep 17 00:00:00 2001 From: Axel Tetzlaff Date: Sun, 28 Jun 2015 16:29:28 +0200 Subject: [PATCH] =?UTF-8?q?Summierung=20von=20Betr=C3=A4gen=20unterschiedl?= =?UTF-8?q?icher=20W=C3=A4hrungen=20verbieten?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DerPreisIstHeiss.java | 4 +++- README.md | 9 ++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/DerPreisIstHeiss.java b/DerPreisIstHeiss.java index 5b5e21c..b3906e4 100644 --- a/DerPreisIstHeiss.java +++ b/DerPreisIstHeiss.java @@ -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); } } @@ -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); } diff --git a/README.md b/README.md index c6e19cd..871654f 100644 --- a/README.md +++ b/README.md @@ -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); } } @@ -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); } @@ -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) ```