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

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