japod@33
|
1 |
package org.apidesign.apifest08.test;
|
japod@33
|
2 |
|
japod@33
|
3 |
import java.util.Currency;
|
japod@33
|
4 |
|
japod@33
|
5 |
import junit.framework.TestCase;
|
japod@33
|
6 |
|
japod@33
|
7 |
import org.apidesign.apifest08.currency.Convertor;
|
japod@33
|
8 |
import org.apidesign.apifest08.currency.exceptions.UnknownConvertorException;
|
japod@33
|
9 |
|
japod@33
|
10 |
/** There are many currencies around the world and many banks manipulate
|
japod@33
|
11 |
* with more than one or two at the same time. As banks are usually the
|
japod@33
|
12 |
* best paying clients, which is true even in case of your Convertor API,
|
japod@33
|
13 |
* it is reasonable to listen to their requests.
|
japod@33
|
14 |
* <p>
|
japod@33
|
15 |
* The quest for today is to enhance your existing convertor API to hold
|
japod@33
|
16 |
* information about many currencies and allow conversions between any of them.
|
japod@33
|
17 |
* Also, as conversion rates for diferent currencies usually arise from various
|
japod@33
|
18 |
* bank departments, there is another important need. There is a need to
|
japod@33
|
19 |
* compose two convertors into one by merging all the information about
|
japod@33
|
20 |
* currencies they know about.
|
japod@33
|
21 |
*/
|
japod@33
|
22 |
public class Task2Test extends TestCase {
|
japod@33
|
23 |
public Task2Test(String testName) {
|
japod@33
|
24 |
super(testName);
|
japod@33
|
25 |
}
|
japod@33
|
26 |
|
japod@33
|
27 |
@Override
|
japod@33
|
28 |
protected void setUp() throws Exception {
|
japod@33
|
29 |
}
|
japod@33
|
30 |
|
japod@33
|
31 |
@Override
|
japod@33
|
32 |
protected void tearDown() throws Exception {
|
japod@33
|
33 |
}
|
japod@33
|
34 |
|
japod@33
|
35 |
// As in Task1Test, keep in mind, that there are three parts
|
japod@33
|
36 |
// of the whole system:
|
japod@33
|
37 |
// 1. there is someone who knows the current exchange rate
|
japod@33
|
38 |
// 2. there is someone who wants to do the conversion
|
japod@33
|
39 |
// 3. there is the API between 1. and 2. which allows them to communicate
|
japod@33
|
40 |
//
|
japod@33
|
41 |
// Please backward compatibly enhance your existing API to support following
|
japod@33
|
42 |
// usecases:
|
japod@33
|
43 |
//
|
japod@33
|
44 |
|
japod@33
|
45 |
/** Create convertor that understands two currencies, CZK and
|
japod@33
|
46 |
* SKK. Make 100 SKK == 75 CZK. This is method for the group of users that
|
japod@33
|
47 |
* knows the exchange rate, and needs to use the API to create objects
|
japod@33
|
48 |
* with the exchange rate. Anyone shall be ready to call this method without
|
japod@33
|
49 |
* any other method being called previously. The API itself shall know
|
japod@33
|
50 |
* nothing about any rates, before this method is called.
|
japod@33
|
51 |
*/
|
japod@33
|
52 |
public static Convertor createTripleConvertor() {
|
japod@33
|
53 |
// Rates: 1USD = 15CZK
|
japod@33
|
54 |
// Rates: 1USD = 20SKK
|
japod@33
|
55 |
// Rates: 75CZK = 100SKK
|
japod@33
|
56 |
// set exchange rates
|
japod@33
|
57 |
Convertor.setConvertorRates(Currency.getInstance("USD"), Currency.getInstance("CZK"), 15, 1);
|
japod@33
|
58 |
Convertor.setConvertorRates(Currency.getInstance("USD"), Currency.getInstance("SKK"), 20, 1);
|
japod@33
|
59 |
Convertor.setConvertorRates(Currency.getInstance("SKK"), Currency.getInstance("CZK"), 75, 100);
|
japod@33
|
60 |
|
japod@33
|
61 |
// create new instance
|
japod@33
|
62 |
Convertor convertor = null;
|
japod@33
|
63 |
try {
|
japod@33
|
64 |
convertor = Convertor.getConvertorInstance(Currency.getInstance("USD"), Currency.getInstance("SKK"), Currency.getInstance("CZK"));
|
japod@33
|
65 |
} catch (UnknownConvertorException e) {
|
japod@33
|
66 |
e.printStackTrace();
|
japod@33
|
67 |
}
|
japod@33
|
68 |
return convertor;
|
japod@33
|
69 |
}
|
japod@33
|
70 |
|
japod@33
|
71 |
/** Define convertor that understands three currencies. Use it.
|
japod@33
|
72 |
*/
|
japod@33
|
73 |
public void testConvertorForUSDandCZKandSKK() throws Exception {
|
japod@33
|
74 |
Convertor c = createTripleConvertor();
|
japod@33
|
75 |
|
japod@33
|
76 |
// convert $5 to CZK using c:
|
japod@33
|
77 |
double result = c.convert(5d, Currency.getInstance("USD"), Currency.getInstance("CZK"));
|
japod@33
|
78 |
assertEquals("Result is not 75 CZK", 75.0, result);
|
japod@33
|
79 |
|
japod@33
|
80 |
// convert $5 to SKK using c:
|
japod@33
|
81 |
result = c.convert(5d, Currency.getInstance("USD"), Currency.getInstance("SKK"));
|
japod@33
|
82 |
assertEquals("Result is not 100 SKK", 100.0, result);
|
japod@33
|
83 |
|
japod@33
|
84 |
// convert 200SKK to CZK using c:
|
japod@33
|
85 |
result = c.convert(200d, Currency.getInstance("SKK"), Currency.getInstance("CZK"));
|
japod@33
|
86 |
assertEquals("Result is not 150 CZK", 150.0, result);
|
japod@33
|
87 |
|
japod@33
|
88 |
// convert 200SKK to USK using c:
|
japod@33
|
89 |
result = c.convert(200d, Currency.getInstance("SKK"), Currency.getInstance("USD"));
|
japod@33
|
90 |
assertEquals("Result is not 10 USD", 10.0, result);
|
japod@33
|
91 |
}
|
japod@33
|
92 |
|
japod@33
|
93 |
/** Merge all currency rates of convertor 1 with convertor 2.
|
japod@33
|
94 |
* Implement this using your API, preferably this method just delegates
|
japod@33
|
95 |
* into some API method which does the actual work, without requiring
|
japod@33
|
96 |
* API clients to code anything complex.
|
japod@33
|
97 |
*/
|
japod@33
|
98 |
public static Convertor merge(Convertor one, Convertor two) {
|
japod@33
|
99 |
return one.merge(two);
|
japod@33
|
100 |
}
|
japod@33
|
101 |
|
japod@33
|
102 |
/** Join the convertors from previous task, Task1Test and show that it
|
japod@33
|
103 |
* can be used to do reasonable conversions.
|
japod@33
|
104 |
*/
|
japod@33
|
105 |
public void testConvertorComposition() throws Exception {
|
japod@33
|
106 |
Convertor c = merge(
|
japod@33
|
107 |
Task1Test.createCZKtoUSD(),
|
japod@33
|
108 |
Task1Test.createSKKtoCZK()
|
japod@33
|
109 |
);
|
japod@33
|
110 |
|
japod@33
|
111 |
// convert $5 to CZK using c:
|
japod@33
|
112 |
double result = c.convert(5d, Currency.getInstance("USD"), Currency.getInstance("CZK"));
|
japod@33
|
113 |
assertEquals("Result is not 85 CZK", 85.0, result);
|
japod@33
|
114 |
|
japod@33
|
115 |
// convert $8 to CZK using c:
|
japod@33
|
116 |
result = c.convert(8d, Currency.getInstance("USD"), Currency.getInstance("CZK"));
|
japod@33
|
117 |
assertEquals("Result is not 136 CZK", 136.0, result);
|
japod@33
|
118 |
|
japod@33
|
119 |
// convert 1003CZK to USD using c:
|
japod@33
|
120 |
result = c.convert(1003d, Currency.getInstance("CZK"), Currency.getInstance("USD"));
|
japod@33
|
121 |
assertEquals("Result is not 59 USD", 59.0, result);
|
japod@33
|
122 |
|
japod@33
|
123 |
// convert 16CZK using c:
|
japod@33
|
124 |
result = c.convert(16d, Currency.getInstance("CZK"), Currency.getInstance("SKK"));
|
japod@33
|
125 |
assertEquals("Result is not 20 SKK", 20.0, result);
|
japod@33
|
126 |
|
japod@33
|
127 |
// convert 500SKK to CZK using c:
|
japod@33
|
128 |
result = c.convert(500d, Currency.getInstance("SKK"), Currency.getInstance("CZK"));
|
japod@33
|
129 |
assertEquals("Result is not 400 CZK", 400.0, result);
|
japod@33
|
130 |
|
japod@33
|
131 |
}
|
japod@33
|
132 |
}
|