1 package org.apidesign.apifest08.test;
3 import java.util.Currency;
5 import junit.framework.TestCase;
7 import org.apidesign.apifest08.currency.Convertor;
10 * Finish the Convertor API, and then write bodies of methods inside of this class to match the given tasks. To fullfil
11 * your task, use the API define in the <code>org.apidesign.apifest08.currency</code> package. Do not you reflection, or
12 * other hacks as your code shall run without any runtime permissions.
14 public class Task1Test extends TestCase {
15 public Task1Test(String testName) {
20 protected void setUp() throws Exception {
24 protected void tearDown() throws Exception {
28 * Create convertor that understands two currencies, CZK and USD. Make 1 USD == 17 CZK. Creation of the convertor
29 * shall not require subclassing of any class or interface on the client side.
30 * @return prepared convertor ready for converting USD to CZK and CZK to USD
32 public static Convertor createCZKtoUSD() {
33 return Convertor.getConvertorInstance(Currency.getInstance("CZK"), Currency.getInstance("USD"));
37 * Create convertor that understands two currencies, CZK and SKK. Make 100 SKK == 80 CZK. Creation of the convertor
38 * shall not require subclassing of any class or interface on the client side.
39 * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
41 public static Convertor createSKKtoCZK() {
42 return Convertor.getConvertorInstance(Currency.getInstance("SKK"), Currency.getInstance("CZK"));
46 * Use the convertor from <code>createCZKtoUSD</code> method and do few conversions with it.
48 public void testCurrencyCZKUSD() throws Exception {
49 Convertor c = createCZKtoUSD();
51 // convert $5 to CZK using c:
52 double result = c.convert(5, Currency.getInstance("USD"));
53 assertEquals("Result is not 85 CZK", 85.0, result);
56 result = c.convert(8, Currency.getInstance("USD"));
57 assertEquals("Result is not 136 CZK", 136.0, result);
59 // convert 1003CZK to USD
60 result = c.convert(1003, Currency.getInstance("CZK"));
61 assertEquals("Result is not 59 USD", 59.0, result);
65 * Use the convertor from <code>createSKKtoCZK</code> method and do few conversions with it.
67 public void testCurrencySKKCZK() throws Exception {
68 Convertor c = createSKKtoCZK();
70 // convert 16CZK using c:
71 double result = c.convert(16, Currency.getInstance("CZK"));
72 assertEquals("Result is not 20 SKK", 20.0, result);
74 // convert 500SKK to CZK
75 result = c.convert(500, Currency.getInstance("SKK"));
76 assertEquals("Result is not 400 CZK", 400.0, result);