1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/task4/solution06/test/org/apidesign/apifest08/test/Task2Test.java Sat Oct 11 23:38:46 2008 +0200
1.3 @@ -0,0 +1,117 @@
1.4 +package org.apidesign.apifest08.test;
1.5 +
1.6 +import static org.apidesign.apifest08.test.Currencies.CZK;
1.7 +import static org.apidesign.apifest08.test.Currencies.USD;
1.8 +import static org.apidesign.apifest08.test.Currencies.SKK;
1.9 +
1.10 +import java.math.BigDecimal;
1.11 +
1.12 +import junit.framework.TestCase;
1.13 +import org.apidesign.apifest08.currency.Convertor;
1.14 +import org.apidesign.apifest08.test.Task1Test;
1.15 +
1.16 +/** There are many currencies around the world and many banks manipulate
1.17 + * with more than one or two at the same time. As banks are usually the
1.18 + * best paying clients, which is true even in case of your Convertor API,
1.19 + * it is reasonable to listen to their requests.
1.20 + * <p>
1.21 + * The quest for today is to enhance your existing convertor API to hold
1.22 + * information about many currencies and allow conversions between any of them.
1.23 + * Also, as conversion rates for diferent currencies usually arise from various
1.24 + * bank departments, there is another important need. There is a need to
1.25 + * compose two convertors into one by merging all the information about
1.26 + * currencies they know about.
1.27 + */
1.28 +public class Task2Test extends TestCase {
1.29 + public Task2Test(String testName) {
1.30 + super(testName);
1.31 + }
1.32 +
1.33 + @Override
1.34 + protected void setUp() throws Exception {
1.35 + }
1.36 +
1.37 + @Override
1.38 + protected void tearDown() throws Exception {
1.39 + }
1.40 +
1.41 + // As in Task1Test, keep in mind, that there are three parts
1.42 + // of the whole system:
1.43 + // 1. there is someone who knows the current exchange rate
1.44 + // 2. there is someone who wants to do the conversion
1.45 + // 3. there is the API between 1. and 2. which allows them to communicate
1.46 + //
1.47 + // Please backward compatibly enhance your existing API to support following
1.48 + // usecases:
1.49 + //
1.50 +
1.51 + /** Create convertor that understands two currencies, CZK and
1.52 + * SKK. Make 100 SKK == 75 CZK. This is method for the group of users that
1.53 + * knows the exchange rate, and needs to use the API to create objects
1.54 + * with the exchange rate. Anyone shall be ready to call this method without
1.55 + * any other method being called previously. The API itself shall know
1.56 + * nothing about any rates, before this method is called.
1.57 + */
1.58 + public static Convertor createTripleConvertor() {
1.59 + // Rates: 1USD = 15CZK
1.60 + // Rates: 1USD = 20SKK
1.61 + // Rates: 75CZK = 100SKK
1.62 + Convertor usdCzk = new Convertor(new BigDecimal(15), USD, CZK);
1.63 + Convertor usdSkk = new Convertor(new BigDecimal(20), USD, SKK);
1.64 + Convertor skkCzk = new Convertor(new BigDecimal("0.75"), SKK, CZK);
1.65 + return new Convertor(new Convertor[]{usdCzk, usdSkk, skkCzk});
1.66 + }
1.67 +
1.68 + /** Define convertor that understands three currencies. Use it.
1.69 + */
1.70 + public void testConvertorForUSDandCZKandSKK() throws Exception {
1.71 + Convertor c = createTripleConvertor();
1.72 +
1.73 + // convert $5 to CZK using c:
1.74 + assertEquals("Result is 75 CZK", 75 ,c.convert(new BigDecimal(5), USD, CZK).getValue().intValue());
1.75 +
1.76 + // convert $5 to SKK using c:
1.77 + assertEquals("Result is 100 SKK", 100, c.convert(new BigDecimal(5), USD, SKK).getValue().intValue());
1.78 +
1.79 + // convert 200SKK to CZK using c:
1.80 + assertEquals("Result is 150 CZK", 150, c.convert(new BigDecimal(200), SKK, CZK).getValue().intValue());
1.81 +
1.82 + // convert 200SKK to USK using c:
1.83 + assertEquals("Result is 10 USD", 10, c.convert(new BigDecimal(200), SKK, USD).getValue().intValue());
1.84 + }
1.85 +
1.86 + /** Merge all currency rates of convertor 1 with convertor 2.
1.87 + * Implement this using your API, preferably this method just delegates
1.88 + * into some API method which does the actual work, without requiring
1.89 + * API clients to code anything complex.
1.90 + */
1.91 + public static Convertor merge(Convertor one, Convertor two) {
1.92 + return new Convertor(new Convertor[]{one, two});
1.93 + }
1.94 +
1.95 + /** Join the convertors from previous task, Task1Test and show that it
1.96 + * can be used to do reasonable conversions.
1.97 + */
1.98 + public void testConvertorComposition() throws Exception {
1.99 + Convertor c = merge(
1.100 + Task1Test.createCZKtoUSD(),
1.101 + Task1Test.createSKKtoCZK()
1.102 + );
1.103 +
1.104 + // convert $5 to CZK using c:
1.105 + assertEquals("Result is 85 CZK", 85, c.convert(new BigDecimal(5), USD, CZK).getValue().intValue());
1.106 +
1.107 + // convert $8 to CZK using c:
1.108 + assertEquals("Result is 136 CZK", 136, c.convert(new BigDecimal(8), USD, CZK).getValue().intValue());
1.109 +
1.110 + // convert 1003CZK to USD using c:
1.111 + assertEquals("Result is 59 USD", 59, c.convert(new BigDecimal(1003), CZK, USD).getValue().intValue());
1.112 +
1.113 + // convert 16CZK using c:
1.114 + assertEquals("Result is 20 SKK", 20, c.convert(new BigDecimal(16), CZK, SKK).getValue().intValue());
1.115 +
1.116 + // convert 500SKK to CZK using c:
1.117 + assertEquals("Result is 400 CZK", 400, c.convert(new BigDecimal(500), SKK, CZK).getValue().intValue());
1.118 +
1.119 + }
1.120 +}