# HG changeset patch # User japod@localhost # Date 1222768878 -7200 # Node ID 45e6a2d1ffd16d9f2052c18134cfc1aa9ede79ea # Parent 83e731257bdc774ed9892f7b639ab8506140006c updating solution 11 to 1.5 diff -r 83e731257bdc -r 45e6a2d1ffd1 task1/solution11/src/org/apidesign/apifest08/currency/Convertor.java --- a/task1/solution11/src/org/apidesign/apifest08/currency/Convertor.java Tue Sep 30 11:59:32 2008 +0200 +++ b/task1/solution11/src/org/apidesign/apifest08/currency/Convertor.java Tue Sep 30 12:01:18 2008 +0200 @@ -22,6 +22,11 @@ Computer computer, CurrencyValue firstCurrencyExchangeRate, CurrencyValue secondCurrencyExchangeRate) { + if (firstCurrencyExchangeRate.getIdentifier() == null || + secondCurrencyExchangeRate.getIdentifier() == null || + firstCurrencyExchangeRate.getIdentifier().equals(secondCurrencyExchangeRate.getIdentifier())) { + throw new IllegalArgumentException("Inappropriate exchange rates' identifiers!"); + } this.computer = computer; this.firstCurrencyExchangeRate = firstCurrencyExchangeRate; this.secondCurrencyExchangeRate = secondCurrencyExchangeRate; @@ -31,11 +36,26 @@ * Convert an amount of the one currency to an amount of the another one currency * with respect to previously specified exchange rate. * - * @param currencyValue an amount of the one currency - * @return an amount of the another one currency + * @param targetCurrency an identifier of the requested currency + * @param currencyValue an amount of the another one currency + * @return an amount of the requested currency */ - public CurrencyValue convert(CurrencyValue currencyValue) { - if (firstCurrencyExchangeRate.getIdentifier().equals(currencyValue.getIdentifier())) { + public CurrencyValue convert( + IdentifierType targetCurrency, + CurrencyValue currencyValue) { + if (firstCurrencyExchangeRate.getIdentifier().equals(targetCurrency) && + secondCurrencyExchangeRate.getIdentifier().equals(currencyValue.getIdentifier())) { + ComputerRequest computerRequest = new ComputerRequest(); + computerRequest.setInput(currencyValue.getAmount()); + computerRequest.setInputCurrencyRatio(secondCurrencyExchangeRate.getAmount()); + computerRequest.setOutputCurrencyRatio(firstCurrencyExchangeRate.getAmount()); + ComputerResponse computerResponse = computer.compute(computerRequest); + + return CurrencyValue.getCurrencyValue( + computerResponse.getResult(), + firstCurrencyExchangeRate.getIdentifier()); + } else if (secondCurrencyExchangeRate.getIdentifier().equals(targetCurrency) && + firstCurrencyExchangeRate.getIdentifier().equals(currencyValue.getIdentifier())) { ComputerRequest computerRequest = new ComputerRequest(); computerRequest.setInput(currencyValue.getAmount()); computerRequest.setInputCurrencyRatio(firstCurrencyExchangeRate.getAmount()); @@ -45,18 +65,8 @@ return CurrencyValue.getCurrencyValue( computerResponse.getResult(), secondCurrencyExchangeRate.getIdentifier()); - } else if (secondCurrencyExchangeRate.getIdentifier().equals(currencyValue.getIdentifier())) { - ComputerRequest computerRequest = new ComputerRequest(); - computerRequest.setInput(currencyValue.getAmount()); - computerRequest.setInputCurrencyRatio(secondCurrencyExchangeRate.getAmount()); - computerRequest.setOutputCurrencyRatio(firstCurrencyExchangeRate.getAmount()); - ComputerResponse computerResponse = computer.compute(computerRequest); - - return CurrencyValue.getCurrencyValue( - computerResponse.getResult(), - firstCurrencyExchangeRate.getIdentifier()); } else { - throw new IllegalArgumentException("Inappropriate currency to convert!"); + throw new IllegalArgumentException("Inappropriate currencies to convert!"); } } diff -r 83e731257bdc -r 45e6a2d1ffd1 task1/solution11/test/org/apidesign/apifest08/test/Task1Test.java --- a/task1/solution11/test/org/apidesign/apifest08/test/Task1Test.java Tue Sep 30 11:59:32 2008 +0200 +++ b/task1/solution11/test/org/apidesign/apifest08/test/Task1Test.java Tue Sep 30 12:01:18 2008 +0200 @@ -23,8 +23,19 @@ protected void tearDown() throws Exception { } + // + // Imagine 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 design such API + // + /** Create convertor that understands two currencies, CZK and - * USD. Make 1 USD == 17 CZK. + * USD. Make 1 USD == 17 CZK. This is a method provided for #1 group - + * e.g. those that know the exchange rate. They somehow need to create + * the objects from the API and tell them the exchange rate. The API itself + * knows nothing about any rates, before the createCZKtoUSD method is called. * * Creation of the convertor shall not require subclassing of any class * or interface on the client side. @@ -39,7 +50,11 @@ } /** Create convertor that understands two currencies, CZK and - * SKK. Make 100 SKK == 80 CZK. + * SKK. Make 100 SKK == 80 CZK. Again this is method for the #1 group - + * it 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. * * Creation of the convertor shall not require subclassing of any class * or interface on the client side. @@ -52,6 +67,13 @@ CurrencyValue.getCurrencyValue(80, "CZK") ); } + + // + // now the methods for group #2 follow: + // this group knows nothing about exchange rates, but knows how to use + // the API to do conversions. It somehow (by calling one of the factory + // methods) gets objects from the API and uses them to do the conversions. + // /** Use the convertor from createCZKtoUSD method and do few conversions * with it. @@ -63,17 +85,17 @@ // convert $5 to CZK using c: // assertEquals("Result is 85 CZK"); - result = c.convert(CurrencyValue.getCurrencyValue(5, "USD")); + result = c.convert("CZK", CurrencyValue.getCurrencyValue(5, "USD")); assertEquals(CurrencyValue.getCurrencyValue(85, "CZK"), result); // convert $8 to CZK // assertEquals("Result is 136 CZK"); - result = c.convert(CurrencyValue.getCurrencyValue(8, "USD")); + result = c.convert("CZK", CurrencyValue.getCurrencyValue(8, "USD")); assertEquals(CurrencyValue.getCurrencyValue(136, "CZK"), result); // convert 1003CZK to USD // assertEquals("Result is 59 USD"); - result = c.convert(CurrencyValue.getCurrencyValue(1003, "CZK")); + result = c.convert("USD", CurrencyValue.getCurrencyValue(1003, "CZK")); assertEquals(CurrencyValue.getCurrencyValue(59, "USD"), result); } @@ -87,13 +109,53 @@ // convert 16CZK using c: // assertEquals("Result is 20 SKK"); - result = c.convert(CurrencyValue.getCurrencyValue(16, "CZK")); + result = c.convert("SKK", CurrencyValue.getCurrencyValue(16, "CZK")); assertEquals(CurrencyValue.getCurrencyValue(20, "SKK"), result); // convert 500SKK to CZK // assertEquals("Result is 400 CZK"); - result = c.convert(CurrencyValue.getCurrencyValue(500, "SKK")); + result = c.convert("CZK", CurrencyValue.getCurrencyValue(500, "SKK")); assertEquals(CurrencyValue.getCurrencyValue(400, "CZK"), result); } + + /** Verify that the CZK to USD convertor knows nothing about SKK. + */ + public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception { + Convertor c = createCZKtoUSD(); + try { + // convert $5 to SKK, the API shall say this is not possible + c.convert("SKK", CurrencyValue.getCurrencyValue(16, "CZK")); + assertTrue("Should not convert", false); + } catch (Exception e) { + assertTrue(true); + } + try { + // convert 500 SKK to CZK, the API shall say this is not possible + c.convert("CZK", CurrencyValue.getCurrencyValue(500, "SKK")); + assertTrue("Should not convert", false); + } catch (Exception e) { + assertTrue(true); + } + + } + + /** Verify that the CZK to SKK convertor knows nothing about USD. + */ + public void testCannotConvertToUSDwithSKKCZKConvertor() throws Exception { + Convertor c = createSKKtoCZK(); + try { + // convert $5 to SKK, the API shall say this is not possible + c.convert("SKK", CurrencyValue.getCurrencyValue(5, "USD")); + assertTrue("Should not convert", false); + } catch (Exception e) { + assertTrue(true); + } + try { + // convert 500 CZK to USD, the API shall say this is not possible + c.convert("USD", CurrencyValue.getCurrencyValue(500, "CZK")); + assertTrue("Should not convert", false); + } catch (Exception e) { + assertTrue(true); + } + } } -