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