Skip to content

Commit

Permalink
subtract(..) implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
oboehm committed Jul 23, 2018
1 parent c30b6b5 commit a91adfb
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 73 deletions.
142 changes: 76 additions & 66 deletions src/main/java/de/jfachwert/bank/Geldbetrag.java
Expand Up @@ -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.
* <p>
* In Anlehnung an {@link BigDecimal} heisst die Methode "valueOf" und
* nicht "of".
* </p>
*
* @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.
* <p>
* In Anlehnung an {@link BigDecimal} heisst die Methode "valueOf" und
* nicht "of".
* </p>
*
* @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.
* <p>
* In Anlehnung an {@link BigDecimal} heisst die Methode "valueOf" und
* nicht "of".
* </p>
*
* @param other the other
* @return ein Geldbetrag
*/
public static Geldbetrag valueOf(MonetaryAmount other) {
if (other instanceof Geldbetrag) {
return (Geldbetrag) other;
}
Expand Down Expand Up @@ -198,7 +235,7 @@ public Geldbetrag withWaehrung(Currency currency) {
*/
@Override
public MonetaryContext getContext() {
return null;
throw new UnsupportedOperationException("not yet implemented");
}

/**
Expand All @@ -214,7 +251,7 @@ public MonetaryContext getContext() {
*/
@Override
public MonetaryAmountFactory<? extends MonetaryAmount> getFactory() {
return null;
throw new UnsupportedOperationException("not yet implemented");
}

/**
Expand Down Expand Up @@ -301,7 +338,7 @@ private static BigDecimal toBigDecimal(NumberValue value) {
*/
@Override
public int signum() {
return 0;
throw new UnsupportedOperationException("not yet implemented");
}

/**
Expand All @@ -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));
Expand All @@ -339,7 +376,7 @@ public Geldbetrag add(MonetaryAmount other) {
*/
@Override
public MonetaryAmount subtract(MonetaryAmount amount) {
return null;
return add(amount.negate());
}

/**
Expand All @@ -354,7 +391,7 @@ public MonetaryAmount subtract(MonetaryAmount amount) {
*/
@Override
public MonetaryAmount multiply(long multiplicand) {
return null;
throw new UnsupportedOperationException("not yet implemented");
}

/**
Expand All @@ -374,7 +411,7 @@ public MonetaryAmount multiply(long multiplicand) {
*/
@Override
public MonetaryAmount multiply(double multiplicand) {
return null;
throw new UnsupportedOperationException("not yet implemented");
}

/**
Expand All @@ -391,7 +428,7 @@ public MonetaryAmount multiply(double multiplicand) {
*/
@Override
public MonetaryAmount multiply(Number multiplicand) {
return null;
throw new UnsupportedOperationException("not yet implemented");
}

/**
Expand All @@ -408,7 +445,7 @@ public MonetaryAmount multiply(Number multiplicand) {
*/
@Override
public MonetaryAmount divide(long divisor) {
return null;
throw new UnsupportedOperationException("not yet implemented");
}

/**
Expand All @@ -425,7 +462,7 @@ public MonetaryAmount divide(long divisor) {
*/
@Override
public MonetaryAmount divide(double divisor) {
return null;
throw new UnsupportedOperationException("not yet implemented");
}

/**
Expand All @@ -442,7 +479,7 @@ public MonetaryAmount divide(double divisor) {
*/
@Override
public MonetaryAmount divide(Number divisor) {
return null;
throw new UnsupportedOperationException("not yet implemented");
}

/**
Expand All @@ -461,7 +498,7 @@ public MonetaryAmount divide(Number divisor) {
*/
@Override
public MonetaryAmount remainder(long divisor) {
return null;
throw new UnsupportedOperationException("not yet implemented");
}

/**
Expand All @@ -480,7 +517,7 @@ public MonetaryAmount remainder(long divisor) {
*/
@Override
public MonetaryAmount remainder(double divisor) {
return null;
throw new UnsupportedOperationException("not yet implemented");
}

/**
Expand All @@ -499,7 +536,7 @@ public MonetaryAmount remainder(double divisor) {
*/
@Override
public MonetaryAmount remainder(Number divisor) {
return null;
throw new UnsupportedOperationException("not yet implemented");
}

/**
Expand All @@ -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");
}

/**
Expand All @@ -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");
}

/**
Expand All @@ -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");
}

/**
Expand All @@ -593,7 +630,7 @@ public MonetaryAmount[] divideAndRemainder(Number divisor) {
*/
@Override
public MonetaryAmount divideToIntegralValue(long divisor) {
return null;
throw new UnsupportedOperationException("not yet implemented");
}

/**
Expand All @@ -608,7 +645,7 @@ public MonetaryAmount divideToIntegralValue(long divisor) {
*/
@Override
public MonetaryAmount divideToIntegralValue(double divisor) {
return null;
throw new UnsupportedOperationException("not yet implemented");
}

/**
Expand All @@ -624,7 +661,7 @@ public MonetaryAmount divideToIntegralValue(double divisor) {
*/
@Override
public MonetaryAmount divideToIntegralValue(Number divisor) {
return null;
throw new UnsupportedOperationException("not yet implemented");
}

/**
Expand All @@ -639,7 +676,7 @@ public MonetaryAmount divideToIntegralValue(Number divisor) {
*/
@Override
public MonetaryAmount scaleByPowerOfTen(int power) {
return null;
throw new UnsupportedOperationException("not yet implemented");
}

/**
Expand All @@ -650,7 +687,7 @@ public MonetaryAmount scaleByPowerOfTen(int power) {
*/
@Override
public MonetaryAmount abs() {
return null;
throw new UnsupportedOperationException("not yet implemented");
}

/**
Expand All @@ -661,7 +698,7 @@ public MonetaryAmount abs() {
*/
@Override
public MonetaryAmount negate() {
return null;
return valueOf(betrag.negate(), currency);
}

/**
Expand All @@ -674,7 +711,7 @@ public MonetaryAmount negate() {
*/
@Override
public MonetaryAmount plus() {
return null;
throw new UnsupportedOperationException("not yet implemented");
}

/**
Expand All @@ -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.
*
* <p>The implementor must ensure <tt>sgn(x.compareTo(y)) ==
* -sgn(y.compareTo(x))</tt> for all <tt>x</tt> and <tt>y</tt>. (This
* implies that <tt>x.compareTo(y)</tt> must throw an exception iff
* <tt>y.compareTo(x)</tt> throws an exception.)
*
* <p>The implementor must also ensure that the relation is transitive:
* <tt>(x.compareTo(y)&gt;0 &amp;&amp; y.compareTo(z)&gt;0)</tt> implies
* <tt>x.compareTo(z)&gt;0</tt>.
*
* <p>Finally, the implementor must ensure that <tt>x.compareTo(y)==0</tt>
* implies that <tt>sgn(x.compareTo(z)) == sgn(y.compareTo(z))</tt>, for
* all <tt>z</tt>.
*
* <p>It is strongly recommended, but <i>not</i> strictly required that
* <tt>(x.compareTo(y)==0) == (x.equals(y))</tt>. Generally speaking, any
* class that implements the <tt>Comparable</tt> 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."
*
* <p>In the foregoing description, the notation
* <tt>sgn(</tt><i>expression</i><tt>)</tt> designates the mathematical
* <i>signum</i> function, which is defined to return one of <tt>-1</tt>,
* <tt>0</tt>, or <tt>1</tt> according to whether the value of
* <i>expression</i> 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));
}

/**
Expand Down
30 changes: 23 additions & 7 deletions src/test/java/de/jfachwert/bank/GeldbetragTest.java
Expand Up @@ -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.
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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));
}

}

0 comments on commit a91adfb

Please sign in to comment.