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