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