japod@41: package org.apidesign.apifest08.test; japod@41: japod@41: import java.math.BigDecimal; japod@41: import junit.framework.TestCase; japod@41: import org.apidesign.apifest08.currency.Convertor; japod@41: import org.apidesign.apifest08.currency.ConvertorCurrency; japod@41: import org.apidesign.apifest08.currency.ExchangeRateProvider; japod@41: japod@41: /** There are many currencies around the world and many banks manipulate japod@41: * with more than one or two at the same time. As banks are usually the japod@41: * best paying clients, which is true even in case of your Convertor API, japod@41: * it is reasonable to listen to their requests. japod@41: *

japod@41: * The quest for today is to enhance your existing convertor API to hold japod@41: * information about many currencies and allow conversions between any of them. japod@41: * Also, as conversion rates for diferent currencies usually arise from various japod@41: * bank departments, there is another important need. There is a need to japod@41: * compose two convertors into one by merging all the information about japod@41: * currencies they know about. japod@41: */ japod@41: public class Task2Test extends TestCase { japod@41: private static ConvertorCurrency currencyCZK = ConvertorCurrency.getInstance("CZK"); japod@41: private static ConvertorCurrency currencySKK = ConvertorCurrency.getInstance("SKK"); japod@41: private static ConvertorCurrency currencyUSD = ConvertorCurrency.getInstance("USD"); japod@41: japod@41: public Task2Test(String testName) { japod@41: super(testName); japod@41: } japod@41: japod@41: @Override japod@41: protected void setUp() throws Exception { japod@41: japod@41: } japod@41: japod@41: @Override japod@41: protected void tearDown() throws Exception { japod@41: } japod@41: japod@41: public static Convertor createUsdToSkkConvertor() { japod@41: ConvertorCurrency fromCurrency = currencyUSD; japod@41: ConvertorCurrency toCurrency = currencyUSD; japod@41: ExchangeRateProvider exchangeRateProvider = new ExchangeRateProvider(new BigDecimal(1), fromCurrency, new BigDecimal(20), toCurrency); japod@41: japod@41: return Convertor.createConvertor(exchangeRateProvider); japod@41: japod@41: } japod@41: japod@41: // As in Task1Test, keep in mind, that there are three parts japod@41: // of the whole system: japod@41: // 1. there is someone who knows the current exchange rate japod@41: // 2. there is someone who wants to do the conversion japod@41: // 3. there is the API between 1. and 2. which allows them to communicate japod@41: // japod@41: // Please backward compatibly enhance your existing API to support following japod@41: // usecases: japod@41: // japod@41: japod@41: /** Create convertor that understands two currencies, CZK and japod@41: * SKK. Make 100 SKK == 75 CZK. This is method for the group of users that japod@41: * knows the exchange rate, and needs to use the API to create objects japod@41: * with the exchange rate. Anyone shall be ready to call this method without japod@41: * any other method being called previously. The API itself shall know japod@41: * nothing about any rates, before this method is called. japod@41: */ japod@41: public static Convertor createTripleConvertor() { japod@41: ExchangeRateProvider exRateProvider = ExchangeRateProvider.createExchangeRateProvider(); japod@41: japod@41: // Rates: 1USD = 15CZK japod@41: exRateProvider.addFixedCurencyRate(currencyUSD, new BigDecimal(1),currencyCZK, new BigDecimal(15)); japod@41: japod@41: // Rates: 1USD = 20SKK japod@41: exRateProvider.addFixedCurencyRate(currencyUSD, new BigDecimal(1), currencySKK, new BigDecimal(20)); japod@41: japod@41: // Rates: 75CZK = 100SKK japod@41: exRateProvider.addFixedCurencyRate(currencyCZK, new BigDecimal(75), currencySKK, new BigDecimal(100)); japod@41: japod@41: Convertor c = Convertor.createConvertor(exRateProvider); japod@41: japod@41: return c; japod@41: } japod@41: japod@41: /** Define convertor that understands three currencies. Use it. japod@41: */ japod@41: public void testConvertorForUSDandCZKandSKK() throws Exception { japod@41: Convertor c = createTripleConvertor(); japod@41: japod@41: // convert $5 to CZK using c: japod@41: assertEquals("Result is 75 CZK",new BigDecimal("75.00"),c.convertWithReversibleRates(currencyUSD, currencyCZK, new BigDecimal(5)).getConverted()); japod@41: japod@41: japod@41: // convert $5 to SKK using c: japod@41: assertEquals("Result is 100 SKK",new BigDecimal("100.00"),c.convertWithReversibleRates(currencyUSD, currencySKK, new BigDecimal(5)).getConverted()); japod@41: japod@41: // convert 200SKK to CZK using c: japod@41: assertEquals("Result is 150 CZK",new BigDecimal("150.00"),c.convertWithReversibleRates(currencySKK, currencyCZK, new BigDecimal(200)).getConverted()); japod@41: japod@41: // convert 200SKK to USK using c: japod@41: // assertEquals("Result is 10 USD"); japod@41: } japod@41: japod@41: /** Merge all currency rates of convertor 1 with convertor 2. japod@41: * Implement this using your API, preferably this method just delegates japod@41: * into some API method which does the actual work, without requiring japod@41: * API clients to code anything complex. japod@41: */ japod@41: public static Convertor merge(Convertor one, Convertor two) { japod@41: return Convertor.createConvertorAsMerge(new Convertor[]{one, two}); japod@41: } japod@41: japod@41: /** Join the convertors from previous task, Task1Test and show that it japod@41: * can be used to do reasonable conversions. japod@41: */ japod@41: public void testConvertorComposition() throws Exception { japod@41: Convertor c = merge( japod@41: Task1Test.createCZKtoUSD(), japod@41: Task1Test.createSKKtoCZK() japod@41: ); japod@41: japod@41: // convert $5 to CZK using c: japod@41: assertEquals("Result is 85 CZK",new BigDecimal("85.00"),c.convertWithReversibleRates(currencyUSD, currencyCZK, new BigDecimal(5)).getConverted()); japod@41: japod@41: // convert $8 to CZK using c: japod@41: // assertEquals("Result is 136 CZK"); japod@41: assertEquals("Result is 136 CZK",new BigDecimal("136.00"),c.convertWithReversibleRates(currencyUSD, currencyCZK, new BigDecimal(8)).getConverted()); japod@41: japod@41: // convert 1003CZK to USD using c: japod@41: assertEquals("Result is 59 USD",new BigDecimal("59.00"),c.convertWithReversibleRates(currencyCZK, currencyUSD, new BigDecimal(1003)).getConverted()); japod@41: japod@41: // convert 16CZK using c: japod@41: assertEquals("Result is 20 SKK",new BigDecimal("20.00"),c.convertWithReversibleRates(currencyCZK, currencySKK, new BigDecimal(16)).getConverted()); japod@41: japod@41: // convert 500SKK to CZK using c: japod@41: assertEquals("Result is 400 CZK",new BigDecimal("400.00"),c.convertWithReversibleRates(currencySKK, currencyCZK, new BigDecimal(500)).getConverted()); japod@41: japod@41: } japod@41: }