# HG changeset patch # User japod@localhost # Date 1223668129 -7200 # Node ID 221f1930cbfbd24b86611860378a369255b004d6 # Parent 03c5c5dc94e7f0782ea073eac3f98dd7ca965614 solution12 task3 diff -r 03c5c5dc94e7 -r 221f1930cbfb task3/solution12/src/org/apidesign/apifest08/currency/Convertor.java --- a/task3/solution12/src/org/apidesign/apifest08/currency/Convertor.java Wed Oct 08 13:24:54 2008 +0200 +++ b/task3/solution12/src/org/apidesign/apifest08/currency/Convertor.java Fri Oct 10 21:48:49 2008 +0200 @@ -20,19 +20,35 @@ private static Hashtable exchangeRates; - private Hashtable convertorInstanceExchangeRates; + private List currencyConvertors; - 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); - } - } - } + /** + * Constructor. Checks if all selected currencies are not null and has defined exchange rates for both + * directions. + * @param currencies currencies for new instance of convertor + * @throws UnknownConvertorException if there are not defined exchange rates for both directions for each + * pair of currencies + */ + private Convertor(Currency[] currencies) throws UnknownConvertorException { + currencyConvertors = new ArrayList(); + + 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 key = currency1.getCurrencyCode() + currency2.getCurrencyCode(); + if (!exchangeRates.containsKey(key)) { + throw new UnknownConvertorException("Selected convertor (" + currency1.getCurrencyCode() + "->" + + currency2.getCurrencyCode() + ") has not defined exchange rates!!!"); + } + + currencyConvertors.add(key); + } + } + } } /** @@ -77,7 +93,11 @@ * @return convertor with merged exchange rates */ public Convertor merge(Convertor convertor) { - convertorInstanceExchangeRates.putAll(convertor.getInstanceExchangeRates()); + if(convertor == null) { + throw new ConvertorException("It's impossible to merge with null convertor!!!"); + } + + currencyConvertors.addAll(convertor.getCurrencyConvertors()); return this; } @@ -92,32 +112,11 @@ * thrown if convertor for selected currencies has not been defined */ 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); + return new Convertor(currencies); } /** @@ -163,8 +162,8 @@ String key = originalCurrency.getCurrencyCode() + newCurrency.getCurrencyCode(); - if(convertorInstanceExchangeRates.containsKey(key)) { - actualyUsedExchangeRate = convertorInstanceExchangeRates.get(key); + if(currencyConvertors.contains(key)) { + actualyUsedExchangeRate = exchangeRates.get(key); } else { throw new InvalidCurrencyException("This convertor could not be used for converting selected currencies (" + originalCurrency.getCurrencyCode() + "->" + newCurrency.getCurrencyCode() + ") !!!"); @@ -174,11 +173,11 @@ } /** - * Returns exchange rates for actual instance of convertor. - * @return exchange rates for actual instance of convertor + * Returns currency convertors for actual instance of convertor. + * @return currency convertors for actual instance of convertor */ - Hashtable getInstanceExchangeRates() { - return convertorInstanceExchangeRates; + List getCurrencyConvertors() { + return currencyConvertors; } } diff -r 03c5c5dc94e7 -r 221f1930cbfb task3/solution12/test/org/apidesign/apifest08/test/Task3Test.java --- a/task3/solution12/test/org/apidesign/apifest08/test/Task3Test.java Wed Oct 08 13:24:54 2008 +0200 +++ b/task3/solution12/test/org/apidesign/apifest08/test/Task3Test.java Fri Oct 10 21:48:49 2008 +0200 @@ -1,7 +1,10 @@ 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; /** The exchange rates are not always the same. They are changing. Day by day, * hour by hour, minute by minute. For every bank it is important to always @@ -16,6 +19,13 @@ * to be used. */ public class Task3Test extends TestCase { + + private static double actualRate; + private static boolean increasing; + + private static final double EXCHANGE_RATE_MAX = 16.0; + private static final double EXCHANGE_RATE_MIN = 15.0; + public Task3Test(String testName) { super(testName); } @@ -43,62 +53,95 @@ * @return new instance of "online" USD and CZK convertor starting with rate 1USD = 16CZK */ public static Convertor createOnlineCZKUSDConvertor() { - // initial rate: 1USD = 16CZK - // 2nd query 1USD = 15.99CZK - // 3rd query 1USD = 15.98CZK - // until 1USD = 15.00CZK - // then 1USD = 15.01CZK - // then 1USD = 15.02CZK - // and so on and on up to 1USD = 16CZK - // and then another round to 15, etc. - return null; + actualRate = 16.01; + increasing = false; + + // sets actual exchange rates + setRates(); + + // create new instance + Convertor convertor = null; + try { + convertor = Convertor.getConvertorInstance(Currency.getInstance("USD"), Currency.getInstance("CZK")); + } catch (UnknownConvertorException e) { + e.printStackTrace(); + } + + return convertor; } - public void testFewQueriesForOnlineConvertor() { - if (Boolean.getBoolean("ignore.failing")) { - // implement me! - return; - } - + public void testFewQueriesForOnlineConvertor() throws Exception { Convertor c = createOnlineCZKUSDConvertor(); doFewQueriesForOnlineConvertor(c); } - static void doFewQueriesForOnlineConvertor(Convertor c) { + static void doFewQueriesForOnlineConvertor(Convertor c) throws Exception { // convert $5 to CZK using c: - //assertEquals("Result is 80 CZK"); + double result = c.convert(5d, Currency.getInstance("USD"), Currency.getInstance("CZK")); + double expectedResult = actualRate * 5; + assertEquals("Result is not " + expectedResult + " CZK", expectedResult, result); + // change exchange rates + setRates(); + // convert $8 to CZK using c: - //assertEquals("Result is 127.92 CZK"); + result = c.convert(8d, Currency.getInstance("USD"), Currency.getInstance("CZK")); + expectedResult = actualRate * 8; + assertEquals("Result is not " + expectedResult + " CZK", expectedResult, result); + + // change exchange rates + setRates(); // convert $1 to CZK using c: - //assertEquals("Result is 15.98 CZK"); + result = c.convert(1d, Currency.getInstance("USD"), Currency.getInstance("CZK")); + expectedResult = actualRate * 1; + assertEquals("Result is not " + expectedResult + " CZK", expectedResult, result); + + // change exchange rates + setRates(); // convert 15.97CZK to USD using c: - //assertEquals("Result is 1$"); + result = c.convert(15.97d, Currency.getInstance("CZK"), Currency.getInstance("USD")); + expectedResult = 15.97 / actualRate ; + assertEquals("Result is not " + expectedResult + " USD", expectedResult, result); - fail("Implement me!"); +// fail("Implement me!"); } /** Join the convertors and show they behave sane. */ public void testOnlineConvertorComposition() throws Exception { - if (Boolean.getBoolean("ignore.failing")) { - // implement me! - return; - } - Convertor c = Task2Test.merge( createOnlineCZKUSDConvertor(), Task1Test.createSKKtoCZK() ); // convert 16CZK to SKK using c: - // assertEquals("Result is 20 SKK"); + double result = c.convert(16d, Currency.getInstance("CZK"), Currency.getInstance("SKK")); + assertEquals("Result is not 20 SKK", 20d, result); // convert 500SKK to CZK using c: - // assertEquals("Result is 400 CZK"); + result = c.convert(500d, Currency.getInstance("SKK"), Currency.getInstance("CZK")); + assertEquals("Result is not 400 CZK", 400d, result); doFewQueriesForOnlineConvertor(c); } + + private static void setRates() { + // logic for change of actual exchange rate + if(increasing) { + actualRate += 0.01; + if(actualRate == EXCHANGE_RATE_MAX){ + increasing = false; + } + } else { + actualRate -= 0.01; + if(actualRate == EXCHANGE_RATE_MIN){ + increasing = true; + } + } + + // set exchange rates + Convertor.setConvertorRates(Currency.getInstance("USD"), Currency.getInstance("CZK"), actualRate, 1d); + } }