1 package org.apidesign.apifest08.test;
3 import java.util.Currency;
5 import junit.framework.TestCase;
7 import org.apidesign.apifest08.currency.Convertor;
8 import org.apidesign.apifest08.currency.ConvertorFactory;
9 import org.apidesign.apifest08.currency.MoneyImpl;
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.
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) {
26 protected void setUp() throws Exception {
30 protected void tearDown() throws Exception {
33 /** Create convertor that understands two currencies, CZK and
34 * USD. Make 1 USD == 17 CZK.
36 * Creation of the convertor shall not require subclassing of any class
37 * or interface on the client side.
39 * @return prepared convertor ready for converting USD to CZK and CZK to USD
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));
48 /** Create convertor that understands two currencies, CZK and
49 * SKK. Make 100 SKK == 80 CZK.
51 * Creation of the convertor shall not require subclassing of any class
52 * or interface on the client side.
54 * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
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));
63 /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
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)));
73 assertEquals("Result is 136 CZK",new MoneyImpl(136,CZK), usdToCzkConvertor.convert(new MoneyImpl(8,USD)));
75 // convert 1003CZK to USD
76 assertEquals("Result is 59 USD", new MoneyImpl(59,USD), czkToUsdConvertor.convert(new MoneyImpl(1003,CZK)));
79 /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
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)));
87 // convert 500SKK to CZK
88 assertEquals("Result is 400 CZK", new MoneyImpl(400,CZK), skkToCzkConvertor.convert(new MoneyImpl(500,SKK)));