1 package org.apidesign.apifest08.test;
3 import static org.apidesign.apifest08.test.Currencies.CZK;
4 import static org.apidesign.apifest08.test.Currencies.USD;
5 import static org.apidesign.apifest08.test.Currencies.SKK;
7 import java.math.BigDecimal;
9 import junit.framework.TestCase;
10 import org.apidesign.apifest08.currency.Convertor;
11 import org.apidesign.apifest08.test.Task1Test;
13 /** There are many currencies around the world and many banks manipulate
14 * with more than one or two at the same time. As banks are usually the
15 * best paying clients, which is true even in case of your Convertor API,
16 * it is reasonable to listen to their requests.
18 * The quest for today is to enhance your existing convertor API to hold
19 * information about many currencies and allow conversions between any of them.
20 * Also, as conversion rates for diferent currencies usually arise from various
21 * bank departments, there is another important need. There is a need to
22 * compose two convertors into one by merging all the information about
23 * currencies they know about.
25 public class Task2Test extends TestCase {
26 public Task2Test(String testName) {
31 protected void setUp() throws Exception {
35 protected void tearDown() throws Exception {
38 // As in Task1Test, keep in mind, that there are three parts
39 // of the whole system:
40 // 1. there is someone who knows the current exchange rate
41 // 2. there is someone who wants to do the conversion
42 // 3. there is the API between 1. and 2. which allows them to communicate
44 // Please backward compatibly enhance your existing API to support following
48 /** Create convertor that understands two currencies, CZK and
49 * SKK. Make 100 SKK == 75 CZK. This is method for the group of users that
50 * knows the exchange rate, and needs to use the API to create objects
51 * with the exchange rate. Anyone shall be ready to call this method without
52 * any other method being called previously. The API itself shall know
53 * nothing about any rates, before this method is called.
55 public static Convertor createTripleConvertor() {
56 // Rates: 1USD = 15CZK
57 // Rates: 1USD = 20SKK
58 // Rates: 75CZK = 100SKK
59 Convertor usdCzk = new Convertor(new BigDecimal(15), USD, CZK);
60 Convertor usdSkk = new Convertor(new BigDecimal(20), USD, SKK);
61 Convertor skkCzk = new Convertor(new BigDecimal("0.75"), SKK, CZK);
62 return new Convertor(new Convertor[]{usdCzk, usdSkk, skkCzk});
65 /** Define convertor that understands three currencies. Use it.
67 public void testConvertorForUSDandCZKandSKK() throws Exception {
68 Convertor c = createTripleConvertor();
70 // convert $5 to CZK using c:
71 assertEquals("Result is 75 CZK", 75 ,c.convert(new BigDecimal(5), USD, CZK).getValue().intValue());
73 // convert $5 to SKK using c:
74 assertEquals("Result is 100 SKK", 100, c.convert(new BigDecimal(5), USD, SKK).getValue().intValue());
76 // convert 200SKK to CZK using c:
77 assertEquals("Result is 150 CZK", 150, c.convert(new BigDecimal(200), SKK, CZK).getValue().intValue());
79 // convert 200SKK to USK using c:
80 assertEquals("Result is 10 USD", 10, c.convert(new BigDecimal(200), SKK, USD).getValue().intValue());
83 /** Merge all currency rates of convertor 1 with convertor 2.
84 * Implement this using your API, preferably this method just delegates
85 * into some API method which does the actual work, without requiring
86 * API clients to code anything complex.
88 public static Convertor merge(Convertor one, Convertor two) {
89 return new Convertor(new Convertor[]{one, two});
92 /** Join the convertors from previous task, Task1Test and show that it
93 * can be used to do reasonable conversions.
95 public void testConvertorComposition() throws Exception {
97 Task1Test.createCZKtoUSD(),
98 Task1Test.createSKKtoCZK()
101 // convert $5 to CZK using c:
102 assertEquals("Result is 85 CZK", 85, c.convert(new BigDecimal(5), USD, CZK).getValue().intValue());
104 // convert $8 to CZK using c:
105 assertEquals("Result is 136 CZK", 136, c.convert(new BigDecimal(8), USD, CZK).getValue().intValue());
107 // convert 1003CZK to USD using c:
108 assertEquals("Result is 59 USD", 59, c.convert(new BigDecimal(1003), CZK, USD).getValue().intValue());
110 // convert 16CZK using c:
111 assertEquals("Result is 20 SKK", 20, c.convert(new BigDecimal(16), CZK, SKK).getValue().intValue());
113 // convert 500SKK to CZK using c:
114 assertEquals("Result is 400 CZK", 400, c.convert(new BigDecimal(500), SKK, CZK).getValue().intValue());