Skip to content

Commit

Permalink
Geldbetrag uses now Waehrung as attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
oboehm committed Aug 4, 2018
1 parent 3246b70 commit b36db5b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/main/java/de/jfachwert/bank/Geldbetrag.java
Expand Up @@ -66,7 +66,7 @@ public class Geldbetrag implements MonetaryAmount, Fachwert {
public static final Geldbetrag MAX_VALUE = new Geldbetrag(BigDecimal.valueOf(Long.MAX_VALUE));

private final BigDecimal betrag;
private final CurrencyUnit currency;
private final Waehrung waehrung;

/**
* Erzeugt einen Geldbetrag in der aktuellen Landeswaehrung.
Expand Down Expand Up @@ -133,7 +133,7 @@ public Geldbetrag(Number betrag, Currency currency) {
*/
public Geldbetrag(Number betrag, CurrencyUnit currency) {
this.betrag = validate(toBigDecimal(betrag), currency);
this.currency = currency;
this.waehrung = Waehrung.of(currency);
}

/**
Expand Down Expand Up @@ -372,7 +372,7 @@ public MonetaryContext getContext() {
* value, the algorithmic implementation as well as the current {@link MonetaryContext}.
* <p>
* This method is used for creating a new amount result after having done calculations that are
* not directly mappable to the default monetary arithmetics, e.g. currency conversion.
* not directly mappable to the default monetary arithmetics, e.g. waehrung conversion.
*
* @return the new {@code MonetaryAmountFactory} with the given {@link MonetaryAmount} as its
* default values.
Expand Down Expand Up @@ -557,7 +557,7 @@ public MonetaryAmount multiply(double multiplicand) {
@Override
public MonetaryAmount multiply(Number multiplicand) {
BigDecimal multiplied = betrag.multiply(toBigDecimal(multiplicand));
return Geldbetrag.valueOf(limitScale(multiplied), currency);
return Geldbetrag.valueOf(limitScale(multiplied), waehrung);
}

/**
Expand Down Expand Up @@ -608,7 +608,9 @@ public MonetaryAmount divide(double divisor) {
*/
@Override
public Geldbetrag divide(Number divisor) {
return Geldbetrag.valueOf(betrag.setScale(4, RoundingMode.HALF_UP).divide(toBigDecimal(divisor), RoundingMode.HALF_UP), currency);
return Geldbetrag.valueOf(betrag.setScale(4, RoundingMode.HALF_UP).divide(toBigDecimal(divisor), RoundingMode.HALF_UP),

waehrung);
}

/**
Expand Down Expand Up @@ -831,7 +833,7 @@ public Geldbetrag abs() {
*/
@Override
public Geldbetrag negate() {
return valueOf(betrag.negate(), currency);
return valueOf(betrag.negate(), waehrung);
}

/**
Expand Down Expand Up @@ -888,7 +890,7 @@ public int compareTo(MonetaryAmount other) {
* andere ist, sonst positive Zahl.
*/
public int compareTo(Number other) {
return this.compareTo(Geldbetrag.valueOf(other, currency));
return this.compareTo(Geldbetrag.valueOf(other, waehrung));
}

/**
Expand All @@ -898,7 +900,7 @@ public int compareTo(Number other) {
*/
@Override
public CurrencyUnit getCurrency() {
return Monetary.getCurrency(currency.getCurrencyCode());
return Monetary.getCurrency(waehrung.getCurrencyCode());
}

/**
Expand Down Expand Up @@ -979,7 +981,7 @@ private void checkCurrency(MonetaryAmount other) {
*/
@Override
public String toString() {
return this.getNumber() + " " + currency;
return this.getNumber() + " " + waehrung.getSymbol();
}

}
32 changes: 32 additions & 0 deletions src/main/java/de/jfachwert/bank/Waehrung.java
Expand Up @@ -74,6 +74,29 @@ public static Waehrung of(Currency currency) {
return new Waehrung(currency);
}
}

/**
* Gibt die entsprechende Currency als Waehrung zurueck.
*
* @param currencyUnit CurrencyUnit
* @return Waehrung
*/
public static Waehrung of(CurrencyUnit currencyUnit) {
if (currencyUnit instanceof Waehrung) {
return (Waehrung) currencyUnit;
} else {
return of(currencyUnit.getCurrencyCode());
}
}
/**
* Gibt die entsprechende Currency als Waehrung zurueck.
*
* @param currency Waehrung, z.B. "EUR"
* @return Waehrung
*/
public static Waehrung of(String currency) {
return of(Currency.getInstance(currency));
}

/**
* Validiert den uebergebenen Waehrungscode.
Expand Down Expand Up @@ -125,6 +148,15 @@ public CurrencyContext getContext() {
throw new UnsupportedOperationException("not yet implemented");
}

/**
* Liefert das Waehrungssymbol.
*
* @return z.B. "$"
*/
public String getSymbol() {
return getCode().getSymbol();
}

/**
* Zum Vergleich wird der Waehrungscode herangezogen und alphabetisch
* verglichen.
Expand Down

0 comments on commit b36db5b

Please sign in to comment.