Skip to content

Commit

Permalink
missing divide and remainder methods implemented
Browse files Browse the repository at this point in the history
TCK: still 72 tests failing
  • Loading branch information
oboehm committed Aug 6, 2018
1 parent 50f7027 commit de52800
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 83 deletions.
132 changes: 49 additions & 83 deletions src/main/java/de/jfachwert/bank/Geldbetrag.java
Expand Up @@ -630,7 +630,7 @@ public Geldbetrag divide(Number divisor) {
*/
@Override
public Geldbetrag remainder(long divisor) {
throw new UnsupportedOperationException("not yet implemented");
return remainder(BigDecimal.valueOf(divisor));
}

/**
Expand All @@ -649,7 +649,7 @@ public Geldbetrag remainder(long divisor) {
*/
@Override
public Geldbetrag remainder(double divisor) {
throw new UnsupportedOperationException("not yet implemented");
return remainder(BigDecimal.valueOf(divisor));
}

/**
Expand All @@ -668,132 +668,98 @@ public Geldbetrag remainder(double divisor) {
*/
@Override
public Geldbetrag remainder(Number divisor) {
throw new UnsupportedOperationException("not yet implemented");
return Geldbetrag.valueOf(betrag.remainder(toBigDecimal(divisor)), waehrung);
}

/**
* Returns a two-element {@code MonetaryAmount} array containing the result of
* {@code divideToIntegralValue} followed by the result of {@code remainder} on the two
* operands.
* <p>
* <p>
* Note that if both the integer quotient and remainder are needed, this method is faster than
* using the {@code divideToIntegralValue} and {@code remainder} methods separately because the
* division need only be carried out once.
*
* @param divisor value by which this {@code MonetaryAmount} is to be divided, and the remainder
* computed.
* @return a two element {@code MonetaryAmount} array: the quotient (the result of
* {@code divideToIntegralValue}) is the initial element and the remainder is the final
* element.
* @throws ArithmeticException if {@code divisor==0}, or if the result exceeds the numeric capabilities of this
* implementation class, i.e. the {@link MonetaryContext} cannot be adapted as
* required.
* Liefert ein zwei-elementiges {@code Geldbatrag}-Array mit dem Ergebnis
* {@code divideToIntegralValue} und{@code remainder}.
*
* @param divisor Teiler
* @return ein zwei-elementiges {@code Geldbatrag}-Array
* @throws ArithmeticException bei {@code divisor==0}
* @see #divideToIntegralValue(long)
* @see #remainder(long)
*/
@Override
public MonetaryAmount[] divideAndRemainder(long divisor) {
throw new UnsupportedOperationException("not yet implemented");
public Geldbetrag[] divideAndRemainder(long divisor) {
return divideAndRemainder(BigDecimal.valueOf(divisor));
}

/**
* Returns a two-element {@code MonetaryAmount} array containing the result of
* {@code divideToIntegralValue} followed by the result of {@code remainder} on the two
* operands.
* <p>
* <p>
* Note that if both the integer quotient and remainder are needed, this method is faster than
* using the {@code divideToIntegralValue} and {@code remainder} methods separately because the
* division need only be carried out once.
*
* @param divisor value by which this {@code MonetaryAmount} is to be divided, and the remainder
* computed.
* @return a two element {@code MonetaryAmount} array: the quotient (the result of
* {@code divideToIntegralValue}) is the initial element and the remainder is the final
* element.
* @throws ArithmeticException if {@code divisor==0}, or if the result exceeds the numeric capabilities of this
* implementation class, i.e. the {@link MonetaryContext} cannot be adapted as
* required.
* Liefert ein zwei-elementiges {@code Geldbatrag}-Array mit dem Ergebnis
* {@code divideToIntegralValue} und{@code remainder}.
*
* @param divisor Teiler
* @return ein zwei-elementiges {@code Geldbatrag}-Array
* @throws ArithmeticException bei {@code divisor==0}
* @see #divideToIntegralValue(double)
* @see #remainder(double)
*/
@Override
public MonetaryAmount[] divideAndRemainder(double divisor) {
throw new UnsupportedOperationException("not yet implemented");
public Geldbetrag[] divideAndRemainder(double divisor) {
return divideAndRemainder(BigDecimal.valueOf(divisor));
}

/**
* Returns a two-element {@code MonetaryAmount} array containing the result of
* {@code divideToIntegralValue} followed by the result of {@code remainder} on the two
* operands.
* <p>
* <p>
* Note that if both the integer quotient and remainder are needed, this method is faster than
* using the {@code divideToIntegralValue} and {@code remainder} methods separately because the
* division need only be carried out once.
*
* @param divisor value by which this {@code MonetaryAmount} is to be divided, and the remainder
* computed.
* @return a two element {@code MonetaryAmount} array: the quotient (the result of
* {@code divideToIntegralValue}) is the initial element and the remainder is the final
* element.
* @throws ArithmeticException if {@code divisor==0}, or if the result exceeds the numeric capabilities of this
* implementation class, i.e. the {@link MonetaryContext} cannot be adapted as
* required.
* Liefert ein zwei-elementiges {@code Geldbatrag}-Array mit dem Ergebnis
* {@code divideToIntegralValue} und{@code remainder}.
*
* @param divisor Teiler
* @return ein zwei-elementiges {@code Geldbatrag}-Array
* @throws ArithmeticException bei {@code divisor==0}
* @see #divideToIntegralValue(Number)
* @see #remainder(Number)
*/
@Override
public MonetaryAmount[] divideAndRemainder(Number divisor) {
throw new UnsupportedOperationException("not yet implemented");
public Geldbetrag[] divideAndRemainder(Number divisor) {
BigDecimal[] numbers = betrag.divideAndRemainder(toBigDecimal(divisor));
Geldbetrag[] betraege = new Geldbetrag[2];
betraege[0] = Geldbetrag.valueOf(numbers[0], waehrung);
betraege[1] = Geldbetrag.valueOf(numbers[1], waehrung);
return betraege;
}

/**
* Returns a {@code MonetaryAmount} whose value is the integer part of the quotient
* <code>this / divisor</code> rounded down. The preferred scale of the result is
* <code>this.scale() -
* divisor.scale()</code>.
* Liefert den Integer-Teil des Quotienten <code>this / divisor</code>
* (abgerundet).
*
* @param divisor value by which this {@code BigDecimal} is to be divided.
* @return The integer part of {@code this / divisor}.
* @throws ArithmeticException if {@code divisor==0}
* @param divisor Teiler
* @return Integer-Teil von {@code this / divisor}.
* @throws ArithmeticException falls {@code divisor==0}
* @see BigDecimal#divideToIntegralValue(BigDecimal)
*/
@Override
public Geldbetrag divideToIntegralValue(long divisor) {
throw new UnsupportedOperationException("not yet implemented");
return divideToIntegralValue(BigDecimal.valueOf(divisor));
}

/**
* Returns a {@code MonetaryAmount} whose value is the integer part of the quotient
* <code>this / divisor</code> rounded down. The preferred scale of the result is
* <code>this.scale() - divisor.scale()</code>.
* Liefert den Integer-Teil des Quotienten <code>this / divisor</code>
* (abgerundet).
*
* @param divisor value by which this {@code BigDecimal} is to be divided.
* @return The integer part of {@code this / divisor}.
* @throws ArithmeticException if {@code divisor==0}
* @param divisor Teiler
* @return Integer-Teil von {@code this / divisor}.
* @throws ArithmeticException falls {@code divisor==0}
* @see BigDecimal#divideToIntegralValue(BigDecimal)
*/
@Override
public Geldbetrag divideToIntegralValue(double divisor) {
throw new UnsupportedOperationException("not yet implemented");
return divideToIntegralValue(BigDecimal.valueOf(divisor));
}

/**
* Returns a {@code MonetaryAmount} whose value is the integer part of the quotient
* <code>this / divisor</code> rounded down. The preferred scale of the result is
* <code>this.scale() -
* divisor.scale()</code>.
* Liefert den Integer-Teil des Quotienten <code>this / divisor</code>
* (abgerundet).
*
* @param divisor value by which this {@code BigDecimal} is to be divided.
* @return The integer part of {@code this / divisor}.
* @throws ArithmeticException if {@code divisor==0}
* @param divisor Teiler
* @return Integer-Teil von {@code this / divisor}.
* @throws ArithmeticException falls {@code divisor==0}
* @see BigDecimal#divideToIntegralValue(BigDecimal)
*/
@Override
public Geldbetrag divideToIntegralValue(Number divisor) {
throw new UnsupportedOperationException("not yet implemented");
return Geldbetrag.valueOf(betrag.divideToIntegralValue(toBigDecimal(divisor)), waehrung);
}

/**
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/de/jfachwert/bank/GeldbetragTest.java
Expand Up @@ -318,6 +318,17 @@ public void testDivideEinCent() {
assertEquals(einCent, halberCent.add(halberCent));
}

/**
* Testmethode fuer {@link Geldbetrag#divideAndRemainder(long)}.
*/
@Test
public void testDivideAndReminder() {
Geldbetrag betrag = Geldbetrag.valueOf("7 USD");
MonetaryAmount[] amounts = betrag.divideAndRemainder(2);
assertEquals(betrag.divideToIntegralValue(2), amounts[0]);
assertEquals(betrag.remainder(2), amounts[1]);
}

/**
* Test-Methode fuer {@link Geldbetrag#compareTo(MonetaryAmount)}.
*/
Expand Down

0 comments on commit de52800

Please sign in to comment.