task1/solution02/test/org/apidesign/apifest08/test/Task1Test.java
author japod@localhost
Sun, 28 Sep 2008 14:12:38 +0200
changeset 6 97662396c0fd
child 16 2864c6d744c0
permissions -rw-r--r--
Adding solutions received for task1
     1 package org.apidesign.apifest08.test;
     2 
     3 import java.util.Currency;
     4 
     5 import junit.framework.TestCase;
     6 
     7 import org.apidesign.apifest08.currency.Convertor;
     8 import org.apidesign.apifest08.currency.ConvertorFactory;
     9 import org.apidesign.apifest08.currency.MoneyImpl;
    10 
    11 /** Finish the Convertor API, and then write bodies of methods inside
    12  * of this class to match the given tasks. To fullfil your task, use the
    13  * API define in the <code>org.apidesign.apifest08.currency</code> package.
    14  * Do not you reflection, or other hacks as your code
    15  * shall run without any runtime permissions.
    16  */
    17 public class Task1Test extends TestCase {
    18 	public static final Currency USD = Currency.getInstance("USD");
    19 	public static final Currency CZK = Currency.getInstance("CZK");
    20 	public static final Currency SKK = Currency.getInstance("SKK");
    21     public Task1Test(String testName) {
    22         super(testName);
    23     }
    24 
    25     @Override
    26     protected void setUp() throws Exception {
    27     }
    28 
    29     @Override
    30     protected void tearDown() throws Exception {
    31     }
    32 
    33     /** Create convertor that understands two currencies, CZK and
    34      *  USD. Make 1 USD == 17 CZK.
    35      *
    36      * Creation of the convertor shall not require subclassing of any class
    37      * or interface on the client side.
    38      *
    39      * @return prepared convertor ready for converting USD to CZK and CZK to USD
    40      */
    41     public static Convertor createCZKtoUSD() {
    42       //You can use both variants. I wrote the first one but than I realized that maybe
    43       //you want me to write the second one. So I have written both. 
    44       return ConvertorFactory.createConvertor(CZK, USD);
    45       //return ConvertorFactory.createConvertor(new MoneyImpl(17,CZK), new MoneyImpl(1,USD));
    46     }
    47 
    48     /** Create convertor that understands two currencies, CZK and
    49      *  SKK. Make 100 SKK == 80 CZK.
    50      *
    51      * Creation of the convertor shall not require subclassing of any class
    52      * or interface on the client side.
    53      * 
    54      * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
    55      */
    56     public static Convertor createSKKtoCZK() {
    57     	//You can use both variants. I wrote the first one but than I realized that maybe
    58         //you want me to write the second one. So I have written both.
    59         //return ConvertorFactory.createConvertor(SKK, CZK);
    60         return ConvertorFactory.createConvertor(new MoneyImpl(100,SKK), new MoneyImpl(80,CZK));
    61     }
    62     
    63     /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
    64      * with it.
    65      */
    66     public void testCurrencyCZKUSD() throws Exception {
    67         Convertor czkToUsdConvertor = createCZKtoUSD();
    68         // convert $5 to CZK using c:
    69         Convertor usdToCzkConvertor = czkToUsdConvertor.revert();
    70 		assertEquals("Result is 85 CZK",new MoneyImpl(85,CZK), usdToCzkConvertor.convert(new MoneyImpl(5,USD)));
    71 
    72         // convert $8 to CZK
    73         assertEquals("Result is 136 CZK",new MoneyImpl(136,CZK), usdToCzkConvertor.convert(new MoneyImpl(8,USD)));
    74 
    75         // convert 1003CZK to USD
    76         assertEquals("Result is 59 USD", new MoneyImpl(59,USD), czkToUsdConvertor.convert(new MoneyImpl(1003,CZK)));
    77     }
    78 
    79     /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
    80      * with it.
    81      */
    82     public void testCurrencySKKCZK() throws Exception {
    83         Convertor skkToCzkConvertor = createSKKtoCZK();
    84         // convert 16CZK using c:
    85         assertEquals("Result is 20 SKK", new MoneyImpl(20,SKK), skkToCzkConvertor.revert().convert(new MoneyImpl(16,CZK)));
    86 
    87         // convert 500SKK to CZK
    88         assertEquals("Result is 400 CZK", new MoneyImpl(400,CZK), skkToCzkConvertor.convert(new MoneyImpl(500,SKK)));
    89     }
    90 }
    91