1 package org.apidesign.apifest08.test;
3 import junit.framework.TestCase;
4 import org.apidesign.apifest08.currency.Convertor;
5 import org.apidesign.apifest08.currency.ConvertorFactory;
6 import org.apidesign.apifest08.currency.CurrencyRate;
7 import org.apidesign.apifest08.currency.Rate;
9 /** The exchange rates are not always the same. They are changing. Day by day,
10 * hour by hour, minute by minute. For every bank it is important to always
11 * have the actual exchange rate available in the system. That is why let's
12 * create a pluggable convertor that will always have up to date value of its
15 * The quest for today is to allow 3rd party developer to write a convertor
16 * that adjusts its exchange rate everytime it is queried. This convertor is
17 * written by independent vendor, the vendor knows only your Convertor API,
18 * he does not know how the whole system looks and how the convertor is supposed
21 public class Task3Test extends TestCase {
22 public Task3Test(String testName) {
27 protected void setUp() throws Exception {
31 protected void tearDown() throws Exception {
34 // Backward compatibly enhance your existing API to support following
39 /** Without knowing anything about the surrounding system, write an
40 * implementation of convertor that will return different rates everytime
41 * it is queried. Convert USD to CZK and vice versa. Start with the rate of
42 * 1USD = 16CZK and adjust it in favor of CZK by 0.01 CZK with every query.
43 * As soon as you reach 1USD = 15CZK adjust it by 0.01 CZK in favor of USD
44 * until you reach 1USD = 16CZK
46 * @return new instance of "online" USD and CZK convertor starting with rate 1USD = 16CZK
48 public static Convertor createOnlineCZKUSDConvertor() {
49 // initial rate: 1USD = 16CZK
50 // 2nd query 1USD = 15.99CZK
51 // 3rd query 1USD = 15.98CZK
52 // until 1USD = 15.00CZK
53 // then 1USD = 15.01CZK
54 // then 1USD = 15.02CZK
55 // and so on and on up to 1USD = 16CZK
56 // and then another round to 15, etc.
57 CurrencyRate onlineCurrencyRate = new CurrencyRate() {
58 private int usdAmount = 100;
59 private int czkAmount = 1600;
60 private boolean up = false;
61 public String getCurrency1() {
65 public String getCurrency2() {
69 public Rate getRate() { //return Rate according to online status
70 Rate rate = new Rate(usdAmount, czkAmount);
72 if (czkAmount < 1600) {
79 if (czkAmount > 1500) {
90 return ConvertorFactory.newInstance().createConvertor(onlineCurrencyRate);
93 public void testFewQueriesForOnlineConvertor() {
94 Convertor c = createOnlineCZKUSDConvertor();
95 doFewQueriesForOnlineConvertor(c);
98 static void doFewQueriesForOnlineConvertor(Convertor c) {
99 // convert $5 to CZK using c:
100 //assertEquals("Result is 80 CZK");
101 assertEquals("Result is 80 CZK", 80.0, c.convert("USD", "CZK", 5), 0.001);
103 // convert $8 to CZK using c:
104 //assertEquals("Result is 127.92 CZK");
105 assertEquals("Result is 127.92 CZK", 127.92, c.convert("USD", "CZK", 8), 0.001);
107 // convert $1 to CZK using c:
108 //assertEquals("Result is 15.98 CZK");
109 assertEquals("Result is 15.98 CZK", 15.98, c.convert("USD", "CZK", 1), 0.001);
111 // convert 15.97CZK to USD using c:
112 //assertEquals("Result is 1$");
113 assertEquals("Result is 1$", 1.0, c.convert("CZK", "USD", 15.97), 0.001);
117 /** Join the convertors and show they behave sane.
119 public void testOnlineConvertorComposition() throws Exception {
120 Convertor c = Task2Test.merge(
121 createOnlineCZKUSDConvertor(),
122 Task1Test.createSKKtoCZK()
125 // convert 16CZK to SKK using c:
126 // assertEquals("Result is 20 SKK");
127 assertEquals("Result is 20 SKK", 20.0, c.convert("CZK", "SKK", 16), 0.001);
129 // convert 500SKK to CZK using c:
130 // assertEquals("Result is 400 CZK");
131 assertEquals("Result is 400 CZK", 400.0, c.convert("SKK", "CZK", 500), 0.001);
133 doFewQueriesForOnlineConvertor(c);