task1/solution04/test/org/apidesign/apifest08/test/Task1Test.java
author japod@localhost
Sun, 28 Sep 2008 14:12:38 +0200
changeset 6 97662396c0fd
child 17 37c9921c653e
permissions -rw-r--r--
Adding solutions received for task1
japod@6
     1
package org.apidesign.apifest08.test;
japod@6
     2
japod@6
     3
import java.math.BigDecimal;
japod@6
     4
import java.util.Currency;
japod@6
     5
import junit.framework.TestCase;
japod@6
     6
import org.apidesign.apifest08.currency.Convertor;
japod@6
     7
import org.apidesign.apifest08.currency.ConvertorFactory;
japod@6
     8
japod@6
     9
/** Finish the Convertor API, and then write bodies of methods inside
japod@6
    10
 * of this class to match the given tasks. To fullfil your task, use the
japod@6
    11
 * API define in the <code>org.apidesign.apifest08.currency</code> package.
japod@6
    12
 * Do not you reflection, or other hacks as your code
japod@6
    13
 * shall run without any runtime permissions.
japod@6
    14
 */
japod@6
    15
public class Task1Test extends TestCase {
japod@6
    16
    public Task1Test(String testName) {
japod@6
    17
        super(testName);
japod@6
    18
    }
japod@6
    19
japod@6
    20
    @Override
japod@6
    21
    protected void setUp() throws Exception {
japod@6
    22
    }
japod@6
    23
japod@6
    24
    @Override
japod@6
    25
    protected void tearDown() throws Exception {
japod@6
    26
    }
japod@6
    27
japod@6
    28
    /** Create convertor that understands two currencies, CZK and
japod@6
    29
     *  USD. Make 1 USD == 17 CZK.
japod@6
    30
     *
japod@6
    31
     * Creation of the convertor shall not require subclassing of any class
japod@6
    32
     * or interface on the client side.
japod@6
    33
     *
japod@6
    34
     * @return prepared convertor ready for converting USD to CZK and CZK to USD
japod@6
    35
     */
japod@6
    36
    public static Convertor createCZKtoUSD()
japod@6
    37
    {
japod@6
    38
        return (ConvertorFactory.getConvertor("CZK", "USD"));
japod@6
    39
    }
japod@6
    40
japod@6
    41
    /** Create convertor that understands two currencies, CZK and
japod@6
    42
     *  SKK. Make 100 SKK == 80 CZK.
japod@6
    43
     *
japod@6
    44
     * Creation of the convertor shall not require subclassing of any class
japod@6
    45
     * or interface on the client side.
japod@6
    46
     * 
japod@6
    47
     * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
japod@6
    48
     */
japod@6
    49
    public static Convertor createSKKtoCZK()
japod@6
    50
    {
japod@6
    51
        return (ConvertorFactory.getConvertor(Currency.getInstance("SKK"), Currency.getInstance("CZK")));
japod@6
    52
    }
japod@6
    53
    
japod@6
    54
    public static Convertor createUSDtoUSD()
japod@6
    55
    {
japod@6
    56
        return (ConvertorFactory.getConvertor(Currency.getInstance("USD"), Currency.getInstance("USD")));
japod@6
    57
    }
japod@6
    58
    
japod@6
    59
    /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
japod@6
    60
     * with it.
japod@6
    61
     */
japod@6
    62
    public void testCurrencyCZKUSD() throws Exception {
japod@6
    63
        Convertor  c = createCZKtoUSD();
japod@6
    64
        BigDecimal result;
japod@6
    65
        
japod@6
    66
        // convert $5 to CZK using c:
japod@6
    67
        // assertEquals("Result is 85 CZK");
japod@6
    68
        result = c.convertFrom(BigDecimal.valueOf(5));
japod@6
    69
        assertEquals(new BigDecimal("85.00"), result);
japod@6
    70
japod@6
    71
        // convert $8 to CZK
japod@6
    72
        // assertEquals("Result is 136 CZK");
japod@6
    73
        result = c.convertFrom(BigDecimal.valueOf(8));
japod@6
    74
        assertEquals(new BigDecimal("136.00"), result);
japod@6
    75
japod@6
    76
        // convert 1003CZK to USD
japod@6
    77
        // assertEquals("Result is 59 USD");
japod@6
    78
        result = c.convertTo(BigDecimal.valueOf(1003));
japod@6
    79
        assertEquals(new BigDecimal("59.00"), result);
japod@6
    80
    }
japod@6
    81
japod@6
    82
    /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
japod@6
    83
     * with it.
japod@6
    84
     */
japod@6
    85
    public void testCurrencySKKCZK() throws Exception {
japod@6
    86
        Convertor c = createSKKtoCZK();
japod@6
    87
        BigDecimal result;
japod@6
    88
        
japod@6
    89
        // convert 16CZK using c:
japod@6
    90
        // assertEquals("Result is 20 SKK");
japod@6
    91
        result = c.convertFrom(BigDecimal.valueOf(16));
japod@6
    92
        assertEquals(new BigDecimal("20.00"), result);
japod@6
    93
                        
japod@6
    94
        // convert 500SKK to CZK
japod@6
    95
        // assertEquals("Result is 400 CZK");
japod@6
    96
        result = c.convertTo(BigDecimal.valueOf(500));
japod@6
    97
        assertEquals(new BigDecimal("400.00"), result);
japod@6
    98
   }
japod@6
    99
japod@6
   100
    /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
japod@6
   101
     * with it.
japod@6
   102
     */
japod@6
   103
    public void testCurrencyUSDUSD() throws Exception {
japod@6
   104
        Convertor c = createUSDtoUSD();
japod@6
   105
        BigDecimal result;
japod@6
   106
        
japod@6
   107
        // convert 1USD using c:
japod@6
   108
        // assertEquals("Result is 1 USD");
japod@6
   109
        result = c.convertFrom(BigDecimal.valueOf(1));
japod@6
   110
        assertEquals(new BigDecimal("1.00"), result);
japod@6
   111
                        
japod@6
   112
        // convert 500USD to USD
japod@6
   113
        // assertEquals("Result is 500 USD");
japod@6
   114
        result = c.convertTo(BigDecimal.valueOf(500));
japod@6
   115
        assertEquals(new BigDecimal("500.00"), result);
japod@6
   116
   }
japod@6
   117
}
japod@6
   118