1.1 --- a/build.xml Wed Oct 01 11:03:45 2008 +0200
1.2 +++ b/build.xml Wed Oct 01 11:23:11 2008 +0200
1.3 @@ -3,7 +3,6 @@
1.4 <property name="apifest.dir" location="${ant.file.samples}/.."/>
1.5 <dirset id="examples" dir="${apifest.dir}">
1.6 <include name="**/nbproject"/>
1.7 - <exclude name="currency/nbproject"/>
1.8 <exclude name="taskx/jtulach/against-solutionXY/nbproject"/>
1.9 <exclude name=".hg/**"/>
1.10 </dirset>
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/currency/test/org/apidesign/apifest08/test/Task2Test.java Wed Oct 01 11:23:11 2008 +0200
2.3 @@ -0,0 +1,107 @@
2.4 +package org.apidesign.apifest08.test;
2.5 +
2.6 +import junit.framework.TestCase;
2.7 +import org.apidesign.apifest08.currency.Convertor;
2.8 +
2.9 +/** There are many currencies around the world and many banks manipulate
2.10 + * with more than one or two at the same time. As banks are usually the
2.11 + * best paying clients, which is true even in case of your Convertor API,
2.12 + * it is reasonable to listen to their requests.
2.13 + * <p>
2.14 + * The quest for today is to enhance your existing convertor API to hold
2.15 + * information about many currencies and allow conversions between any of them.
2.16 + * Also, as conversion rates for diferent currencies usually arise from various
2.17 + * bank departments, there is another important need. There is a need to
2.18 + * compose two convertors into one by merging all the information about
2.19 + * currencies they know about.
2.20 + */
2.21 +public class Task2Test extends TestCase {
2.22 + public Task2Test(String testName) {
2.23 + super(testName);
2.24 + }
2.25 +
2.26 + @Override
2.27 + protected void setUp() throws Exception {
2.28 + }
2.29 +
2.30 + @Override
2.31 + protected void tearDown() throws Exception {
2.32 + }
2.33 +
2.34 + // As in Task1Test, keep in mind, that there are three parts
2.35 + // of the whole system:
2.36 + // 1. there is someone who knows the current exchange rate
2.37 + // 2. there is someone who wants to do the conversion
2.38 + // 3. there is the API between 1. and 2. which allows them to communicate
2.39 + //
2.40 + // Please backward compatibly enhance your existing API to support following
2.41 + // usecases:
2.42 + //
2.43 +
2.44 + /** Create convertor that understands two currencies, CZK and
2.45 + * SKK. Make 100 SKK == 75 CZK. This is method for the group of users that
2.46 + * knows the exchange rate, and needs to use the API to create objects
2.47 + * with the exchange rate. Anyone shall be ready to call this method without
2.48 + * any other method being called previously. The API itself shall know
2.49 + * nothing about any rates, before this method is called.
2.50 + */
2.51 + public static Convertor createTripleConvertor() {
2.52 + // Rates: 1USD = 15CZK
2.53 + // Rates: 1USD = 20SKK
2.54 + // Rates: 75CZK = 100SKK
2.55 + return null;
2.56 + }
2.57 +
2.58 + /** Define convertor that understands three currencies. Use it.
2.59 + */
2.60 + public void testConvertorForUSDandCZKandSKK() throws Exception {
2.61 + Convertor c = createTripleConvertor();
2.62 +
2.63 + // convert $5 to CZK using c:
2.64 + // assertEquals("Result is 75 CZK");
2.65 +
2.66 + // convert $5 to SKK using c:
2.67 + // assertEquals("Result is 100 SKK");
2.68 +
2.69 + // convert 200SKK to CZK using c:
2.70 + // assertEquals("Result is 150 CZK");
2.71 +
2.72 + // convert 200SKK to USK using c:
2.73 + // assertEquals("Result is 10 USD");
2.74 + }
2.75 +
2.76 + /** Merge all currency rates of convertor 1 with convertor 2.
2.77 + * Implement this using your API, preferably this method just delegates
2.78 + * into some API method which does the actual work, without requiring
2.79 + * API clients to code anything complex.
2.80 + */
2.81 + public static Convertor merge(Convertor one, Convertor two) {
2.82 + return null;
2.83 + }
2.84 +
2.85 + /** Join the convertors from previous task, Task1Test and show that it
2.86 + * can be used to do reasonable conversions.
2.87 + */
2.88 + public void testConvertorComposition() throws Exception {
2.89 + Convertor c = merge(
2.90 + Task1Test.createCZKtoUSD(),
2.91 + Task1Test.createSKKtoCZK()
2.92 + );
2.93 +
2.94 + // convert $5 to CZK using c:
2.95 + // assertEquals("Result is 85 CZK");
2.96 +
2.97 + // convert $8 to CZK using c:
2.98 + // assertEquals("Result is 136 CZK");
2.99 +
2.100 + // convert 1003CZK to USD using c:
2.101 + // assertEquals("Result is 59 USD");
2.102 +
2.103 + // convert 16CZK using c:
2.104 + // assertEquals("Result is 20 SKK");
2.105 +
2.106 + // convert 500SKK to CZK using c:
2.107 + // assertEquals("Result is 400 CZK");
2.108 +
2.109 + }
2.110 +}