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 |
|
japod@6
|
9 |
/**
|
japod@6
|
10 |
* Finish the Convertor API, and then write bodies of methods inside of this class to match the given tasks. To fullfil
|
japod@6
|
11 |
* your task, use the API define in the <code>org.apidesign.apifest08.currency</code> package. Do not you reflection, or
|
japod@6
|
12 |
* other hacks as your code shall run without any runtime permissions.
|
japod@6
|
13 |
*/
|
japod@6
|
14 |
public class Task1Test extends TestCase {
|
japod@6
|
15 |
public Task1Test(String testName) {
|
japod@6
|
16 |
super(testName);
|
japod@6
|
17 |
}
|
japod@6
|
18 |
|
japod@6
|
19 |
@Override
|
japod@6
|
20 |
protected void setUp() throws Exception {
|
japod@6
|
21 |
}
|
japod@6
|
22 |
|
japod@6
|
23 |
@Override
|
japod@6
|
24 |
protected void tearDown() throws Exception {
|
japod@6
|
25 |
}
|
japod@6
|
26 |
|
japod@6
|
27 |
/**
|
japod@6
|
28 |
* Create convertor that understands two currencies, CZK and USD. Make 1 USD == 17 CZK. Creation of the convertor
|
japod@6
|
29 |
* shall not require subclassing of any class or interface on the client side.
|
japod@6
|
30 |
* @return prepared convertor ready for converting USD to CZK and CZK to USD
|
japod@6
|
31 |
*/
|
japod@6
|
32 |
public static Convertor createCZKtoUSD() {
|
japod@6
|
33 |
return Convertor.getConvertorInstance(Currency.getInstance("CZK"), Currency.getInstance("USD"));
|
japod@6
|
34 |
}
|
japod@6
|
35 |
|
japod@6
|
36 |
/**
|
japod@6
|
37 |
* Create convertor that understands two currencies, CZK and SKK. Make 100 SKK == 80 CZK. Creation of the convertor
|
japod@6
|
38 |
* shall not require subclassing of any class or interface on the client side.
|
japod@6
|
39 |
* @return prepared convertor ready for converting SKK to CZK and CZK to SKK
|
japod@6
|
40 |
*/
|
japod@6
|
41 |
public static Convertor createSKKtoCZK() {
|
japod@6
|
42 |
return Convertor.getConvertorInstance(Currency.getInstance("SKK"), Currency.getInstance("CZK"));
|
japod@6
|
43 |
}
|
japod@6
|
44 |
|
japod@6
|
45 |
/**
|
japod@6
|
46 |
* Use the convertor from <code>createCZKtoUSD</code> method and do few conversions with it.
|
japod@6
|
47 |
*/
|
japod@6
|
48 |
public void testCurrencyCZKUSD() throws Exception {
|
japod@6
|
49 |
Convertor c = createCZKtoUSD();
|
japod@6
|
50 |
|
japod@6
|
51 |
// convert $5 to CZK using c:
|
japod@6
|
52 |
double result = c.convert(5, Currency.getInstance("USD"));
|
japod@6
|
53 |
assertEquals("Result is not 85 CZK", 85.0, result);
|
japod@6
|
54 |
|
japod@6
|
55 |
// convert $8 to CZK
|
japod@6
|
56 |
result = c.convert(8, Currency.getInstance("USD"));
|
japod@6
|
57 |
assertEquals("Result is not 136 CZK", 136.0, result);
|
japod@6
|
58 |
|
japod@6
|
59 |
// convert 1003CZK to USD
|
japod@6
|
60 |
result = c.convert(1003, Currency.getInstance("CZK"));
|
japod@6
|
61 |
assertEquals("Result is not 59 USD", 59.0, result);
|
japod@6
|
62 |
}
|
japod@6
|
63 |
|
japod@6
|
64 |
/**
|
japod@6
|
65 |
* Use the convertor from <code>createSKKtoCZK</code> method and do few conversions with it.
|
japod@6
|
66 |
*/
|
japod@6
|
67 |
public void testCurrencySKKCZK() throws Exception {
|
japod@6
|
68 |
Convertor c = createSKKtoCZK();
|
japod@6
|
69 |
|
japod@6
|
70 |
// convert 16CZK using c:
|
japod@6
|
71 |
double result = c.convert(16, Currency.getInstance("CZK"));
|
japod@6
|
72 |
assertEquals("Result is not 20 SKK", 20.0, result);
|
japod@6
|
73 |
|
japod@6
|
74 |
// convert 500SKK to CZK
|
japod@6
|
75 |
result = c.convert(500, Currency.getInstance("SKK"));
|
japod@6
|
76 |
assertEquals("Result is not 400 CZK", 400.0, result);
|
japod@6
|
77 |
}
|
japod@6
|
78 |
}
|