task2/solution13/test/org/apidesign/apifest08/test/Task2Test.java
author japod@localhost
Tue, 07 Oct 2008 01:18:23 +0200
changeset 41 a7e6f84fb078
permissions -rw-r--r--
adding solution13 for task2
japod@41
     1
package org.apidesign.apifest08.test;
japod@41
     2
japod@41
     3
import java.math.BigDecimal;
japod@41
     4
import junit.framework.TestCase;
japod@41
     5
import org.apidesign.apifest08.currency.Convertor;
japod@41
     6
import org.apidesign.apifest08.currency.ConvertorCurrency;
japod@41
     7
import org.apidesign.apifest08.currency.ExchangeRateProvider;
japod@41
     8
japod@41
     9
/** There are many currencies around the world and many banks manipulate
japod@41
    10
 * with more than one or two at the same time. As banks are usually the
japod@41
    11
 * best paying clients, which is true even in case of your Convertor API,
japod@41
    12
 * it is reasonable to listen to their requests.
japod@41
    13
 * <p>
japod@41
    14
 * The quest for today is to enhance your existing convertor API to hold
japod@41
    15
 * information about many currencies and allow conversions between any of them.
japod@41
    16
 * Also, as conversion rates for diferent currencies usually arise from various
japod@41
    17
 * bank departments, there is another important need. There is a need to
japod@41
    18
 * compose two convertors into one by merging all the information about
japod@41
    19
 * currencies they know about.
japod@41
    20
 */
japod@41
    21
public class Task2Test extends TestCase {
japod@41
    22
    private static ConvertorCurrency currencyCZK = ConvertorCurrency.getInstance("CZK");
japod@41
    23
    private static ConvertorCurrency currencySKK = ConvertorCurrency.getInstance("SKK");
japod@41
    24
    private static ConvertorCurrency currencyUSD = ConvertorCurrency.getInstance("USD");
japod@41
    25
  
japod@41
    26
    public Task2Test(String testName) {
japod@41
    27
        super(testName);
japod@41
    28
    }
japod@41
    29
japod@41
    30
    @Override
japod@41
    31
    protected void setUp() throws Exception {
japod@41
    32
japod@41
    33
    }
japod@41
    34
japod@41
    35
    @Override
japod@41
    36
    protected void tearDown() throws Exception {
japod@41
    37
    }
japod@41
    38
    
japod@41
    39
    public static Convertor createUsdToSkkConvertor() {
japod@41
    40
        ConvertorCurrency fromCurrency = currencyUSD;
japod@41
    41
        ConvertorCurrency toCurrency = currencyUSD;
japod@41
    42
        ExchangeRateProvider exchangeRateProvider = new ExchangeRateProvider(new BigDecimal(1), fromCurrency, new BigDecimal(20), toCurrency);
japod@41
    43
        
japod@41
    44
        return Convertor.createConvertor(exchangeRateProvider);
japod@41
    45
        
japod@41
    46
    }
japod@41
    47
japod@41
    48
    // As in Task1Test, keep in mind, that there are three parts
japod@41
    49
    // of the whole system:
japod@41
    50
    // 1. there is someone who knows the current exchange rate
japod@41
    51
    // 2. there is someone who wants to do the conversion
japod@41
    52
    // 3. there is the API between 1. and 2. which allows them to communicate
japod@41
    53
    // 
japod@41
    54
    // Please backward compatibly enhance your existing API to support following
japod@41
    55
    // usecases:
japod@41
    56
    //
japod@41
    57
    
japod@41
    58
    /** Create convertor that understands two currencies, CZK and
japod@41
    59
     *  SKK. Make 100 SKK == 75 CZK. This is method for the group of users that
japod@41
    60
     *  knows the exchange rate, and needs to use the API to create objects
japod@41
    61
     *  with the exchange rate. Anyone shall be ready to call this method without
japod@41
    62
     *  any other method being called previously. The API itself shall know
japod@41
    63
     *  nothing about any rates, before this method is called.
japod@41
    64
     */
japod@41
    65
    public static Convertor createTripleConvertor() {
japod@41
    66
        ExchangeRateProvider exRateProvider = ExchangeRateProvider.createExchangeRateProvider();
japod@41
    67
        
japod@41
    68
        // Rates: 1USD = 15CZK
japod@41
    69
        exRateProvider.addFixedCurencyRate(currencyUSD, new BigDecimal(1),currencyCZK,  new BigDecimal(15));
japod@41
    70
        
japod@41
    71
        // Rates: 1USD = 20SKK
japod@41
    72
        exRateProvider.addFixedCurencyRate(currencyUSD, new BigDecimal(1), currencySKK,  new BigDecimal(20));
japod@41
    73
        
japod@41
    74
        // Rates: 75CZK = 100SKK
japod@41
    75
        exRateProvider.addFixedCurencyRate(currencyCZK, new BigDecimal(75), currencySKK,  new BigDecimal(100));
japod@41
    76
        
japod@41
    77
        Convertor c = Convertor.createConvertor(exRateProvider);
japod@41
    78
        
japod@41
    79
        return c;
japod@41
    80
    }
japod@41
    81
japod@41
    82
    /** Define convertor that understands three currencies. Use it.
japod@41
    83
     */
japod@41
    84
    public void testConvertorForUSDandCZKandSKK() throws Exception {
japod@41
    85
        Convertor c = createTripleConvertor();
japod@41
    86
japod@41
    87
        // convert $5 to CZK using c:
japod@41
    88
        assertEquals("Result is 75 CZK",new BigDecimal("75.00"),c.convertWithReversibleRates(currencyUSD, currencyCZK, new BigDecimal(5)).getConverted());
japod@41
    89
japod@41
    90
japod@41
    91
        // convert $5 to SKK using c:
japod@41
    92
        assertEquals("Result is 100 SKK",new BigDecimal("100.00"),c.convertWithReversibleRates(currencyUSD, currencySKK, new BigDecimal(5)).getConverted());
japod@41
    93
japod@41
    94
        // convert 200SKK to CZK using c:
japod@41
    95
        assertEquals("Result is 150 CZK",new BigDecimal("150.00"),c.convertWithReversibleRates(currencySKK, currencyCZK, new BigDecimal(200)).getConverted());
japod@41
    96
japod@41
    97
        // convert 200SKK to USK using c:
japod@41
    98
        // assertEquals("Result is 10 USD");
japod@41
    99
    }
japod@41
   100
japod@41
   101
    /** Merge all currency rates of convertor 1 with convertor 2.
japod@41
   102
     * Implement this using your API, preferably this method just delegates
japod@41
   103
     * into some API method which does the actual work, without requiring
japod@41
   104
     * API clients to code anything complex.
japod@41
   105
     */
japod@41
   106
    public static Convertor merge(Convertor one, Convertor two) {
japod@41
   107
        return Convertor.createConvertorAsMerge(new Convertor[]{one, two});
japod@41
   108
    }
japod@41
   109
japod@41
   110
    /** Join the convertors from previous task, Task1Test and show that it
japod@41
   111
     * can be used to do reasonable conversions.
japod@41
   112
     */
japod@41
   113
    public void testConvertorComposition() throws Exception {
japod@41
   114
        Convertor c = merge(
japod@41
   115
            Task1Test.createCZKtoUSD(),
japod@41
   116
            Task1Test.createSKKtoCZK()
japod@41
   117
        );
japod@41
   118
japod@41
   119
        // convert $5 to CZK using c:
japod@41
   120
        assertEquals("Result is 85 CZK",new BigDecimal("85.00"),c.convertWithReversibleRates(currencyUSD, currencyCZK, new BigDecimal(5)).getConverted());
japod@41
   121
japod@41
   122
        // convert $8 to CZK using c:
japod@41
   123
        // assertEquals("Result is 136 CZK");
japod@41
   124
        assertEquals("Result is 136 CZK",new BigDecimal("136.00"),c.convertWithReversibleRates(currencyUSD, currencyCZK, new BigDecimal(8)).getConverted());
japod@41
   125
japod@41
   126
        // convert 1003CZK to USD using c:
japod@41
   127
        assertEquals("Result is 59 USD",new BigDecimal("59.00"),c.convertWithReversibleRates(currencyCZK, currencyUSD, new BigDecimal(1003)).getConverted());
japod@41
   128
japod@41
   129
        // convert 16CZK using c:
japod@41
   130
        assertEquals("Result is 20 SKK",new BigDecimal("20.00"),c.convertWithReversibleRates(currencyCZK, currencySKK, new BigDecimal(16)).getConverted());
japod@41
   131
japod@41
   132
        // convert 500SKK to CZK using c:
japod@41
   133
        assertEquals("Result is 400 CZK",new BigDecimal("400.00"),c.convertWithReversibleRates(currencySKK, currencyCZK, new BigDecimal(500)).getConverted());
japod@41
   134
japod@41
   135
    }
japod@41
   136
}