1.1 --- a/task1/solution06/src/org/apidesign/apifest08/currency/Convertor.java Sun Sep 28 14:12:38 2008 +0200
1.2 +++ b/task1/solution06/src/org/apidesign/apifest08/currency/Convertor.java Tue Sep 30 12:24:45 2008 +0200
1.3 @@ -1,23 +1,30 @@
1.4 package org.apidesign.apifest08.currency;
1.5
1.6 +import static org.apidesign.apifest08.currency.Assert.notNull;
1.7 +
1.8 import java.math.BigDecimal;
1.9 +import java.math.RoundingMode;
1.10 import java.util.Currency;
1.11
1.12 -public abstract class Convertor {
1.13 -
1.14 - /**
1.15 - * Converts an amount to another amount according to a given currency.
1.16 - *
1.17 - * @param from a source
1.18 - * @param toCurrency a target currency
1.19 - * @return a converted amount
1.20 - * @throws ConversionException if the conversion fails
1.21 - * @throws UnsupportedConversionException if the conversion between a given currencies is not supported.
1.22 - */
1.23 - public abstract Amount convert(Amount from, Currency toCurrency) throws ConversionException, UnsupportedConversionException;
1.24 +public final class Convertor {
1.25
1.26 + private final Currency first;
1.27 + private final Currency second;
1.28 + private final BigDecimal rateValue; // a rate between the first currency and the second currency
1.29 + public static final BigDecimal one = new BigDecimal(1);
1.30 +
1.31 + public Convertor(BigDecimal rateValue, Currency currencyFirst, Currency currencySecond) {
1.32 + notNull(currencyFirst, "currencyFirst");
1.33 + notNull(currencySecond, "currencySecond");
1.34 + notNull(rateValue, "rateValue");
1.35 +
1.36 + this.rateValue = rateValue;
1.37 + this.first = currencyFirst;
1.38 + this.second = currencySecond;
1.39 + }
1.40 +
1.41 /**
1.42 - * Converts an amount value between two currencies.
1.43 + * Converts an amount value between the two currencies of this converter.
1.44 *
1.45 * @param amount an amount
1.46 * @param fromCurrency an amount currency
1.47 @@ -27,6 +34,31 @@
1.48 * @throws ConversionException if the conversion fails
1.49 * @throws UnsupportedConversionException if the conversion between a given currencies is not supported.
1.50 */
1.51 - public abstract Amount convert(BigDecimal amount, Currency fromCurrency, Currency toCurrency) throws ConversionException, UnsupportedConversionException;
1.52 + public Amount convert(BigDecimal amount, Currency fromCurrency, Currency toCurrency) throws ConversionException {
1.53 + notNull(amount, "amount");
1.54 + notNull(fromCurrency, "fromCurrency");
1.55 + notNull(toCurrency, "toCurrency");
1.56 +
1.57 + if((fromCurrency != first && fromCurrency != second) || (toCurrency != first && toCurrency != second)) {
1.58 + throw new UnsupportedConversionException(fromCurrency, toCurrency);
1.59 + }
1.60
1.61 + BigDecimal rateValue = getRateValue(fromCurrency, toCurrency);
1.62 + BigDecimal result = rateValue.multiply(amount);
1.63 + return new Amount(result, toCurrency);
1.64 + }
1.65 +
1.66 + private BigDecimal getRateValue(Currency fromCurrency, Currency toCurrency) {
1.67 +
1.68 + BigDecimal retVal;
1.69 +
1.70 + if(first == fromCurrency) {
1.71 + retVal = rateValue;
1.72 + } else {
1.73 + //reverse rate
1.74 + retVal = one.divide(rateValue, 10 ,RoundingMode.HALF_UP);
1.75 + }
1.76 +
1.77 + return retVal;
1.78 + }
1.79 }