# HG changeset patch # User japod@localhost # Date 1223331427 -7200 # Node ID 80f2d8751f356e0d3a4057a01a46f8f8e8d0889b # Parent 2198184978d5ccfd88dea5e90bf8cc8b6f43594a adding solution12 for task 2 diff -r 2198184978d5 -r 80f2d8751f35 task2/solution12/src/org/apidesign/apifest08/currency/Convertor.java --- a/task2/solution12/src/org/apidesign/apifest08/currency/Convertor.java Wed Oct 01 11:23:11 2008 +0200 +++ b/task2/solution12/src/org/apidesign/apifest08/currency/Convertor.java Tue Oct 07 00:17:07 2008 +0200 @@ -1,7 +1,9 @@ package org.apidesign.apifest08.currency; +import java.util.ArrayList; import java.util.Currency; import java.util.Hashtable; +import java.util.List; import org.apidesign.apifest08.currency.exceptions.ConvertorException; import org.apidesign.apifest08.currency.exceptions.InvalidCurrencyException; @@ -18,20 +20,19 @@ private static Hashtable exchangeRates; - private ExchangeRate exchangeRate12; - private ExchangeRate exchangeRate21; + private Hashtable convertorInstanceExchangeRates; - private Convertor(Currency currency1, Currency currency2) throws UnknownConvertorException { - String key12 = currency1.getCurrencyCode() + currency2.getCurrencyCode(); - String key21 = currency2.getCurrencyCode() + currency1.getCurrencyCode(); - - if (!(exchangeRates.containsKey(key12) && exchangeRates.containsKey(key21))) { - throw new UnknownConvertorException("Selected convertor (" + currency1.getCurrencyCode() + "->" - + currency2.getCurrencyCode() + ") has not defined any rates!!!"); - } - - this.exchangeRate12 = exchangeRates.get(key12); - this.exchangeRate21 = exchangeRates.get(key21); + private Convertor(List currencies) throws UnknownConvertorException { + convertorInstanceExchangeRates = new Hashtable(); + for (Currency currency1 : currencies) { + for (Currency currency2 : currencies) { + if(!currency1.getCurrencyCode().equals(currency2.getCurrencyCode())) { + String key = currency1.getCurrencyCode() + currency2.getCurrencyCode(); + ExchangeRate exchangeRate = exchangeRates.get(key); + convertorInstanceExchangeRates.put(key, exchangeRate); + } + } + } } /** @@ -67,6 +68,18 @@ exchangeRates.put(key21, new ExchangeRate(currency2, currency1, recountedRate, unit)); } + + /** + * Merge exchange rates of actual convertor with exchange rates from selected + * convertor. If there are same currencies in both convertors, these from selected + * convertor rewrites these in actual convertor. + * @param convertor convertor to merge with actual one + * @return convertor with merged exchange rates + */ + public Convertor merge(Convertor convertor) { + convertorInstanceExchangeRates.putAll(convertor.getInstanceExchangeRates()); + return this; + } /** * Creates new instance of convertor. @@ -78,11 +91,33 @@ * @throws UnknownConvertorException * thrown if convertor for selected currencies has not been defined */ - public static Convertor getConvertorInstance(Currency currency1, Currency currency2) throws UnknownConvertorException { - if (currency1 == null || currency2 == null) { - throw new ConvertorException("None of the currencies should be null!!!"); - } - return new Convertor(currency1, currency2); + public static Convertor getConvertorInstance(Currency... currencies) throws UnknownConvertorException { + List currencyList = new ArrayList(); + + if(currencies.length < 2) { + throw new ConvertorException("To get convertor instance, you have to select at least two currencies!!!"); + } + + for (Currency currency1 : currencies) { + for (Currency currency2 : currencies) { + if(currency1 == null || currency2 == null) { + throw new ConvertorException("None of the currencies should be null!!!"); + } + + if(!currency1.getCurrencyCode().equals(currency2.getCurrencyCode())) { + String key12 = currency1.getCurrencyCode() + currency2.getCurrencyCode(); + String key21 = currency2.getCurrencyCode() + currency1.getCurrencyCode(); + if (!(exchangeRates.containsKey(key12) && exchangeRates.containsKey(key21))) { + throw new UnknownConvertorException("Selected convertor (" + currency1.getCurrencyCode() + "<->" + + currency2.getCurrencyCode() + ") has not defined exchange rates!!!"); + } + } + } + + currencyList.add(currency1); + } + + return new Convertor(currencyList); } /** @@ -125,25 +160,25 @@ */ private ExchangeRate getExchangeRate(Currency originalCurrency, Currency newCurrency) throws InvalidCurrencyException { ExchangeRate actualyUsedExchangeRate = null; + + String key = originalCurrency.getCurrencyCode() + newCurrency.getCurrencyCode(); - if (originalCurrency.getCurrencyCode().equals(exchangeRate12.getOriginalCurrency().getCurrencyCode()) - && newCurrency.getCurrencyCode().equals(exchangeRate12.getNewCurrency().getCurrencyCode())) { - actualyUsedExchangeRate = exchangeRate12; - } else if (originalCurrency.getCurrencyCode().equals(exchangeRate21.getOriginalCurrency().getCurrencyCode()) - && newCurrency.getCurrencyCode().equals(exchangeRate21.getNewCurrency().getCurrencyCode())) { - actualyUsedExchangeRate = exchangeRate21; + if(convertorInstanceExchangeRates.containsKey(key)) { + actualyUsedExchangeRate = convertorInstanceExchangeRates.get(key); } else { - throw new InvalidCurrencyException("This convertor " + this - + " could not be used for converting selected currencies (" + originalCurrency.getCurrencyCode() + "->" + throw new InvalidCurrencyException("This convertor could not be used for converting selected currencies (" + originalCurrency.getCurrencyCode() + "->" + newCurrency.getCurrencyCode() + ") !!!"); } return actualyUsedExchangeRate; } - - public String toString() { - String currency1Code = exchangeRate12.getOriginalCurrency().getCurrencyCode(); - String currency2Code = exchangeRate12.getNewCurrency().getCurrencyCode(); - return "Converter [" + currency1Code + "->" + currency2Code + ", " + currency2Code + "->" + currency1Code + "]"; + + /** + * Returns exchange rates for actual instance of convertor. + * @return exchange rates for actual instance of convertor + */ + Hashtable getInstanceExchangeRates() { + return convertorInstanceExchangeRates; } + } diff -r 2198184978d5 -r 80f2d8751f35 task2/solution12/test/org/apidesign/apifest08/test/Task2Test.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/task2/solution12/test/org/apidesign/apifest08/test/Task2Test.java Tue Oct 07 00:17:07 2008 +0200 @@ -0,0 +1,132 @@ +package org.apidesign.apifest08.test; + +import java.util.Currency; + +import junit.framework.TestCase; + +import org.apidesign.apifest08.currency.Convertor; +import org.apidesign.apifest08.currency.exceptions.UnknownConvertorException; + +/** There are many currencies around the world and many banks manipulate + * with more than one or two at the same time. As banks are usually the + * best paying clients, which is true even in case of your Convertor API, + * it is reasonable to listen to their requests. + *

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