japod@6
|
1 |
package org.apidesign.apifest08.test;
|
japod@6
|
2 |
|
japod@6
|
3 |
import junit.framework.*;
|
japod@6
|
4 |
import org.apidesign.apifest08.currency.*;
|
japod@6
|
5 |
|
japod@6
|
6 |
import java.util.*;
|
japod@6
|
7 |
|
japod@6
|
8 |
/** Finish the Convertor API, and then write bodies of methods inside
|
japod@6
|
9 |
* of this class to match the given tasks. To fullfil your task, use the
|
japod@6
|
10 |
* API define in the <code>org.apidesign.apifest08.currency</code> package.
|
japod@6
|
11 |
* Do not you reflection, or other hacks as your code
|
japod@6
|
12 |
* shall run without any runtime permissions.
|
japod@6
|
13 |
*/
|
japod@6
|
14 |
public class Task1Test extends TestCase {
|
japod@6
|
15 |
|
japod@6
|
16 |
private static final String
|
japod@6
|
17 |
USD = "USD",
|
japod@6
|
18 |
CZK = "CZK",
|
japod@6
|
19 |
SKK = "SKK";
|
japod@6
|
20 |
|
japod@6
|
21 |
private static final Currency
|
japod@6
|
22 |
USD2 = Currency.getInstance(USD),
|
japod@6
|
23 |
CZK2 = Currency.getInstance(CZK),
|
japod@6
|
24 |
SKK2 = Currency.getInstance(SKK);
|
japod@6
|
25 |
|
japod@6
|
26 |
public Task1Test(String testName) {
|
japod@6
|
27 |
super(testName);
|
japod@6
|
28 |
}
|
japod@6
|
29 |
|
japod@6
|
30 |
@Override
|
japod@6
|
31 |
protected void setUp() throws Exception {
|
japod@6
|
32 |
}
|
japod@6
|
33 |
|
japod@6
|
34 |
@Override
|
japod@6
|
35 |
protected void tearDown() throws Exception {
|
japod@6
|
36 |
}
|
japod@6
|
37 |
|
japod@6
|
38 |
/** Create convertor that understands two currencies, CZK and
|
japod@6
|
39 |
* USD. Make 1 USD == 17 CZK.
|
japod@6
|
40 |
*
|
japod@6
|
41 |
* Creation of the convertor shall not require subclassing of any class
|
japod@6
|
42 |
* or interface on the client side.
|
japod@6
|
43 |
*
|
japod@6
|
44 |
* @return prepared convertor ready for converting USD to CZK and CZK to USD
|
japod@6
|
45 |
*/
|
japod@6
|
46 |
public static CurrencyConverter createCZKtoUSD() {
|
japod@6
|
47 |
return OfflineConverterProvider.getInstance().getConverter(17, CZK, 1, USD);
|
japod@6
|
48 |
}
|
japod@6
|
49 |
|
japod@6
|
50 |
public static CurrencyConverter createCZKtoUSD2() {
|
japod@6
|
51 |
return OfflineConverterProvider.getInstance().getConverter(CZK, USD);
|
japod@6
|
52 |
}
|
japod@6
|
53 |
|
japod@6
|
54 |
/** Create convertor that understands two currencies, CZK and
|
japod@6
|
55 |
* SKK. Make 100 SKK == 80 CZK.
|
japod@6
|
56 |
*
|
japod@6
|
57 |
* Creation of the convertor shall not require subclassing of any class
|
japod@6
|
58 |
* or interface on the client side.
|
japod@6
|
59 |
*
|
japod@6
|
60 |
* @return prepared convertor ready for converting SKK to CZK and CZK to SKK
|
japod@6
|
61 |
*/
|
japod@6
|
62 |
public static CurrencyConverter createSKKtoCZK() {
|
japod@6
|
63 |
return OfflineConverterProvider.getInstance().getConverter(100, SKK, 80, CZK);
|
japod@6
|
64 |
}
|
japod@6
|
65 |
|
japod@6
|
66 |
public static CurrencyConverter createSKKtoCZK2() {
|
japod@6
|
67 |
return OfflineConverterProvider.getInstance().getConverter(SKK, CZK);
|
japod@6
|
68 |
}
|
japod@6
|
69 |
|
japod@6
|
70 |
/** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
|
japod@6
|
71 |
* with it.
|
japod@6
|
72 |
*/
|
japod@6
|
73 |
public void testCurrencyCZKUSD() throws Exception {
|
japod@6
|
74 |
CurrencyConverter c = createCZKtoUSD();
|
japod@6
|
75 |
testCZKUSD(c);
|
japod@6
|
76 |
|
japod@6
|
77 |
// test without specifying rates
|
japod@6
|
78 |
c = createCZKtoUSD2();
|
japod@6
|
79 |
testCZKUSD(c);
|
japod@6
|
80 |
}
|
japod@6
|
81 |
|
japod@6
|
82 |
private void testCZKUSD(CurrencyConverter c) {
|
japod@6
|
83 |
double czk, usd;
|
japod@6
|
84 |
// convert $5 to CZK using c:
|
japod@6
|
85 |
// assertEquals("Result is 85 CZK");
|
japod@6
|
86 |
czk = c.convert(5, USD, CZK);
|
japod@6
|
87 |
assertEquals(85.0, czk, 0.0);
|
japod@6
|
88 |
czk = c.convert(5, USD2, CZK2);
|
japod@6
|
89 |
assertEquals(85.0, czk, 0.0);
|
japod@6
|
90 |
|
japod@6
|
91 |
// convert $8 to CZK
|
japod@6
|
92 |
// assertEquals("Result is 136 CZK");
|
japod@6
|
93 |
czk = c.convert(8, USD, CZK);
|
japod@6
|
94 |
assertEquals(136.0, czk, 0.0);
|
japod@6
|
95 |
czk = c.convert(8, USD2, CZK2);
|
japod@6
|
96 |
assertEquals(136.0, czk, 0.0);
|
japod@6
|
97 |
|
japod@6
|
98 |
// convert 1003CZK to USD
|
japod@6
|
99 |
// assertEquals("Result is 59 USD");
|
japod@6
|
100 |
usd = c.convert(1003, CZK, USD);
|
japod@6
|
101 |
assertEquals(59.0, usd, 0.0);
|
japod@6
|
102 |
usd = c.convert(1003, CZK2, USD2);
|
japod@6
|
103 |
assertEquals(59.0, usd, 0.0);
|
japod@6
|
104 |
}
|
japod@6
|
105 |
|
japod@6
|
106 |
/** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
|
japod@6
|
107 |
* with it.
|
japod@6
|
108 |
*/
|
japod@6
|
109 |
public void testCurrencySKKCZK() throws Exception {
|
japod@6
|
110 |
CurrencyConverter c = createSKKtoCZK();
|
japod@6
|
111 |
testCZKSKK(c);
|
japod@6
|
112 |
|
japod@6
|
113 |
// test without specifying rates
|
japod@6
|
114 |
c = createSKKtoCZK2();
|
japod@6
|
115 |
testCZKSKK(c);
|
japod@6
|
116 |
}
|
japod@6
|
117 |
|
japod@6
|
118 |
private void testCZKSKK(CurrencyConverter c) {
|
japod@6
|
119 |
double czk, skk;
|
japod@6
|
120 |
// convert 16CZK using c:
|
japod@6
|
121 |
// assertEquals("Result is 20 SKK");
|
japod@6
|
122 |
skk = c.convert(16, CZK, SKK);
|
japod@6
|
123 |
assertEquals(20.0, skk, 0.0);
|
japod@6
|
124 |
skk = c.convert(16, CZK2, SKK2);
|
japod@6
|
125 |
assertEquals(20.0, skk, 0.0);
|
japod@6
|
126 |
|
japod@6
|
127 |
// convert 500SKK to CZK
|
japod@6
|
128 |
// assertEquals("Result is 400 CZK");
|
japod@6
|
129 |
czk = c.convert(500, SKK, CZK);
|
japod@6
|
130 |
assertEquals(400.0, czk, 0.0);
|
japod@6
|
131 |
czk = c.convert(500, SKK2, CZK2);
|
japod@6
|
132 |
assertEquals(400.0, czk, 0.0);
|
japod@6
|
133 |
}
|
japod@6
|
134 |
} |