1.1 --- a/task3/solution12/src/org/apidesign/apifest08/currency/Convertor.java Wed Oct 08 13:24:54 2008 +0200
1.2 +++ b/task3/solution12/src/org/apidesign/apifest08/currency/Convertor.java Fri Oct 10 21:48:49 2008 +0200
1.3 @@ -20,19 +20,35 @@
1.4
1.5 private static Hashtable<String, ExchangeRate> exchangeRates;
1.6
1.7 - private Hashtable<String, ExchangeRate> convertorInstanceExchangeRates;
1.8 + private List<String> currencyConvertors;
1.9
1.10 - private Convertor(List<Currency> currencies) throws UnknownConvertorException {
1.11 - convertorInstanceExchangeRates = new Hashtable<String, ExchangeRate>();
1.12 - for (Currency currency1 : currencies) {
1.13 - for (Currency currency2 : currencies) {
1.14 - if(!currency1.getCurrencyCode().equals(currency2.getCurrencyCode())) {
1.15 - String key = currency1.getCurrencyCode() + currency2.getCurrencyCode();
1.16 - ExchangeRate exchangeRate = exchangeRates.get(key);
1.17 - convertorInstanceExchangeRates.put(key, exchangeRate);
1.18 - }
1.19 - }
1.20 - }
1.21 + /**
1.22 + * Constructor. Checks if all selected currencies are not null and has defined exchange rates for both
1.23 + * directions.
1.24 + * @param currencies currencies for new instance of convertor
1.25 + * @throws UnknownConvertorException if there are not defined exchange rates for both directions for each
1.26 + * pair of currencies
1.27 + */
1.28 + private Convertor(Currency[] currencies) throws UnknownConvertorException {
1.29 + currencyConvertors = new ArrayList<String>();
1.30 +
1.31 + for (Currency currency1 : currencies) {
1.32 + for (Currency currency2 : currencies) {
1.33 + if(currency1 == null || currency2 == null) {
1.34 + throw new ConvertorException("None of the currencies should be null!!!");
1.35 + }
1.36 +
1.37 + if(!currency1.getCurrencyCode().equals(currency2.getCurrencyCode())) {
1.38 + String key = currency1.getCurrencyCode() + currency2.getCurrencyCode();
1.39 + if (!exchangeRates.containsKey(key)) {
1.40 + throw new UnknownConvertorException("Selected convertor (" + currency1.getCurrencyCode() + "->"
1.41 + + currency2.getCurrencyCode() + ") has not defined exchange rates!!!");
1.42 + }
1.43 +
1.44 + currencyConvertors.add(key);
1.45 + }
1.46 + }
1.47 + }
1.48 }
1.49
1.50 /**
1.51 @@ -77,7 +93,11 @@
1.52 * @return convertor with merged exchange rates
1.53 */
1.54 public Convertor merge(Convertor convertor) {
1.55 - convertorInstanceExchangeRates.putAll(convertor.getInstanceExchangeRates());
1.56 + if(convertor == null) {
1.57 + throw new ConvertorException("It's impossible to merge with null convertor!!!");
1.58 + }
1.59 +
1.60 + currencyConvertors.addAll(convertor.getCurrencyConvertors());
1.61 return this;
1.62 }
1.63
1.64 @@ -92,32 +112,11 @@
1.65 * thrown if convertor for selected currencies has not been defined
1.66 */
1.67 public static Convertor getConvertorInstance(Currency... currencies) throws UnknownConvertorException {
1.68 - List<Currency> currencyList = new ArrayList<Currency>();
1.69 -
1.70 if(currencies.length < 2) {
1.71 throw new ConvertorException("To get convertor instance, you have to select at least two currencies!!!");
1.72 }
1.73 -
1.74 - for (Currency currency1 : currencies) {
1.75 - for (Currency currency2 : currencies) {
1.76 - if(currency1 == null || currency2 == null) {
1.77 - throw new ConvertorException("None of the currencies should be null!!!");
1.78 - }
1.79 -
1.80 - if(!currency1.getCurrencyCode().equals(currency2.getCurrencyCode())) {
1.81 - String key12 = currency1.getCurrencyCode() + currency2.getCurrencyCode();
1.82 - String key21 = currency2.getCurrencyCode() + currency1.getCurrencyCode();
1.83 - if (!(exchangeRates.containsKey(key12) && exchangeRates.containsKey(key21))) {
1.84 - throw new UnknownConvertorException("Selected convertor (" + currency1.getCurrencyCode() + "<->"
1.85 - + currency2.getCurrencyCode() + ") has not defined exchange rates!!!");
1.86 - }
1.87 - }
1.88 - }
1.89 -
1.90 - currencyList.add(currency1);
1.91 - }
1.92
1.93 - return new Convertor(currencyList);
1.94 + return new Convertor(currencies);
1.95 }
1.96
1.97 /**
1.98 @@ -163,8 +162,8 @@
1.99
1.100 String key = originalCurrency.getCurrencyCode() + newCurrency.getCurrencyCode();
1.101
1.102 - if(convertorInstanceExchangeRates.containsKey(key)) {
1.103 - actualyUsedExchangeRate = convertorInstanceExchangeRates.get(key);
1.104 + if(currencyConvertors.contains(key)) {
1.105 + actualyUsedExchangeRate = exchangeRates.get(key);
1.106 } else {
1.107 throw new InvalidCurrencyException("This convertor could not be used for converting selected currencies (" + originalCurrency.getCurrencyCode() + "->"
1.108 + newCurrency.getCurrencyCode() + ") !!!");
1.109 @@ -174,11 +173,11 @@
1.110 }
1.111
1.112 /**
1.113 - * Returns exchange rates for actual instance of convertor.
1.114 - * @return exchange rates for actual instance of convertor
1.115 + * Returns currency convertors for actual instance of convertor.
1.116 + * @return currency convertors for actual instance of convertor
1.117 */
1.118 - Hashtable<String, ExchangeRate> getInstanceExchangeRates() {
1.119 - return convertorInstanceExchangeRates;
1.120 + List<String> getCurrencyConvertors() {
1.121 + return currencyConvertors;
1.122 }
1.123
1.124 }
2.1 --- a/task3/solution12/test/org/apidesign/apifest08/test/Task3Test.java Wed Oct 08 13:24:54 2008 +0200
2.2 +++ b/task3/solution12/test/org/apidesign/apifest08/test/Task3Test.java Fri Oct 10 21:48:49 2008 +0200
2.3 @@ -1,7 +1,10 @@
2.4 package org.apidesign.apifest08.test;
2.5
2.6 +import java.util.Currency;
2.7 +
2.8 import junit.framework.TestCase;
2.9 import org.apidesign.apifest08.currency.Convertor;
2.10 +import org.apidesign.apifest08.currency.exceptions.UnknownConvertorException;
2.11
2.12 /** The exchange rates are not always the same. They are changing. Day by day,
2.13 * hour by hour, minute by minute. For every bank it is important to always
2.14 @@ -16,6 +19,13 @@
2.15 * to be used.
2.16 */
2.17 public class Task3Test extends TestCase {
2.18 +
2.19 + private static double actualRate;
2.20 + private static boolean increasing;
2.21 +
2.22 + private static final double EXCHANGE_RATE_MAX = 16.0;
2.23 + private static final double EXCHANGE_RATE_MIN = 15.0;
2.24 +
2.25 public Task3Test(String testName) {
2.26 super(testName);
2.27 }
2.28 @@ -43,62 +53,95 @@
2.29 * @return new instance of "online" USD and CZK convertor starting with rate 1USD = 16CZK
2.30 */
2.31 public static Convertor createOnlineCZKUSDConvertor() {
2.32 - // initial rate: 1USD = 16CZK
2.33 - // 2nd query 1USD = 15.99CZK
2.34 - // 3rd query 1USD = 15.98CZK
2.35 - // until 1USD = 15.00CZK
2.36 - // then 1USD = 15.01CZK
2.37 - // then 1USD = 15.02CZK
2.38 - // and so on and on up to 1USD = 16CZK
2.39 - // and then another round to 15, etc.
2.40 - return null;
2.41 + actualRate = 16.01;
2.42 + increasing = false;
2.43 +
2.44 + // sets actual exchange rates
2.45 + setRates();
2.46 +
2.47 + // create new instance
2.48 + Convertor convertor = null;
2.49 + try {
2.50 + convertor = Convertor.getConvertorInstance(Currency.getInstance("USD"), Currency.getInstance("CZK"));
2.51 + } catch (UnknownConvertorException e) {
2.52 + e.printStackTrace();
2.53 + }
2.54 +
2.55 + return convertor;
2.56 }
2.57
2.58 - public void testFewQueriesForOnlineConvertor() {
2.59 - if (Boolean.getBoolean("ignore.failing")) {
2.60 - // implement me!
2.61 - return;
2.62 - }
2.63 -
2.64 + public void testFewQueriesForOnlineConvertor() throws Exception {
2.65 Convertor c = createOnlineCZKUSDConvertor();
2.66 doFewQueriesForOnlineConvertor(c);
2.67 }
2.68
2.69 - static void doFewQueriesForOnlineConvertor(Convertor c) {
2.70 + static void doFewQueriesForOnlineConvertor(Convertor c) throws Exception {
2.71 // convert $5 to CZK using c:
2.72 - //assertEquals("Result is 80 CZK");
2.73 + double result = c.convert(5d, Currency.getInstance("USD"), Currency.getInstance("CZK"));
2.74 + double expectedResult = actualRate * 5;
2.75 + assertEquals("Result is not " + expectedResult + " CZK", expectedResult, result);
2.76
2.77 + // change exchange rates
2.78 + setRates();
2.79 +
2.80 // convert $8 to CZK using c:
2.81 - //assertEquals("Result is 127.92 CZK");
2.82 + result = c.convert(8d, Currency.getInstance("USD"), Currency.getInstance("CZK"));
2.83 + expectedResult = actualRate * 8;
2.84 + assertEquals("Result is not " + expectedResult + " CZK", expectedResult, result);
2.85 +
2.86 + // change exchange rates
2.87 + setRates();
2.88
2.89 // convert $1 to CZK using c:
2.90 - //assertEquals("Result is 15.98 CZK");
2.91 + result = c.convert(1d, Currency.getInstance("USD"), Currency.getInstance("CZK"));
2.92 + expectedResult = actualRate * 1;
2.93 + assertEquals("Result is not " + expectedResult + " CZK", expectedResult, result);
2.94 +
2.95 + // change exchange rates
2.96 + setRates();
2.97
2.98 // convert 15.97CZK to USD using c:
2.99 - //assertEquals("Result is 1$");
2.100 + result = c.convert(15.97d, Currency.getInstance("CZK"), Currency.getInstance("USD"));
2.101 + expectedResult = 15.97 / actualRate ;
2.102 + assertEquals("Result is not " + expectedResult + " USD", expectedResult, result);
2.103
2.104 - fail("Implement me!");
2.105 +// fail("Implement me!");
2.106 }
2.107
2.108 /** Join the convertors and show they behave sane.
2.109 */
2.110 public void testOnlineConvertorComposition() throws Exception {
2.111 - if (Boolean.getBoolean("ignore.failing")) {
2.112 - // implement me!
2.113 - return;
2.114 - }
2.115 -
2.116 Convertor c = Task2Test.merge(
2.117 createOnlineCZKUSDConvertor(),
2.118 Task1Test.createSKKtoCZK()
2.119 );
2.120
2.121 // convert 16CZK to SKK using c:
2.122 - // assertEquals("Result is 20 SKK");
2.123 + double result = c.convert(16d, Currency.getInstance("CZK"), Currency.getInstance("SKK"));
2.124 + assertEquals("Result is not 20 SKK", 20d, result);
2.125
2.126 // convert 500SKK to CZK using c:
2.127 - // assertEquals("Result is 400 CZK");
2.128 + result = c.convert(500d, Currency.getInstance("SKK"), Currency.getInstance("CZK"));
2.129 + assertEquals("Result is not 400 CZK", 400d, result);
2.130
2.131 doFewQueriesForOnlineConvertor(c);
2.132 }
2.133 +
2.134 + private static void setRates() {
2.135 + // logic for change of actual exchange rate
2.136 + if(increasing) {
2.137 + actualRate += 0.01;
2.138 + if(actualRate == EXCHANGE_RATE_MAX){
2.139 + increasing = false;
2.140 + }
2.141 + } else {
2.142 + actualRate -= 0.01;
2.143 + if(actualRate == EXCHANGE_RATE_MIN){
2.144 + increasing = true;
2.145 + }
2.146 + }
2.147 +
2.148 + // set exchange rates
2.149 + Convertor.setConvertorRates(Currency.getInstance("USD"), Currency.getInstance("CZK"), actualRate, 1d);
2.150 + }
2.151 }