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

japod@33: * The quest for today is to enhance your existing convertor API to hold japod@33: * information about many currencies and allow conversions between any of them. japod@33: * Also, as conversion rates for diferent currencies usually arise from various japod@33: * bank departments, there is another important need. There is a need to japod@33: * compose two convertors into one by merging all the information about japod@33: * currencies they know about. japod@33: */ japod@33: public class Task2Test extends TestCase { japod@33: public Task2Test(String testName) { japod@33: super(testName); japod@33: } japod@33: japod@33: @Override japod@33: protected void setUp() throws Exception { japod@33: } japod@33: japod@33: @Override japod@33: protected void tearDown() throws Exception { japod@33: } japod@33: japod@33: // As in Task1Test, keep in mind, that there are three parts japod@33: // of the whole system: japod@33: // 1. there is someone who knows the current exchange rate japod@33: // 2. there is someone who wants to do the conversion japod@33: // 3. there is the API between 1. and 2. which allows them to communicate japod@33: // japod@33: // Please backward compatibly enhance your existing API to support following japod@33: // usecases: japod@33: // japod@33: japod@33: /** Create convertor that understands two currencies, CZK and japod@33: * SKK. Make 100 SKK == 75 CZK. This is method for the group of users that japod@33: * knows the exchange rate, and needs to use the API to create objects japod@33: * with the exchange rate. Anyone shall be ready to call this method without japod@33: * any other method being called previously. The API itself shall know japod@33: * nothing about any rates, before this method is called. japod@33: */ japod@33: public static Convertor createTripleConvertor() { japod@33: // Rates: 1USD = 15CZK japod@33: // Rates: 1USD = 20SKK japod@33: // Rates: 75CZK = 100SKK japod@33: // set exchange rates japod@33: Convertor.setConvertorRates(Currency.getInstance("USD"), Currency.getInstance("CZK"), 15, 1); japod@33: Convertor.setConvertorRates(Currency.getInstance("USD"), Currency.getInstance("SKK"), 20, 1); japod@33: Convertor.setConvertorRates(Currency.getInstance("SKK"), Currency.getInstance("CZK"), 75, 100); japod@33: japod@33: // create new instance japod@33: Convertor convertor = null; japod@33: try { japod@33: convertor = Convertor.getConvertorInstance(Currency.getInstance("USD"), Currency.getInstance("SKK"), Currency.getInstance("CZK")); japod@33: } catch (UnknownConvertorException e) { japod@33: e.printStackTrace(); japod@33: } japod@33: return convertor; japod@33: } japod@33: japod@33: /** Define convertor that understands three currencies. Use it. japod@33: */ japod@33: public void testConvertorForUSDandCZKandSKK() throws Exception { japod@33: Convertor c = createTripleConvertor(); japod@33: japod@33: // convert $5 to CZK using c: japod@33: double result = c.convert(5d, Currency.getInstance("USD"), Currency.getInstance("CZK")); japod@33: assertEquals("Result is not 75 CZK", 75.0, result); japod@33: japod@33: // convert $5 to SKK using c: japod@33: result = c.convert(5d, Currency.getInstance("USD"), Currency.getInstance("SKK")); japod@33: assertEquals("Result is not 100 SKK", 100.0, result); japod@33: japod@33: // convert 200SKK to CZK using c: japod@33: result = c.convert(200d, Currency.getInstance("SKK"), Currency.getInstance("CZK")); japod@33: assertEquals("Result is not 150 CZK", 150.0, result); japod@33: japod@33: // convert 200SKK to USK using c: japod@33: result = c.convert(200d, Currency.getInstance("SKK"), Currency.getInstance("USD")); japod@33: assertEquals("Result is not 10 USD", 10.0, result); japod@33: } japod@33: japod@33: /** Merge all currency rates of convertor 1 with convertor 2. japod@33: * Implement this using your API, preferably this method just delegates japod@33: * into some API method which does the actual work, without requiring japod@33: * API clients to code anything complex. japod@33: */ japod@33: public static Convertor merge(Convertor one, Convertor two) { japod@33: return one.merge(two); japod@33: } japod@33: japod@33: /** Join the convertors from previous task, Task1Test and show that it japod@33: * can be used to do reasonable conversions. japod@33: */ japod@33: public void testConvertorComposition() throws Exception { japod@33: Convertor c = merge( japod@33: Task1Test.createCZKtoUSD(), japod@33: Task1Test.createSKKtoCZK() japod@33: ); japod@33: japod@33: // convert $5 to CZK using c: japod@33: double result = c.convert(5d, Currency.getInstance("USD"), Currency.getInstance("CZK")); japod@33: assertEquals("Result is not 85 CZK", 85.0, result); japod@33: japod@33: // convert $8 to CZK using c: japod@33: result = c.convert(8d, Currency.getInstance("USD"), Currency.getInstance("CZK")); japod@33: assertEquals("Result is not 136 CZK", 136.0, result); japod@33: japod@33: // convert 1003CZK to USD using c: japod@33: result = c.convert(1003d, Currency.getInstance("CZK"), Currency.getInstance("USD")); japod@33: assertEquals("Result is not 59 USD", 59.0, result); japod@33: japod@33: // convert 16CZK using c: japod@33: result = c.convert(16d, Currency.getInstance("CZK"), Currency.getInstance("SKK")); japod@33: assertEquals("Result is not 20 SKK", 20.0, result); japod@33: japod@33: // convert 500SKK to CZK using c: japod@33: result = c.convert(500d, Currency.getInstance("SKK"), Currency.getInstance("CZK")); japod@33: assertEquals("Result is not 400 CZK", 400.0, result); japod@33: japod@33: } japod@33: }