# HG changeset patch # User japod@localhost # Date 1223668479 -7200 # Node ID c38391fb9b38633778d02684a83f512d6c90d9bd # Parent 221f1930cbfbd24b86611860378a369255b004d6 solution06 task3 diff -r 221f1930cbfb -r c38391fb9b38 task3/solution06/src/org/apidesign/apifest08/currency/Convertor.java --- a/task3/solution06/src/org/apidesign/apifest08/currency/Convertor.java Fri Oct 10 21:48:49 2008 +0200 +++ b/task3/solution06/src/org/apidesign/apifest08/currency/Convertor.java Fri Oct 10 21:54:39 2008 +0200 @@ -8,6 +8,9 @@ import java.util.Currency; import java.util.List; +/** + * Currency covertor. + */ public final class Convertor { private List convertorDelegates = new ArrayList(); @@ -24,7 +27,22 @@ notNull(currencyFirst, "currencyFirst"); notNull(currencySecond, "currencySecond"); notNull(rateValue, "rateValue"); - convertorDelegates.add(new ConvertorDelegate(rateValue, currencyFirst, currencySecond)); + convertorDelegates.add(new ConvertorDelegate(new StaticRateProvider(rateValue), currencyFirst, currencySecond)); + } + + /** + * Create new instance of the converter for the given currencies and its rate. + * A rate value is provided by {@link RateProvider}. + * + * @param rateProvider the rate provider + * @param currencyFirst the first currency + * @param currencySecond the second currency + */ + public Convertor(RateProvider rateProvider, Currency currencyFirst, Currency currencySecond) { + notNull(currencyFirst, "currencyFirst"); + notNull(currencySecond, "currencySecond"); + notNull(rateProvider, "rateProvider"); + convertorDelegates.add(new ConvertorDelegate(rateProvider, currencyFirst, currencySecond)); } /** @@ -84,11 +102,12 @@ private static class ConvertorDelegate { private final Currency first; private final Currency second; - private final BigDecimal rateValue; // a rate between the first currency and the second currency + private final RateProvider rateProvider; public static final BigDecimal one = new BigDecimal(1); - private ConvertorDelegate(BigDecimal rateValue, Currency currencyFirst, Currency currencySecond) { - this.rateValue = rateValue; + + private ConvertorDelegate(RateProvider rateProvider, Currency currencyFirst, Currency currencySecond) { + this.rateProvider = rateProvider; this.first = currencyFirst; this.second = currencySecond; } @@ -104,8 +123,16 @@ BigDecimal retVal; if(first == fromCurrency) { + BigDecimal rateValue = rateProvider.getRate(); + if(rateValue == null) { + throw new NullPointerException("Rate cannot be null!"); + } retVal = rateValue; } else { + BigDecimal rateValue = rateProvider.getRate(); + if(rateValue == null) { + throw new NullPointerException("Rate cannot be null!"); + } //reverse rate retVal = one.divide(rateValue, 10 ,RoundingMode.HALF_UP); } @@ -121,4 +148,29 @@ return ((fromCurrency == first || fromCurrency == second) && (toCurrency == first || toCurrency == second)); } } + + /** + * A rate provider. This class represents a way how could be "static" convertor + * extended in order converts according to current rate. + */ + public static abstract class RateProvider { + + /** + * @return a rate between the from currency and the to currency associated with + * a given convertor. + */ + public abstract BigDecimal getRate(); + } + + private static class StaticRateProvider extends RateProvider{ + private final BigDecimal rateValue; + + private StaticRateProvider(BigDecimal rateValue){ + this.rateValue = rateValue; + } + + public BigDecimal getRate() { + return this.rateValue; + } + } } diff -r 221f1930cbfb -r c38391fb9b38 task3/solution06/test/org/apidesign/apifest08/test/Task3Test.java --- a/task3/solution06/test/org/apidesign/apifest08/test/Task3Test.java Fri Oct 10 21:48:49 2008 +0200 +++ b/task3/solution06/test/org/apidesign/apifest08/test/Task3Test.java Fri Oct 10 21:54:39 2008 +0200 @@ -1,7 +1,11 @@ package org.apidesign.apifest08.test; +import java.math.BigDecimal; +import java.math.RoundingMode; + import junit.framework.TestCase; import org.apidesign.apifest08.currency.Convertor; +import static org.apidesign.apifest08.test.Currencies.*; /** The exchange rates are not always the same. They are changing. Day by day, * hour by hour, minute by minute. For every bank it is important to always @@ -43,6 +47,21 @@ * @return new instance of "online" USD and CZK convertor starting with rate 1USD = 16CZK */ public static Convertor createOnlineCZKUSDConvertor() { + Convertor.RateProvider rateProvider = new Convertor.RateProvider() { + private BigDecimal seed = new BigDecimal("16.01").setScale(2); + + @Override + public BigDecimal getRate() { + if(seed.equals(new BigDecimal("15").setScale(2))) { + seed = seed.add(new BigDecimal("0.01")).setScale(2); + } else { + seed = seed.subtract(new BigDecimal("0.01")).setScale(2); + } + return seed; + } + + }; + // initial rate: 1USD = 16CZK // 2nd query 1USD = 15.99CZK // 3rd query 1USD = 15.98CZK @@ -51,7 +70,7 @@ // then 1USD = 15.02CZK // and so on and on up to 1USD = 16CZK // and then another round to 15, etc. - return null; + return new Convertor(rateProvider, USD, CZK); } public void testFewQueriesForOnlineConvertor() { @@ -66,18 +85,16 @@ static void doFewQueriesForOnlineConvertor(Convertor c) { // convert $5 to CZK using c: - //assertEquals("Result is 80 CZK"); + assertEquals("Result is 80 CZK",80 ,c.convert(new BigDecimal(5), USD, CZK).getValue().intValue()); // convert $8 to CZK using c: - //assertEquals("Result is 127.92 CZK"); + assertEquals("Result is 127.92 CZK", 127.92d , c.convert(new BigDecimal(8), USD, CZK).getValue().doubleValue()); // convert $1 to CZK using c: - //assertEquals("Result is 15.98 CZK"); + assertEquals("Result is 15.98 CZK", 15.98d, c.convert(new BigDecimal(1), USD, CZK).getValue().doubleValue()); // convert 15.97CZK to USD using c: - //assertEquals("Result is 1$"); - - fail("Implement me!"); + assertEquals("Result is 1$", 1, c.convert(new BigDecimal("15.97").setScale(2), CZK, USD).getValue().intValue()); } /** Join the convertors and show they behave sane. @@ -94,10 +111,10 @@ ); // convert 16CZK to SKK using c: - // assertEquals("Result is 20 SKK"); + assertEquals("Result is 20 SKK", 20, c.convert(new BigDecimal(16), CZK, SKK).getValue().intValue()); // convert 500SKK to CZK using c: - // assertEquals("Result is 400 CZK"); + assertEquals("Result is 400 CZK", 400, c.convert(new BigDecimal(500), SKK, CZK).getValue().intValue()); doFewQueriesForOnlineConvertor(c); }