1 package org.apidesign.apifest08.test;
3 import junit.framework.TestCase;
4 import org.apidesign.apifest08.currency.Convertor;
6 /** There are many currencies around the world and many banks manipulate
7 * with more than one or two at the same time. As banks are usually the
8 * best paying clients, which is true even in case of your Convertor API,
9 * it is reasonable to listen to their requests.
11 * The quest for today is to enhance your existing convertor API to hold
12 * information about many currencies and allow conversions between any of them.
13 * Also, as conversion rates for diferent currencies usually arise from various
14 * bank departments, there is another important need. There is a need to
15 * compose two convertors into one by merging all the information about
16 * currencies they know about.
18 public class Task2Test extends TestCase {
19 public Task2Test(String testName) {
24 protected void setUp() throws Exception {
28 protected void tearDown() throws Exception {
31 // As in Task1Test, keep in mind, that there are three parts
32 // of the whole system:
33 // 1. there is someone who knows the current exchange rate
34 // 2. there is someone who wants to do the conversion
35 // 3. there is the API between 1. and 2. which allows them to communicate
37 // Please backward compatibly enhance your existing API to support following
41 /** Create convertor that understands two currencies, CZK and
42 * SKK. Make 100 SKK == 75 CZK. This is method for the group of users that
43 * knows the exchange rate, and needs to use the API to create objects
44 * with the exchange rate. Anyone shall be ready to call this method without
45 * any other method being called previously. The API itself shall know
46 * nothing about any rates, before this method is called.
48 public static Convertor createTripleConvertor() {
49 // Rates: 1USD = 15CZK
50 // Rates: 1USD = 20SKK
51 // Rates: 75CZK = 100SKK
55 /** Define convertor that understands three currencies. Use it.
57 public void testConvertorForUSDandCZKandSKK() throws Exception {
58 Convertor c = createTripleConvertor();
60 // convert $5 to CZK using c:
61 // assertEquals("Result is 75 CZK");
63 // convert $5 to SKK using c:
64 // assertEquals("Result is 100 SKK");
66 // convert 200SKK to CZK using c:
67 // assertEquals("Result is 150 CZK");
69 // convert 200SKK to USK using c:
70 // assertEquals("Result is 10 USD");
73 /** Merge all currency rates of convertor 1 with convertor 2.
74 * Implement this using your API, preferably this method just delegates
75 * into some API method which does the actual work, without requiring
76 * API clients to code anything complex.
78 public static Convertor merge(Convertor one, Convertor two) {
82 /** Join the convertors from previous task, Task1Test and show that it
83 * can be used to do reasonable conversions.
85 public void testConvertorComposition() throws Exception {
87 Task1Test.createCZKtoUSD(),
88 Task1Test.createSKKtoCZK()
91 // convert $5 to CZK using c:
92 // assertEquals("Result is 85 CZK");
94 // convert $8 to CZK using c:
95 // assertEquals("Result is 136 CZK");
97 // convert 1003CZK to USD using c:
98 // assertEquals("Result is 59 USD");
100 // convert 16CZK using c:
101 // assertEquals("Result is 20 SKK");
103 // convert 500SKK to CZK using c:
104 // assertEquals("Result is 400 CZK");