task3/solution14/test/org/apidesign/apifest08/test/Task3Test.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sat, 11 Oct 2008 10:49:25 +0200
changeset 59 c1d43bc1e9c0
parent 54 1b300c79f4ce
permissions -rw-r--r--
Removing all 'implement me' messages
     1 package org.apidesign.apifest08.test;
     2 
     3 import junit.framework.TestCase;
     4 import org.apidesign.apifest08.currency.Convertor;
     5 import org.apidesign.apifest08.currency.ConvertorFactory;
     6 import org.apidesign.apifest08.currency.CurrencyRate;
     7 import org.apidesign.apifest08.currency.Rate;
     8 
     9 /** The exchange rates are not always the same. They are changing. Day by day,
    10  * hour by hour, minute by minute. For every bank it is important to always
    11  * have the actual exchange rate available in the system. That is why let's
    12  * create a pluggable convertor that will always have up to date value of its
    13  * exchange rate.
    14  * <p>
    15  * The quest for today is to allow 3rd party developer to write a convertor
    16  * that adjusts its exchange rate everytime it is queried. This convertor is
    17  * written by independent vendor, the vendor knows only your Convertor API,
    18  * he does not know how the whole system looks and how the convertor is supposed
    19  * to be used.
    20  */
    21 public class Task3Test extends TestCase {
    22     public Task3Test(String testName) {
    23         super(testName);
    24     }
    25 
    26     @Override
    27     protected void setUp() throws Exception {
    28     }
    29 
    30     @Override
    31     protected void tearDown() throws Exception {
    32     }
    33 
    34     // Backward compatibly enhance your existing API to support following
    35     // usecases:
    36     //
    37 
    38 
    39     /** Without knowing anything about the surrounding system, write an
    40      * implementation of convertor that will return different rates everytime
    41      * it is queried. Convert USD to CZK and vice versa. Start with the rate of
    42      * 1USD = 16CZK and adjust it in favor of CZK by 0.01 CZK with every query.
    43      * As soon as you reach 1USD = 15CZK adjust it by 0.01 CZK in favor of USD
    44      * until you reach 1USD = 16CZK
    45      *
    46      * @return new instance of "online" USD and CZK convertor starting with rate 1USD = 16CZK
    47      */
    48     public static Convertor createOnlineCZKUSDConvertor() {
    49         // initial rate: 1USD = 16CZK
    50         // 2nd query 1USD = 15.99CZK
    51         // 3rd query 1USD = 15.98CZK
    52         // until 1USD = 15.00CZK
    53         // then 1USD = 15.01CZK
    54         // then 1USD = 15.02CZK
    55         // and so on and on up to 1USD = 16CZK
    56         // and then another round to 15, etc.
    57         CurrencyRate onlineCurrencyRate = new CurrencyRate() {
    58             private int usdAmount = 100;
    59             private int czkAmount = 1600;
    60             private boolean up = false;
    61             public String getCurrency1() {
    62                 return "USD";
    63             }
    64 
    65             public String getCurrency2() {
    66                 return "CZK";
    67             }
    68 
    69             public Rate getRate() { //return Rate according to online status
    70                 Rate rate = new Rate(usdAmount, czkAmount);
    71                 if (up) {
    72                     if (czkAmount < 1600) {
    73                         czkAmount++;
    74                     } else {
    75                         up = false;
    76                         czkAmount--;
    77                     }
    78                 } else { //down
    79                     if (czkAmount > 1500) {
    80                         czkAmount--;
    81                     } else {
    82                         up = true;
    83                         czkAmount++;
    84                     }
    85                 }
    86                 return rate;
    87             }
    88         };
    89 
    90         return ConvertorFactory.newInstance().createConvertor(onlineCurrencyRate);
    91     }
    92 
    93     public void testFewQueriesForOnlineConvertor() {
    94         Convertor c = createOnlineCZKUSDConvertor();
    95         doFewQueriesForOnlineConvertor(c);
    96     }
    97 
    98     static void doFewQueriesForOnlineConvertor(Convertor c) {
    99         // convert $5 to CZK using c:
   100         //assertEquals("Result is 80 CZK");
   101         assertEquals("Result is 80 CZK", 80.0, c.convert("USD", "CZK", 5), 0.001);
   102 
   103         // convert $8 to CZK using c:
   104         //assertEquals("Result is 127.92 CZK");
   105         assertEquals("Result is 127.92 CZK", 127.92, c.convert("USD", "CZK", 8), 0.001);
   106 
   107         // convert $1 to CZK using c:
   108         //assertEquals("Result is 15.98 CZK");
   109         assertEquals("Result is 15.98 CZK", 15.98, c.convert("USD", "CZK", 1), 0.001);
   110 
   111         // convert 15.97CZK to USD using c:
   112         //assertEquals("Result is 1$");
   113         assertEquals("Result is 1$", 1.0, c.convert("CZK", "USD", 15.97), 0.001);
   114         
   115     }
   116 
   117     /** Join the convertors and show they behave sane.
   118      */
   119     public void testOnlineConvertorComposition() throws Exception {
   120         Convertor c = Task2Test.merge(
   121             createOnlineCZKUSDConvertor(),
   122             Task1Test.createSKKtoCZK()
   123         );
   124 
   125         // convert 16CZK to SKK using c:
   126         // assertEquals("Result is 20 SKK");
   127         assertEquals("Result is 20 SKK", 20.0, c.convert("CZK", "SKK", 16), 0.001);
   128 
   129         // convert 500SKK to CZK using c:
   130         // assertEquals("Result is 400 CZK");
   131         assertEquals("Result is 400 CZK", 400.0, c.convert("SKK", "CZK", 500), 0.001);
   132 
   133         doFewQueriesForOnlineConvertor(c);
   134     }
   135 }