diff --git a/src/main/java/de/jfachwert/bank/Geldbetrag.java b/src/main/java/de/jfachwert/bank/Geldbetrag.java index fa9ba3bf..04457fce 100644 --- a/src/main/java/de/jfachwert/bank/Geldbetrag.java +++ b/src/main/java/de/jfachwert/bank/Geldbetrag.java @@ -113,11 +113,48 @@ public Geldbetrag(Number betrag, Currency currency) { * Wandelt den angegebenen MonetaryAmount in einen Geldbetrag um. Um die * Anzahl von Objekten gering zu halten, wird nur dann tatsaechlich eine * neues Objekt erzeugt, wenn es sich nicht vermeiden laesst. + *

+ * In Anlehnung an {@link BigDecimal} heisst die Methode "valueOf" und + * nicht "of". + *

* * @param other the other - * @return the geldbetrag + * @return ein Geldbetrag */ - public static Geldbetrag of(MonetaryAmount other) { + public static Geldbetrag valueOf(String other) { + return valueOf(new Geldbetrag(other)); + } + + /** + * Wandelt den angegebenen MonetaryAmount in einen Geldbetrag um. Um die + * Anzahl von Objekten gering zu halten, wird nur dann tatsaechlich eine + * neues Objekt erzeugt, wenn es sich nicht vermeiden laesst. + *

+ * In Anlehnung an {@link BigDecimal} heisst die Methode "valueOf" und + * nicht "of". + *

+ * + * @param value Wert des andere Geldbetrags + * @param currency Waehrung des anderen Geldbetrags + * @return ein Geldbetrag + */ + public static Geldbetrag valueOf(Number value, Currency currency) { + return valueOf(new Geldbetrag(value, currency)); + } + + /** + * Wandelt den angegebenen MonetaryAmount in einen Geldbetrag um. Um die + * Anzahl von Objekten gering zu halten, wird nur dann tatsaechlich eine + * neues Objekt erzeugt, wenn es sich nicht vermeiden laesst. + *

+ * In Anlehnung an {@link BigDecimal} heisst die Methode "valueOf" und + * nicht "of". + *

+ * + * @param other the other + * @return ein Geldbetrag + */ + public static Geldbetrag valueOf(MonetaryAmount other) { if (other instanceof Geldbetrag) { return (Geldbetrag) other; } @@ -198,7 +235,7 @@ public Geldbetrag withWaehrung(Currency currency) { */ @Override public MonetaryContext getContext() { - return null; + throw new UnsupportedOperationException("not yet implemented"); } /** @@ -214,7 +251,7 @@ public MonetaryContext getContext() { */ @Override public MonetaryAmountFactory getFactory() { - return null; + throw new UnsupportedOperationException("not yet implemented"); } /** @@ -301,7 +338,7 @@ private static BigDecimal toBigDecimal(NumberValue value) { */ @Override public int signum() { - return 0; + throw new UnsupportedOperationException("not yet implemented"); } /** @@ -321,7 +358,7 @@ public Geldbetrag add(MonetaryAmount other) { return this; } if (this.isEqualTo(Geldbetrag.ZERO)) { - return Geldbetrag.of(other); + return Geldbetrag.valueOf(other); } BigDecimal n = other.getNumber().numberValue(BigDecimal.class); return new Geldbetrag(betrag.add(n)); @@ -339,7 +376,7 @@ public Geldbetrag add(MonetaryAmount other) { */ @Override public MonetaryAmount subtract(MonetaryAmount amount) { - return null; + return add(amount.negate()); } /** @@ -354,7 +391,7 @@ public MonetaryAmount subtract(MonetaryAmount amount) { */ @Override public MonetaryAmount multiply(long multiplicand) { - return null; + throw new UnsupportedOperationException("not yet implemented"); } /** @@ -374,7 +411,7 @@ public MonetaryAmount multiply(long multiplicand) { */ @Override public MonetaryAmount multiply(double multiplicand) { - return null; + throw new UnsupportedOperationException("not yet implemented"); } /** @@ -391,7 +428,7 @@ public MonetaryAmount multiply(double multiplicand) { */ @Override public MonetaryAmount multiply(Number multiplicand) { - return null; + throw new UnsupportedOperationException("not yet implemented"); } /** @@ -408,7 +445,7 @@ public MonetaryAmount multiply(Number multiplicand) { */ @Override public MonetaryAmount divide(long divisor) { - return null; + throw new UnsupportedOperationException("not yet implemented"); } /** @@ -425,7 +462,7 @@ public MonetaryAmount divide(long divisor) { */ @Override public MonetaryAmount divide(double divisor) { - return null; + throw new UnsupportedOperationException("not yet implemented"); } /** @@ -442,7 +479,7 @@ public MonetaryAmount divide(double divisor) { */ @Override public MonetaryAmount divide(Number divisor) { - return null; + throw new UnsupportedOperationException("not yet implemented"); } /** @@ -461,7 +498,7 @@ public MonetaryAmount divide(Number divisor) { */ @Override public MonetaryAmount remainder(long divisor) { - return null; + throw new UnsupportedOperationException("not yet implemented"); } /** @@ -480,7 +517,7 @@ public MonetaryAmount remainder(long divisor) { */ @Override public MonetaryAmount remainder(double divisor) { - return null; + throw new UnsupportedOperationException("not yet implemented"); } /** @@ -499,7 +536,7 @@ public MonetaryAmount remainder(double divisor) { */ @Override public MonetaryAmount remainder(Number divisor) { - return null; + throw new UnsupportedOperationException("not yet implemented"); } /** @@ -525,7 +562,7 @@ public MonetaryAmount remainder(Number divisor) { */ @Override public MonetaryAmount[] divideAndRemainder(long divisor) { - return new MonetaryAmount[0]; + throw new UnsupportedOperationException("not yet implemented"); } /** @@ -551,7 +588,7 @@ public MonetaryAmount[] divideAndRemainder(long divisor) { */ @Override public MonetaryAmount[] divideAndRemainder(double divisor) { - return new MonetaryAmount[0]; + throw new UnsupportedOperationException("not yet implemented"); } /** @@ -577,7 +614,7 @@ public MonetaryAmount[] divideAndRemainder(double divisor) { */ @Override public MonetaryAmount[] divideAndRemainder(Number divisor) { - return new MonetaryAmount[0]; + throw new UnsupportedOperationException("not yet implemented"); } /** @@ -593,7 +630,7 @@ public MonetaryAmount[] divideAndRemainder(Number divisor) { */ @Override public MonetaryAmount divideToIntegralValue(long divisor) { - return null; + throw new UnsupportedOperationException("not yet implemented"); } /** @@ -608,7 +645,7 @@ public MonetaryAmount divideToIntegralValue(long divisor) { */ @Override public MonetaryAmount divideToIntegralValue(double divisor) { - return null; + throw new UnsupportedOperationException("not yet implemented"); } /** @@ -624,7 +661,7 @@ public MonetaryAmount divideToIntegralValue(double divisor) { */ @Override public MonetaryAmount divideToIntegralValue(Number divisor) { - return null; + throw new UnsupportedOperationException("not yet implemented"); } /** @@ -639,7 +676,7 @@ public MonetaryAmount divideToIntegralValue(Number divisor) { */ @Override public MonetaryAmount scaleByPowerOfTen(int power) { - return null; + throw new UnsupportedOperationException("not yet implemented"); } /** @@ -650,7 +687,7 @@ public MonetaryAmount scaleByPowerOfTen(int power) { */ @Override public MonetaryAmount abs() { - return null; + throw new UnsupportedOperationException("not yet implemented"); } /** @@ -661,7 +698,7 @@ public MonetaryAmount abs() { */ @Override public MonetaryAmount negate() { - return null; + return valueOf(betrag.negate(), currency); } /** @@ -674,7 +711,7 @@ public MonetaryAmount negate() { */ @Override public MonetaryAmount plus() { - return null; + throw new UnsupportedOperationException("not yet implemented"); } /** @@ -688,50 +725,23 @@ public MonetaryAmount plus() { */ @Override public MonetaryAmount stripTrailingZeros() { - return null; + throw new UnsupportedOperationException("not yet implemented"); } /** + * Vergleicht die Zahlenwerter der beiden Geldbetraege. Aber nur, wenn es + * sich um die gleiche Waehrung handelt. Sonst wird eine + * {@link MonetaryException} ausgeloest. * Compares this object with the specified object for order. Returns a - * negative integer, zero, or a positive integer as this object is less - * than, equal to, or greater than the specified object. - * - *

The implementor must ensure sgn(x.compareTo(y)) == - * -sgn(y.compareTo(x)) for all x and y. (This - * implies that x.compareTo(y) must throw an exception iff - * y.compareTo(x) throws an exception.) - * - *

The implementor must also ensure that the relation is transitive: - * (x.compareTo(y)>0 && y.compareTo(z)>0) implies - * x.compareTo(z)>0. - * - *

Finally, the implementor must ensure that x.compareTo(y)==0 - * implies that sgn(x.compareTo(z)) == sgn(y.compareTo(z)), for - * all z. - * - *

It is strongly recommended, but not strictly required that - * (x.compareTo(y)==0) == (x.equals(y)). Generally speaking, any - * class that implements the Comparable interface and violates - * this condition should clearly indicate this fact. The recommended - * language is "Note: this class has a natural ordering that is - * inconsistent with equals." - * - *

In the foregoing description, the notation - * sgn(expression) designates the mathematical - * signum function, which is defined to return one of -1, - * 0, or 1 according to whether the value of - * expression is negative, zero or positive. - * - * @param o the object to be compared. - * @return a negative integer, zero, or a positive integer as this object - * is less than, equal to, or greater than the specified object. - * @throws NullPointerException if the specified object is null - * @throws ClassCastException if the specified object's type prevents it - * from being compared to this object. - */ - @Override - public int compareTo(MonetaryAmount o) { - return 0; + * + * @param other der andere Geldbetrag + * @return 0 bei Gleicheit; negative Zahl, wenn dieser Geldbetrag kleiner + * als der andere ist; sonst positive Zahl. + */ + @Override + public int compareTo(MonetaryAmount other) { + checkCurrency(other); + return betrag.compareTo(other.getNumber().numberValue(BigDecimal.class)); } /** diff --git a/src/test/java/de/jfachwert/bank/GeldbetragTest.java b/src/test/java/de/jfachwert/bank/GeldbetragTest.java index 8c5b20b2..1e3afbd4 100644 --- a/src/test/java/de/jfachwert/bank/GeldbetragTest.java +++ b/src/test/java/de/jfachwert/bank/GeldbetragTest.java @@ -48,6 +48,14 @@ protected Geldbetrag createFachwert() { return new Geldbetrag(BigDecimal.ONE); } + /** + * Illegale Betraege sollte nicht akzeptiert werden. + */ + @Test(expected = ValidationException.class) + public void testInvalidGeldbetrag() { + new Geldbetrag("falscher Fuffzger"); + } + /** * Rundungsdifferenzen beim Vergleich im 1/10-Cent-Bereich sollten keine * Rolle fuer den Vergleich spielen. @@ -119,13 +127,19 @@ public void testAddZeroSameBetrag() { @Test public void testAddOperationLeadsToNewObject() { - Geldbetrag base = new Geldbetrag(11L); + Geldbetrag base = new Geldbetrag("12.3456"); Geldbetrag one = new Geldbetrag(1L); Geldbetrag sum = base.add(one); assertNotSame(base, sum); assertNotSame(one, sum); + assertEquals(Geldbetrag.valueOf("13.3456"), sum); } - + + /** + * Laut API-Doc sollte bei unterschiedlichen Waehrungen eine + * {@link MonetaryException} geworfen werden. + * + */ @Test(expected = MonetaryException.class) public void testAddWithDifferentCurrency() { Geldbetrag oneEuro = new Geldbetrag(1.0).withWaehrung("EUR"); @@ -154,13 +168,15 @@ public void testPrecisionOfFiveZerosAfterComma() { public void testPrecisionOfZeroInFifthAfterCommaPosition() { new Geldbetrag(new BigDecimal("0.00010")); } - /** - * Illegale Betraege sollte nicht akzeptiert werden. + * Test-Methode fuer {@link Geldbetrag#subtract(MonetaryAmount)}. */ - @Test(expected = ValidationException.class) - public void testInvalidGeldbetrag() { - new Geldbetrag("falscher Fuffzger"); + + @Test + public void testSubtract() { + MonetaryAmount guthaben = new Geldbetrag(42); + MonetaryAmount schulden = new Geldbetrag(50); + assertEquals(new Geldbetrag(-8), guthaben.subtract(schulden)); } }