# HG changeset patch # User japod@localhost # Date 1223331663 -7200 # Node ID 8898c620fe964142a70cdbc5f4c760df0c79f0ed # Parent 3a18aae85c9e96707f75d6b2de9446079c1bd712 adding solution04 for task2 diff -r 3a18aae85c9e -r 8898c620fe96 task2/solution04/src/org/apidesign/apifest08/currency/CompositeConvertorImpl.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/task2/solution04/src/org/apidesign/apifest08/currency/CompositeConvertorImpl.java Tue Oct 07 00:21:03 2008 +0200 @@ -0,0 +1,314 @@ +package org.apidesign.apifest08.currency; + + +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.Collections; +import java.util.Currency; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + + +/** + * A composite convertor allows conversions between many currencies by forwarding conversion requests to stored convertors. + * A composite convertor will build all possible conversions that are allowed by the underlying set of convertors. + * + * @author D'Arcy Smith + * @verson 1.0 + */ +final class CompositeConvertorImpl + implements Convertor +{ + /** + * The convertors that are supported. + */ + private final Convertor[] convertors; + + /** + * Keeps track of what convertors to use to convert between currencies. + */ + private final Map> possibleConversions; + + { + possibleConversions = new HashMap>(); + } + + /** + * Construct a ComositeConvertorImpl with the specified convertors. + * This will result in all possible conversions between the supplied currencies being made. + * + * @param cs the convertors to use. + * @throws IllegalArgumentException if any of the items in cs are null. + */ + CompositeConvertorImpl(Convertor ... cs) + { + int newConvertors; + + convertors = cs; + + // track all of the known conversion + for(final Convertor convertor : convertors) + { + final Set currencies; + Map possible; + + if(convertor == null) + { + throw new IllegalArgumentException("cs cannot contain null"); + } + + currencies = convertor.getCurrencies(); + + for(final Currency currency : currencies) + { + possible = possibleConversions.get(currency); + + if(possible == null) + { + possible = new HashMap(); + possibleConversions.put(currency, possible); + } + + for(final Currency c : currencies) + { + possible.put(c, convertor); + } + } + } + + // make up conversions that can be derived... eg: + // we have: + // USD <-> CAD + // CAD <-> CZK + // SSK <-> GBP + // we can derive: + // USD <-> CZK + // we cannot derive: + // USD <-> GBP + // CAD <-> GBP + // CZK <-> GBP + do + { + newConvertors = 0; + + // todo... need to loop this until all the ones that can be handled are done. + for(final Currency from : getCurrencies()) + { + for(final Currency to : getCurrencies()) + { + if(!(canConvert(from, to))) + { + final Set fromCurrencies; + final Set toCurrencies; + final Set common; + + fromCurrencies = possibleConversions.get(from).keySet(); + toCurrencies = possibleConversions.get(to).keySet(); + common = new HashSet(); + + for(final Currency currency : fromCurrencies) + { + if(toCurrencies.contains(currency)) + { + common.add(currency); + } + } + + for(final Currency currency : common) + { + final Convertor convertor; + + convertor = createConvertor(from, to, currency); + possibleConversions.get(from).put(to, convertor); + possibleConversions.get(to).put(from, convertor); + newConvertors++; + } + } + } + } + } + while(newConvertors > 0); + } + + /** + * Check to see if converting between the two currencies is possible. + * + * @param from the currency to convert from. + * @param to the currency to convert to. + * @return true if the conversion is possible. + * @throws IllegalArgumentException if either from or to are null. + */ + public boolean canConvert(final Currency from, final Currency to) + { + final Map possible; + + if(from == null) + { + throw new IllegalArgumentException("from cannot be null"); + } + + if(to == null) + { + throw new IllegalArgumentException("to cannot be null"); + } + + possible = possibleConversions.get(from); + + if(possible.containsKey(to)) + { + return (true); + } + + return (false); + } + + /** + * Get the currencies that the convertor supports. Just because a currency is + * supported does not mean that canConvert will return true. + * + * @return the supported currencies. + */ + public Set getCurrencies() + { + final Set currencies; + + currencies = possibleConversions.keySet(); + + return (Collections.unmodifiableSet(currencies)); + } + + /** + * Get the conversion rate between two currencies. + * + * @param from the currency to convert from. + * @param to the currency to convert to. + * @return the conversion rate between the two currencies. + * @throws IllegalArgumentException if either from or to is null. + * @throws InvalidConversionException if canConvert would return false. + */ + public BigDecimal getConversionRate(final Currency from, final Currency to) + throws InvalidConversionException + { + final Map possible; + Convertor convertor; + + if(from == null) + { + throw new IllegalArgumentException("from cannot be null"); + } + + if(to == null) + { + throw new IllegalArgumentException("to cannot be null"); + } + + if(!(canConvert(from, to))) + { + throw new InvalidConversionException("cannot convert", to); + } + + possible = possibleConversions.get(from); + convertor = possible.get(to); + + if(convertor == null) + { + throw new Error(); + } + + return (convertor.getConversionRate(from, to)); + } + + /** + * Convert an amount from one currency to another. + * + * @param from the currency to convert from. + * @param to the currency to convert to. + * @param amount the amount to convert. + * @return the converted amount. + * @throws IllegalArgumentException if any of the arguments are null. + * @throws InvalidConversionException if either from or to are not valid for the convertor. + */ + public BigDecimal convert(final Currency from, + final Currency to, + final BigDecimal amount) + throws InvalidConversionException + { + final BigDecimal result; + + if(amount == null) + { + throw new IllegalArgumentException("amount cannot be null"); + } + + if(from == null) + { + throw new IllegalArgumentException("from cannot be null"); + } + + if(to == null) + { + throw new IllegalArgumentException("to cannot be null"); + } + + result = amount.multiply(getConversionRate(from, to)); + + return (result.setScale(2, RoundingMode.HALF_DOWN)); + } + + /** + * Create a convertor between two currencies using another currency that is able to convert between both. + * + * @param from the currency to convert from. + * @param to the currency to convert to. + * @param intermediary the currency to use as a go-between. + * @return a Convertor that is able to convert between from an to. + * @throws IllegalArgumentException if any of the arguments are null. + */ + private Convertor createConvertor(final Currency from, + final Currency to, + final Currency intermediary) + { + final Convertor fromIntermediary; + final Convertor toIntermediary; + + if(from == null) + { + throw new IllegalArgumentException("from cannot be null"); + } + + if(to == null) + { + throw new IllegalArgumentException("to cannot be null"); + } + + if(intermediary == null) + { + throw new IllegalArgumentException("intermediary cannot be null"); + } + + fromIntermediary = possibleConversions.get(from).get(intermediary); + toIntermediary = possibleConversions.get(to).get(intermediary); + + try + { + final BigDecimal fromRate; + final BigDecimal toRate; + final BigDecimal rate; + final Convertor convertor; + + fromRate = fromIntermediary.getConversionRate(from, intermediary); + toRate = toIntermediary.getConversionRate(intermediary, to); + rate = fromRate.multiply(toRate); + + convertor = ConvertorFactory.getConvertor(from, BigDecimal.ONE, to, rate); + + return (convertor); + } + catch (InvalidConversionException ex) + { + throw new Error(); + } + } +} diff -r 3a18aae85c9e -r 8898c620fe96 task2/solution04/src/org/apidesign/apifest08/currency/ConverterImpl.java --- a/task2/solution04/src/org/apidesign/apifest08/currency/ConverterImpl.java Tue Oct 07 00:19:37 2008 +0200 +++ b/task2/solution04/src/org/apidesign/apifest08/currency/ConverterImpl.java Tue Oct 07 00:21:03 2008 +0200 @@ -4,7 +4,10 @@ import java.math.BigDecimal; import java.math.MathContext; import java.math.RoundingMode; +import java.util.Collections; import java.util.Currency; +import java.util.HashSet; +import java.util.Set; /** @@ -128,12 +131,59 @@ throw new InvalidConversionException("cannot convert to: " + to.getCurrencyCode(), to, currencyA, currencyB); } - // converting between the same currency is no converstion at all. + result = amount.multiply(getConversionRate(from, to)); + + return (result.setScale(2, RoundingMode.HALF_DOWN)); + } + + /** + * Check to see if converting between the two currencies is possible. + * + * @param from the currency to convert from. + * @param to the currency to convert to. + * @return true if the conversion is possible. + */ + public boolean canConvert(final Currency from, final Currency to) + { + return ((from.equals(currencyA) || from.equals(currencyB)) && + (to.equals(currencyA) || to.equals(currencyB))); + } + + /** + * Get the currencies that the convertor supports. + * + * @return the supported currencies. + */ + public Set getCurrencies() + { + final Set currencies; + + currencies = new HashSet(); + currencies.add(currencyA); + currencies.add(currencyB); + + return (Collections.unmodifiableSet(currencies)); + } + + /** + * Get the conversion rate between two currencies. + * + * @param from the currency to convert from. + * @param to the currency to convert to. + * @return the conversion rate between the two currencies. + * @throws InvalidConversionException if canConvert would return false. + */ + public BigDecimal getConversionRate(final Currency from, + final Currency to) + throws InvalidConversionException + { + final BigDecimal rate; + if(from.equals(to)) { - result = amount; + rate = BigDecimal.ONE; } - else + else { final BigDecimal rateX; final BigDecimal rateY; @@ -150,10 +200,83 @@ rateY = currencyARate; } - temp = amount.divide(rateX, MathContext.DECIMAL32); - result = temp.multiply(rateY); + temp = BigDecimal.ONE.divide(rateX, MathContext.DECIMAL64); + rate = temp.multiply(rateY); + } + + return (rate.setScale(20, RoundingMode.HALF_EVEN)); + } + + /** + * Check to see if two ConvertorImpls are equal. + * + * @param obj the object to check + * @return if the ConvertorImpls are not the same (cuyrrencies and rates). + */ + @Override + public boolean equals(Object obj) + { + if (obj == null) + { + return false; + } + + if (getClass() != obj.getClass()) + { + return false; } - return (result.setScale(2, RoundingMode.HALF_DOWN)); + final ConvertorImpl other = (ConvertorImpl) obj; + + // it would be nice if NetBeans could chck to see if the variable is final and guaranteed not to be null... but that + // would likely be tricky... but in a NetBeans engineer reads that see if you can do it :-) + if (this.currencyA != other.currencyA && (this.currencyA == null || !this.currencyA.equals(other.currencyA))) + { + return false; + } + + if (this.currencyB != other.currencyB && (this.currencyB == null || !this.currencyB.equals(other.currencyB))) + { + return false; + } + + if (this.currencyARate != other.currencyARate && (this.currencyARate == null || !this.currencyARate.equals(other.currencyARate))) + { + return false; + } + + if (this.currencyBRate != other.currencyBRate && (this.currencyBRate == null || !this.currencyBRate.equals(other.currencyBRate))) + { + return false; + } + + return true; + } + + /** + * Get the hashCode of the Convertor. + * + * @return the hashCode of the convertor. + */ + @Override + public int hashCode() + { + int hash = 7; + hash = 37 * hash + (this.currencyA != null ? this.currencyA.hashCode() : 0); + hash = 37 * hash + (this.currencyB != null ? this.currencyB.hashCode() : 0); + hash = 37 * hash + (this.currencyARate != null ? this.currencyARate.hashCode() : 0); + hash = 37 * hash + (this.currencyBRate != null ? this.currencyBRate.hashCode() : 0); + return hash; + } + + /** + * Get the currencyCode of both currencies. + * + * @return the currency codes of both currencies. + */ + @Override + public String toString() + { + return (currencyA.getCurrencyCode() + " to " + currencyB.getCurrencyCode()); } } diff -r 3a18aae85c9e -r 8898c620fe96 task2/solution04/src/org/apidesign/apifest08/currency/Convertor.java --- a/task2/solution04/src/org/apidesign/apifest08/currency/Convertor.java Tue Oct 07 00:19:37 2008 +0200 +++ b/task2/solution04/src/org/apidesign/apifest08/currency/Convertor.java Tue Oct 07 00:21:03 2008 +0200 @@ -3,6 +3,7 @@ import java.math.BigDecimal; import java.util.Currency; +import java.util.Set; /** @@ -27,4 +28,32 @@ Currency to, BigDecimal amount) throws InvalidConversionException; + + /** + * Check to see if converting between the two currencies is possible. + * + * @param from the currency to convert from. + * @param to the currency to convert to. + * @return true if the conversion is possible. + */ + boolean canConvert(Currency from, Currency to); + + /** + * Get the currencies that the convertor supports. Just because a currency is + * supported does not mean that canConvert will return true. + * + * @return the supported currencies. + */ + Set getCurrencies(); + + /** + * Get the conversion rate between two currencies. + * + * @param from the currency to convert from. + * @param to the currency to convert to. + * @return the conversion rate between the two currencies. + * @throws InvalidConversionException if canConvert would return false. + */ + BigDecimal getConversionRate(final Currency from, final Currency to) + throws InvalidConversionException; } diff -r 3a18aae85c9e -r 8898c620fe96 task2/solution04/src/org/apidesign/apifest08/currency/ConvertorFactory.java --- a/task2/solution04/src/org/apidesign/apifest08/currency/ConvertorFactory.java Tue Oct 07 00:19:37 2008 +0200 +++ b/task2/solution04/src/org/apidesign/apifest08/currency/ConvertorFactory.java Tue Oct 07 00:21:03 2008 +0200 @@ -1,10 +1,7 @@ package org.apidesign.apifest08.currency; -import java.lang.ref.WeakReference; import java.math.BigDecimal; import java.util.Currency; -import java.util.Map; -import java.util.WeakHashMap; /** @@ -15,15 +12,15 @@ */ public final class ConvertorFactory { - /** + /* * flyweight so that only one vestion of each converter is created at a time. - */ private final static Map> convertors; static { convertors = new WeakHashMap>(); } + */ /** * Prevent accidental construction. @@ -74,7 +71,7 @@ final Currency b, final BigDecimal bRate) { - final String key; + // final String key; Convertor convertor; if(a == null) @@ -97,6 +94,7 @@ throw new IllegalArgumentException("bRate cannot be null"); } + /* key = a.getCurrencyCode() + aRate + b.getCurrencyCode() + bRate; // make sure that we don't try to overwrite one @@ -107,10 +105,68 @@ convertor = new ConvertorImpl(a, aRate, b, bRate); convertors.put(key, new WeakReference(convertor)); } + + convertor = convertors.get(key).get(); } + */ + + convertor = new ConvertorImpl(a, aRate, b, bRate); + + return (convertor); + } + + public static Convertor mergeConvertors(final Convertor ... cs) + { + Convertor convertor; + + /* + final String key; - convertor = convertors.get(key).get(); + // ISSUE: only takes into account the names... not the rates... + key = getKey(cs); + + // make sure that we don't try to overwrite one + synchronized(convertors) + { + if(!(convertors.containsKey(key))) + { + convertor = new CompositeConvertorImpl(cs); + convertors.put(key, new WeakReference(convertor)); + } + + convertor = convertors.get(key).get(); + } + */ + + convertor = new CompositeConvertorImpl(cs); return (convertor); } + + /* + private static String getKey(final Convertor ... cs) + { + final Set currencies; + final StringBuilder builder; + + currencies = new HashSet(); + + for(final Convertor convertor : cs) + { + final Set c; + + c = convertor.getCurrencies(); + currencies.addAll(c); + } + + builder = new StringBuilder(); + + for(final Currency currency : currencies) + { + builder.append(currency.getCurrencyCode()); + } + + return (builder.toString()); + } + */ } diff -r 3a18aae85c9e -r 8898c620fe96 task2/solution04/src/org/apidesign/apifest08/currency/InvalidConversionException.java --- a/task2/solution04/src/org/apidesign/apifest08/currency/InvalidConversionException.java Tue Oct 07 00:19:37 2008 +0200 +++ b/task2/solution04/src/org/apidesign/apifest08/currency/InvalidConversionException.java Tue Oct 07 00:21:03 2008 +0200 @@ -28,18 +28,31 @@ */ private final Currency currencyB; + /** - * Construct a new InvalidConversionException wit the specified message. + * Construct a new InvalidConversionException with the specified message. + * + * @param msg the message for getMessage. + * @param bad the currency that is not valid. + */ + public InvalidConversionException(final String msg, + final Currency bad) + { + this(msg, bad, null, null); + } + + /** + * Construct a new InvalidConversionException with the specified message. * * @param msg the message for getMessage. * @param bad the currency that is not valid. * @param a a valid currency. * @param b a valid currency. */ - public InvalidConversionException(final String msg, - final Currency bad, - final Currency a, - final Currency b) + public InvalidConversionException(final String msg, + final Currency bad, + final Currency a, + final Currency b) { super(msg); diff -r 3a18aae85c9e -r 8898c620fe96 task2/solution04/test/org/apidesign/apifest08/test/Task1Test.java --- a/task2/solution04/test/org/apidesign/apifest08/test/Task1Test.java Tue Oct 07 00:19:37 2008 +0200 +++ b/task2/solution04/test/org/apidesign/apifest08/test/Task1Test.java Tue Oct 07 00:21:03 2008 +0200 @@ -3,6 +3,7 @@ import java.math.BigDecimal; import java.util.Currency; +import java.util.Set; import junit.framework.TestCase; import org.apidesign.apifest08.currency.Convertor; import org.apidesign.apifest08.currency.ConvertorFactory; @@ -50,7 +51,8 @@ */ public static Convertor createCZKtoUSD() { - return (ConvertorFactory.getConvertor("CZK", BigDecimal.valueOf(17.0), "USD", BigDecimal.valueOf(1))); + return (ConvertorFactory.getConvertor("CZK", BigDecimal.valueOf(17.0), + "USD", BigDecimal.valueOf(1))); } /** Create convertor that understands two currencies, CZK and @@ -63,7 +65,8 @@ */ public static Convertor createSKKtoCZK() { - return (ConvertorFactory.getConvertor(Currency.getInstance("SKK"), BigDecimal.valueOf(100), Currency.getInstance("CZK"), BigDecimal.valueOf(80))); + return (ConvertorFactory.getConvertor(Currency.getInstance("SKK"), BigDecimal.valueOf(100), + Currency.getInstance("CZK"), BigDecimal.valueOf(80))); } /** Use the convertor from createCZKtoUSD method and do few conversions @@ -180,5 +183,41 @@ assertEquals(CZK, ex.getCurrencyB()); } } + + public void testGetCurrencies() + { + Convertor c; + Set currencies; + + c = createSKKtoCZK(); + currencies = c.getCurrencies(); + assertEquals(2, currencies.size()); + assertTrue(currencies.contains(Currency.getInstance("SKK"))); + assertTrue(currencies.contains(Currency.getInstance("CZK"))); + + c = createCZKtoUSD(); + currencies = c.getCurrencies(); + assertEquals(2, currencies.size()); + assertTrue(currencies.contains(Currency.getInstance("USD"))); + assertTrue(currencies.contains(Currency.getInstance("CZK"))); + } + + public void testGetConverstionRate() + throws InvalidConversionException + { + Convertor c; + + c = createSKKtoCZK(); + assertEquals(1.0, c.getConversionRate(Currency.getInstance("CZK"), Currency.getInstance("CZK")).doubleValue()); + assertEquals(1.0, c.getConversionRate(Currency.getInstance("SKK"), Currency.getInstance("SKK")).doubleValue()); + assertEquals(0.80, c.getConversionRate(Currency.getInstance("SKK"), Currency.getInstance("CZK")).doubleValue()); + assertEquals(1.25, c.getConversionRate(Currency.getInstance("CZK"), Currency.getInstance("SKK")).doubleValue()); + + c = createCZKtoUSD(); + assertEquals(1.0, c.getConversionRate(Currency.getInstance("CZK"), Currency.getInstance("CZK")).doubleValue()); + assertEquals(1.0, c.getConversionRate(Currency.getInstance("USD"), Currency.getInstance("USD")).doubleValue()); + assertEquals(1.0/17.0, c.getConversionRate(Currency.getInstance("CZK"), Currency.getInstance("USD")).doubleValue(), 0.00000000000000001); + assertEquals(17.0, c.getConversionRate(Currency.getInstance("USD"), Currency.getInstance("CZK")).doubleValue(), 0.00000000000000001); + } } diff -r 3a18aae85c9e -r 8898c620fe96 task2/solution04/test/org/apidesign/apifest08/test/Task2Test.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/task2/solution04/test/org/apidesign/apifest08/test/Task2Test.java Tue Oct 07 00:21:03 2008 +0200 @@ -0,0 +1,179 @@ +package org.apidesign.apifest08.test; + +import java.math.BigDecimal; +import java.util.Currency; +import java.util.Set; +import junit.framework.TestCase; +import org.apidesign.apifest08.currency.Convertor; +import org.apidesign.apifest08.currency.ConvertorFactory; +import org.apidesign.apifest08.currency.InvalidConversionException; + + +/** There are many currencies around the world and many banks manipulate + * with more than one or two at the same time. As banks are usually the + * best paying clients, which is true even in case of your Convertor API, + * it is reasonable to listen to their requests. + *

+ * The quest for today is to enhance your existing convertor API to hold + * information about many currencies and allow conversions between any of them. + * Also, as conversion rates for diferent currencies usually arise from various + * bank departments, there is another important need. There is a need to + * compose two convertors into one by merging all the information about + * currencies they know about. + */ +public class Task2Test extends TestCase +{ + private final static Currency CZK; + private final static Currency SKK; + private final static Currency USD; + + static + { + CZK = Currency.getInstance("CZK"); + SKK = Currency.getInstance("SKK"); + USD = Currency.getInstance("USD"); + } + + public Task2Test(String testName) + { + super(testName); + } + + @Override + protected void setUp() + throws Exception + { + } + + @Override + protected void tearDown() + throws Exception + { + } + + // As in Task1Test, keep in mind, that there are three parts + // of the whole system: + // 1. there is someone who knows the current exchange rate + // 2. there is someone who wants to do the conversion + // 3. there is the API between 1. and 2. which allows them to communicate + // + // Please backward compatibly enhance your existing API to support following + // usecases: + // + + /** Create convertor that understands two currencies, CZK and + * SKK. Make 100 SKK == 75 CZK. This is method for the group of users that + * knows the exchange rate, and needs to use the API to create objects + * with the exchange rate. Anyone shall be ready to call this method without + * any other method being called previously. The API itself shall know + * nothing about any rates, before this method is called. + */ + public static Convertor createTripleConvertor() { + // Rates: 1USD = 15CZK + // Rates: 1USD = 20SKK + // Rates: 75CZK = 100SKK + Convertor c = ConvertorFactory.mergeConvertors( + ConvertorFactory.getConvertor(USD, BigDecimal.ONE, CZK, BigDecimal.valueOf(15.00)), + ConvertorFactory.getConvertor(USD, BigDecimal.ONE, SKK, BigDecimal.valueOf(20.00)) + ); + + return c; + } + + /** Define convertor that understands three currencies. Use it. + */ + public void testConvertorForUSDandCZKandSKK() throws Exception { + Convertor c = createTripleConvertor(); + + // convert $5 to CZK using c: + // assertEquals("Result is 75 CZK"); + assertEquals(new BigDecimal("75.00"), c.convert(USD, CZK, BigDecimal.valueOf(5.00))); + + // convert $5 to SKK using c: + // assertEquals("Result is 100 SKK"); + assertEquals(new BigDecimal("100.00"), c.convert(USD, SKK, BigDecimal.valueOf(5.00))); + + // convert 200SKK to CZK using c: + // assertEquals("Result is 150 CZK"); + assertEquals(new BigDecimal("150.00"), c.convert(SKK, CZK, BigDecimal.valueOf(200.00))); + + // convert 200SKK to USK using c: + // assertEquals("Result is 10 USD"); + assertEquals(new BigDecimal("10.00"), c.convert(SKK, USD, BigDecimal.valueOf(200.00))); + } + + /** Merge all currency rates of convertor 1 with convertor 2. + * Implement this using your API, preferably this method just delegates + * into some API method which does the actual work, without requiring + * API clients to code anything complex. + */ + public static Convertor merge(Convertor one, Convertor two) { + return ConvertorFactory.mergeConvertors(one, two); + } + + /** Join the convertors from previous task, Task1Test and show that it + * can be used to do reasonable conversions. + */ + public void testConvertorComposition() throws Exception { + Convertor c = merge( + Task1Test.createCZKtoUSD(), + Task1Test.createSKKtoCZK() + ); + + // convert $5 to CZK using c: + // assertEquals("Result is 85 CZK"); + assertEquals(new BigDecimal("85.00"), c.convert(USD, CZK, BigDecimal.valueOf(5.00))); + + // convert $8 to CZK using c: + // assertEquals("Result is 136 CZK"); + assertEquals(new BigDecimal("136.00"), c.convert(USD, CZK, BigDecimal.valueOf(8.00))); + + // convert 1003CZK to USD using c: + // assertEquals("Result is 59 USD"); + assertEquals(new BigDecimal("59.00"), c.convert(CZK, USD, BigDecimal.valueOf(1003.00))); + + // convert 16CZK using c: + // assertEquals("Result is 20 SKK"); + assertEquals(new BigDecimal("20.00"), c.convert(CZK, SKK, BigDecimal.valueOf(16.00))); + + // convert 500SKK to CZK using c: + // assertEquals("Result is 400 CZK"); + assertEquals(new BigDecimal("400.00"), c.convert(SKK, CZK, BigDecimal.valueOf(500.00))); + } + + public void testGetCurrencies() + { + Convertor c = merge( + Task1Test.createCZKtoUSD(), + Task1Test.createSKKtoCZK() + ); + Set currencies; + + currencies = c.getCurrencies(); + assertEquals(3, currencies.size()); + assertTrue(currencies.contains(Currency.getInstance("SKK"))); + assertTrue(currencies.contains(Currency.getInstance("CZK"))); + assertTrue(currencies.contains(Currency.getInstance("USD"))); + } + + public void testGetConverstionRate() + throws InvalidConversionException + { + Convertor c = merge( + Task1Test.createCZKtoUSD(), + Task1Test.createSKKtoCZK() + ); + + assertEquals(1.0, c.getConversionRate(USD, USD).doubleValue(), 0.0000000000000001); + assertEquals(17.0, c.getConversionRate(USD, CZK).doubleValue(), 0.0000000000000001); + assertEquals(21.25, c.getConversionRate(USD, SKK).doubleValue(), 0.0000000000000001); + + assertEquals(1.0 / 17.0, c.getConversionRate(CZK, USD).doubleValue(), 0.0000000000000001); + assertEquals(1.0, c.getConversionRate(CZK, CZK).doubleValue(), 0.0000000000000001); + assertEquals(1.25, c.getConversionRate(CZK, SKK).doubleValue(), 0.0000000000000001); + + assertEquals(0.04705882352941176, c.getConversionRate(SKK, USD).doubleValue(), 0.0000000000000001); + assertEquals(0.8, c.getConversionRate(SKK, CZK).doubleValue(), 0.0000000000000001); + assertEquals(1.0, c.getConversionRate(SKK, SKK).doubleValue(), 0.0000000000000001); + } +}