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