updating solution 11 to 1.5
authorjapod@localhost
Tue, 30 Sep 2008 12:01:18 +0200
changeset 1945e6a2d1ffd1
parent 18 83e731257bdc
child 20 7ca97f802b5a
updating solution 11 to 1.5
task1/solution11/src/org/apidesign/apifest08/currency/Convertor.java
task1/solution11/test/org/apidesign/apifest08/test/Task1Test.java
     1.1 --- a/task1/solution11/src/org/apidesign/apifest08/currency/Convertor.java	Tue Sep 30 11:59:32 2008 +0200
     1.2 +++ b/task1/solution11/src/org/apidesign/apifest08/currency/Convertor.java	Tue Sep 30 12:01:18 2008 +0200
     1.3 @@ -22,6 +22,11 @@
     1.4              Computer<AmountType> computer,
     1.5              CurrencyValue<AmountType, IdentifierType> firstCurrencyExchangeRate,
     1.6              CurrencyValue<AmountType, IdentifierType> secondCurrencyExchangeRate) {
     1.7 +        if (firstCurrencyExchangeRate.getIdentifier() == null ||
     1.8 +            secondCurrencyExchangeRate.getIdentifier() == null ||
     1.9 +            firstCurrencyExchangeRate.getIdentifier().equals(secondCurrencyExchangeRate.getIdentifier())) {
    1.10 +                throw new IllegalArgumentException("Inappropriate exchange rates' identifiers!");
    1.11 +        }
    1.12          this.computer = computer;
    1.13          this.firstCurrencyExchangeRate = firstCurrencyExchangeRate;
    1.14          this.secondCurrencyExchangeRate = secondCurrencyExchangeRate;
    1.15 @@ -31,11 +36,26 @@
    1.16       * Convert an amount of the one currency to an amount of the another one currency
    1.17       * with respect to previously specified exchange rate.
    1.18       * 
    1.19 -     * @param currencyValue an amount of the one currency
    1.20 -     * @return an amount of the another one currency
    1.21 +     * @param targetCurrency an identifier of the requested currency
    1.22 +     * @param currencyValue an amount of the another one currency
    1.23 +     * @return an amount of the requested currency
    1.24       */
    1.25 -    public CurrencyValue<AmountType, IdentifierType> convert(CurrencyValue<AmountType, IdentifierType> currencyValue) {
    1.26 -        if (firstCurrencyExchangeRate.getIdentifier().equals(currencyValue.getIdentifier())) {
    1.27 +    public CurrencyValue<AmountType, IdentifierType> convert(
    1.28 +            IdentifierType targetCurrency,
    1.29 +            CurrencyValue<AmountType, IdentifierType> currencyValue) {
    1.30 +        if (firstCurrencyExchangeRate.getIdentifier().equals(targetCurrency) &&
    1.31 +            secondCurrencyExchangeRate.getIdentifier().equals(currencyValue.getIdentifier())) {
    1.32 +            ComputerRequest<AmountType> computerRequest = new ComputerRequest<AmountType>();
    1.33 +            computerRequest.setInput(currencyValue.getAmount());
    1.34 +            computerRequest.setInputCurrencyRatio(secondCurrencyExchangeRate.getAmount());
    1.35 +            computerRequest.setOutputCurrencyRatio(firstCurrencyExchangeRate.getAmount());
    1.36 +            ComputerResponse<AmountType> computerResponse = computer.compute(computerRequest);
    1.37 +
    1.38 +            return CurrencyValue.getCurrencyValue(
    1.39 +                    computerResponse.getResult(),
    1.40 +                    firstCurrencyExchangeRate.getIdentifier());
    1.41 +        } else if (secondCurrencyExchangeRate.getIdentifier().equals(targetCurrency) &&
    1.42 +                   firstCurrencyExchangeRate.getIdentifier().equals(currencyValue.getIdentifier())) {
    1.43              ComputerRequest<AmountType> computerRequest = new ComputerRequest<AmountType>();
    1.44              computerRequest.setInput(currencyValue.getAmount());
    1.45              computerRequest.setInputCurrencyRatio(firstCurrencyExchangeRate.getAmount());
    1.46 @@ -45,18 +65,8 @@
    1.47              return CurrencyValue.getCurrencyValue(
    1.48                      computerResponse.getResult(),
    1.49                      secondCurrencyExchangeRate.getIdentifier());
    1.50 -        } else if (secondCurrencyExchangeRate.getIdentifier().equals(currencyValue.getIdentifier())) {
    1.51 -            ComputerRequest<AmountType> computerRequest = new ComputerRequest<AmountType>();
    1.52 -            computerRequest.setInput(currencyValue.getAmount());
    1.53 -            computerRequest.setInputCurrencyRatio(secondCurrencyExchangeRate.getAmount());
    1.54 -            computerRequest.setOutputCurrencyRatio(firstCurrencyExchangeRate.getAmount());
    1.55 -            ComputerResponse<AmountType> computerResponse = computer.compute(computerRequest);
    1.56 -
    1.57 -            return CurrencyValue.getCurrencyValue(
    1.58 -                    computerResponse.getResult(),
    1.59 -                    firstCurrencyExchangeRate.getIdentifier());
    1.60          } else {
    1.61 -            throw new IllegalArgumentException("Inappropriate currency to convert!");
    1.62 +            throw new IllegalArgumentException("Inappropriate currencies to convert!");
    1.63          }
    1.64      }
    1.65  
     2.1 --- a/task1/solution11/test/org/apidesign/apifest08/test/Task1Test.java	Tue Sep 30 11:59:32 2008 +0200
     2.2 +++ b/task1/solution11/test/org/apidesign/apifest08/test/Task1Test.java	Tue Sep 30 12:01:18 2008 +0200
     2.3 @@ -23,8 +23,19 @@
     2.4      protected void tearDown() throws Exception {
     2.5      }
     2.6  
     2.7 +    //
     2.8 +    // Imagine that there are three parts of the whole system:
     2.9 +    // 1. there is someone who knows the current exchange rate
    2.10 +    // 2. there is someone who wants to do the conversion
    2.11 +    // 3. there is the API between 1. and 2. which allows them to communicate
    2.12 +    // Please design such API
    2.13 +    //
    2.14 +
    2.15      /** Create convertor that understands two currencies, CZK and
    2.16 -     *  USD. Make 1 USD == 17 CZK.
    2.17 +     *  USD. Make 1 USD == 17 CZK. This is a method provided for #1 group -
    2.18 +     *  e.g. those that know the exchange rate. They somehow need to create
    2.19 +     *  the objects from the API and tell them the exchange rate. The API itself
    2.20 +     *  knows nothing about any rates, before the createCZKtoUSD method is called.
    2.21       *
    2.22       * Creation of the convertor shall not require subclassing of any class
    2.23       * or interface on the client side.
    2.24 @@ -39,7 +50,11 @@
    2.25      }
    2.26  
    2.27      /** Create convertor that understands two currencies, CZK and
    2.28 -     *  SKK. Make 100 SKK == 80 CZK.
    2.29 +     *  SKK. Make 100 SKK == 80 CZK. Again this is method for the #1 group -
    2.30 +     *  it knows the exchange rate, and needs to use the API to create objects
    2.31 +     *  with the exchange rate. Anyone shall be ready to call this method without
    2.32 +     *  any other method being called previously. The API itself shall know
    2.33 +     *  nothing about any rates, before this method is called.
    2.34       *
    2.35       * Creation of the convertor shall not require subclassing of any class
    2.36       * or interface on the client side.
    2.37 @@ -52,6 +67,13 @@
    2.38                  CurrencyValue.getCurrencyValue(80, "CZK")
    2.39          );
    2.40      }
    2.41 +
    2.42 +    //
    2.43 +    // now the methods for group #2 follow:
    2.44 +    // this group knows nothing about exchange rates, but knows how to use
    2.45 +    // the API to do conversions. It somehow (by calling one of the factory
    2.46 +    // methods) gets objects from the API and uses them to do the conversions.
    2.47 +    //
    2.48      
    2.49      /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
    2.50       * with it.
    2.51 @@ -63,17 +85,17 @@
    2.52          
    2.53          // convert $5 to CZK using c:
    2.54          // assertEquals("Result is 85 CZK");
    2.55 -        result = c.convert(CurrencyValue.getCurrencyValue(5, "USD"));
    2.56 +        result = c.convert("CZK", CurrencyValue.getCurrencyValue(5, "USD"));
    2.57          assertEquals(CurrencyValue.getCurrencyValue(85, "CZK"), result);
    2.58  
    2.59          // convert $8 to CZK
    2.60          // assertEquals("Result is 136 CZK");
    2.61 -        result = c.convert(CurrencyValue.getCurrencyValue(8, "USD"));
    2.62 +        result = c.convert("CZK", CurrencyValue.getCurrencyValue(8, "USD"));
    2.63          assertEquals(CurrencyValue.getCurrencyValue(136, "CZK"), result);
    2.64  
    2.65          // convert 1003CZK to USD
    2.66          // assertEquals("Result is 59 USD");
    2.67 -        result = c.convert(CurrencyValue.getCurrencyValue(1003, "CZK"));
    2.68 +        result = c.convert("USD", CurrencyValue.getCurrencyValue(1003, "CZK"));
    2.69          assertEquals(CurrencyValue.getCurrencyValue(59, "USD"), result);
    2.70      }
    2.71  
    2.72 @@ -87,13 +109,53 @@
    2.73          
    2.74          // convert 16CZK using c:
    2.75          // assertEquals("Result is 20 SKK");
    2.76 -        result = c.convert(CurrencyValue.getCurrencyValue(16, "CZK"));
    2.77 +        result = c.convert("SKK", CurrencyValue.getCurrencyValue(16, "CZK"));
    2.78          assertEquals(CurrencyValue.getCurrencyValue(20, "SKK"), result);
    2.79  
    2.80          // convert 500SKK to CZK
    2.81          // assertEquals("Result is 400 CZK");
    2.82 -        result = c.convert(CurrencyValue.getCurrencyValue(500, "SKK"));
    2.83 +        result = c.convert("CZK", CurrencyValue.getCurrencyValue(500, "SKK"));
    2.84          assertEquals(CurrencyValue.getCurrencyValue(400, "CZK"), result);
    2.85      }
    2.86 +
    2.87 +    /** Verify that the CZK to USD convertor knows nothing about SKK.
    2.88 +     */
    2.89 +    public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
    2.90 +        Convertor<Integer, String> c = createCZKtoUSD();
    2.91 +        try {
    2.92 +            // convert $5 to SKK, the API shall say this is not possible
    2.93 +            c.convert("SKK", CurrencyValue.getCurrencyValue(16, "CZK"));
    2.94 +            assertTrue("Should not convert", false);
    2.95 +        } catch (Exception e) {
    2.96 +            assertTrue(true);
    2.97 +        }
    2.98 +        try {
    2.99 +            // convert 500 SKK to CZK, the API shall say this is not possible
   2.100 +            c.convert("CZK", CurrencyValue.getCurrencyValue(500, "SKK"));
   2.101 +            assertTrue("Should not convert", false);
   2.102 +        } catch (Exception e) {
   2.103 +            assertTrue(true);
   2.104 +        }
   2.105 +        
   2.106 +    }
   2.107 +
   2.108 +    /** Verify that the CZK to SKK convertor knows nothing about USD.
   2.109 +     */
   2.110 +    public void testCannotConvertToUSDwithSKKCZKConvertor() throws Exception {
   2.111 +        Convertor<Integer, String> c = createSKKtoCZK();
   2.112 +        try {
   2.113 +            // convert $5 to SKK, the API shall say this is not possible
   2.114 +            c.convert("SKK", CurrencyValue.getCurrencyValue(5, "USD"));
   2.115 +            assertTrue("Should not convert", false);
   2.116 +        } catch (Exception e) {
   2.117 +            assertTrue(true);
   2.118 +        }
   2.119 +        try {
   2.120 +            // convert 500 CZK to USD, the API shall say this is not possible
   2.121 +            c.convert("USD", CurrencyValue.getCurrencyValue(500, "CZK"));
   2.122 +            assertTrue("Should not convert", false);
   2.123 +        } catch (Exception e) {
   2.124 +            assertTrue(true);
   2.125 +        }
   2.126 +    }
   2.127  }
   2.128 -