# HG changeset patch # User japod@localhost # Date 1222770285 -7200 # Node ID 61e4c4c120fde28e5b76cd7181a54c5be928ea02 # Parent 7ca97f802b5ade82ea0c821afb53fdcff25f63fc updating solution 06 to 1.5 diff -r 7ca97f802b5a -r 61e4c4c120fd task1/solution06/src/org/apidesign/apifest08/currency/Amount.java --- a/task1/solution06/src/org/apidesign/apifest08/currency/Amount.java Tue Sep 30 12:04:26 2008 +0200 +++ b/task1/solution06/src/org/apidesign/apifest08/currency/Amount.java Tue Sep 30 12:24:45 2008 +0200 @@ -1,9 +1,10 @@ package org.apidesign.apifest08.currency; +import static org.apidesign.apifest08.currency.Assert.notNull; + import java.math.BigDecimal; import java.math.RoundingMode; import java.util.Currency; -import static org.apidesign.apifest08.currency.Assert.*; /** * An amount representation. Amount is represented as composition of a value and diff -r 7ca97f802b5a -r 61e4c4c120fd task1/solution06/src/org/apidesign/apifest08/currency/Assert.java --- a/task1/solution06/src/org/apidesign/apifest08/currency/Assert.java Tue Sep 30 12:04:26 2008 +0200 +++ b/task1/solution06/src/org/apidesign/apifest08/currency/Assert.java Tue Sep 30 12:24:45 2008 +0200 @@ -1,9 +1,11 @@ package org.apidesign.apifest08.currency; -public class Assert { +public final class Assert { static void notNull(Object value, String argumentName) { if(value == null) { throw new IllegalArgumentException("The argument '" + argumentName + "' connot not be null"); } } } + + diff -r 7ca97f802b5a -r 61e4c4c120fd task1/solution06/src/org/apidesign/apifest08/currency/Convertor.java --- a/task1/solution06/src/org/apidesign/apifest08/currency/Convertor.java Tue Sep 30 12:04:26 2008 +0200 +++ b/task1/solution06/src/org/apidesign/apifest08/currency/Convertor.java Tue Sep 30 12:24:45 2008 +0200 @@ -1,23 +1,30 @@ package org.apidesign.apifest08.currency; +import static org.apidesign.apifest08.currency.Assert.notNull; + import java.math.BigDecimal; +import java.math.RoundingMode; import java.util.Currency; -public abstract class Convertor { - - /** - * Converts an amount to another amount according to a given currency. - * - * @param from a source - * @param toCurrency a target currency - * @return a converted amount - * @throws ConversionException if the conversion fails - * @throws UnsupportedConversionException if the conversion between a given currencies is not supported. - */ - public abstract Amount convert(Amount from, Currency toCurrency) throws ConversionException, UnsupportedConversionException; +public final class Convertor { + private final Currency first; + private final Currency second; + private final BigDecimal rateValue; // a rate between the first currency and the second currency + public static final BigDecimal one = new BigDecimal(1); + + public Convertor(BigDecimal rateValue, Currency currencyFirst, Currency currencySecond) { + notNull(currencyFirst, "currencyFirst"); + notNull(currencySecond, "currencySecond"); + notNull(rateValue, "rateValue"); + + this.rateValue = rateValue; + this.first = currencyFirst; + this.second = currencySecond; + } + /** - * Converts an amount value between two currencies. + * Converts an amount value between the two currencies of this converter. * * @param amount an amount * @param fromCurrency an amount currency @@ -27,6 +34,31 @@ * @throws ConversionException if the conversion fails * @throws UnsupportedConversionException if the conversion between a given currencies is not supported. */ - public abstract Amount convert(BigDecimal amount, Currency fromCurrency, Currency toCurrency) throws ConversionException, UnsupportedConversionException; + public Amount convert(BigDecimal amount, Currency fromCurrency, Currency toCurrency) throws ConversionException { + notNull(amount, "amount"); + notNull(fromCurrency, "fromCurrency"); + notNull(toCurrency, "toCurrency"); + + if((fromCurrency != first && fromCurrency != second) || (toCurrency != first && toCurrency != second)) { + throw new UnsupportedConversionException(fromCurrency, toCurrency); + } + BigDecimal rateValue = getRateValue(fromCurrency, toCurrency); + BigDecimal result = rateValue.multiply(amount); + return new Amount(result, toCurrency); + } + + private BigDecimal getRateValue(Currency fromCurrency, Currency toCurrency) { + + BigDecimal retVal; + + if(first == fromCurrency) { + retVal = rateValue; + } else { + //reverse rate + retVal = one.divide(rateValue, 10 ,RoundingMode.HALF_UP); + } + + return retVal; + } } diff -r 7ca97f802b5a -r 61e4c4c120fd task1/solution06/src/org/apidesign/apifest08/currency/UnsupportedConversionException.java --- a/task1/solution06/src/org/apidesign/apifest08/currency/UnsupportedConversionException.java Tue Sep 30 12:04:26 2008 +0200 +++ b/task1/solution06/src/org/apidesign/apifest08/currency/UnsupportedConversionException.java Tue Sep 30 12:24:45 2008 +0200 @@ -2,7 +2,7 @@ import java.util.Currency; -public class UnsupportedConversionException extends ConversionException{ +public final class UnsupportedConversionException extends ConversionException{ private static final long serialVersionUID = 1L; @@ -10,7 +10,7 @@ private Currency to; public UnsupportedConversionException(Currency from, Currency to) { - super("Conversion from the currency " + from + " to the currency " + to + " or vice versa in not supported yet. Missing bid."); + super("Conversion from the currency " + from + " to the currency " + to + " or vice versa in not supported."); this.from = from; this.to = to; } diff -r 7ca97f802b5a -r 61e4c4c120fd task1/solution06/test/org/apidesign/apifest08/test/Currencies.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/task1/solution06/test/org/apidesign/apifest08/test/Currencies.java Tue Sep 30 12:24:45 2008 +0200 @@ -0,0 +1,9 @@ +package org.apidesign.apifest08.test; + +import java.util.Currency; + +public class Currencies { + public static final Currency CZK = Currency.getInstance("CZK"); + public static final Currency SKK = Currency.getInstance("SKK"); + public static final Currency USD = Currency.getInstance("USD"); +} diff -r 7ca97f802b5a -r 61e4c4c120fd task1/solution06/test/org/apidesign/apifest08/test/Task1Test.java --- a/task1/solution06/test/org/apidesign/apifest08/test/Task1Test.java Tue Sep 30 12:04:26 2008 +0200 +++ b/task1/solution06/test/org/apidesign/apifest08/test/Task1Test.java Tue Sep 30 12:24:45 2008 +0200 @@ -1,16 +1,16 @@ package org.apidesign.apifest08.test; -import static org.apidesign.apifest08.currency.Currencies.CZK; -import static org.apidesign.apifest08.currency.Currencies.SKK; -import static org.apidesign.apifest08.currency.Currencies.USD; +import static org.apidesign.apifest08.test.Currencies.CZK; +import static org.apidesign.apifest08.test.Currencies.SKK; +import static org.apidesign.apifest08.test.Currencies.USD; import java.math.BigDecimal; import junit.framework.TestCase; import org.apidesign.apifest08.currency.Amount; +import org.apidesign.apifest08.currency.ConversionException; import org.apidesign.apifest08.currency.Convertor; -import org.apidesign.apifest08.currency.ConvertorFactory; import org.apidesign.apifest08.currency.UnsupportedConversionException; /** Finish the Convertor API, and then write bodies of methods inside @@ -41,7 +41,7 @@ * @return prepared convertor ready for converting USD to CZK and CZK to USD */ public static Convertor createCZKtoUSD() { - return ConvertorFactory.newInstance(); + return new Convertor(new BigDecimal(17), USD, CZK); } /** Create convertor that understands two currencies, CZK and @@ -53,7 +53,7 @@ * @return prepared convertor ready for converting SKK to CZK and CZK to SKK */ public static Convertor createSKKtoCZK() { - return ConvertorFactory.newInstance(); + return new Convertor(new BigDecimal("0.8"), SKK, CZK); } /** Use the convertor from createCZKtoUSD method and do few conversions @@ -88,14 +88,49 @@ assertEquals("Result is 400 CZK", 400, result.getValue().intValue()); } - public void testUnssuportedConversion(){ - Convertor c = ConvertorFactory.newInstance(); + + /** + * Verify that the CZK to USD convertor knows nothing about SKK. + */ + public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception { + Convertor c = createCZKtoUSD(); + // convert $5 to SKK, the API shall say this is not possible + try { + c.convert(new BigDecimal(5), USD, SKK); + fail("convert $5 to SKK, the API shall say this is not possible"); + } catch (ConversionException e) { + //expected + } + + // convert 500 SKK to CZK, the API shall say this is not possible + + try { + c.convert(new BigDecimal("500"), SKK, CZK); + fail("convert 500 SKK to CZK, the API shall say this is not possible"); + } catch (ConversionException e) { + //expected + } + } + + /** + * Verify that the CZK to SKK convertor knows nothing about USD. + */ + public void testCannotConvertToSKKwithCZKSKKConvertor() throws Exception { + Convertor c = createSKKtoCZK(); + // convert $5 to SKK, the API shall say this is not possible try { c.convert(new BigDecimal(5), USD, SKK); - fail(); - } catch(UnsupportedConversionException e) { - //expected - } - } + fail("convert $5 to SKK, the API shall say this is not possible"); + } catch(ConversionException e) { + //expected + } + + try { + c.convert(new BigDecimal(500), CZK, USD); + fail("convert 500 CZK to USD, the API shall say this is not possible"); + } catch(ConversionException e) { + //expected + } + } }