# HG changeset patch # User japod@localhost # Date 1222774795 -7200 # Node ID 5e73778cc1f1c5cbdb928242e4d8ec7305483d17 # Parent 61e4c4c120fde28e5b76cd7181a54c5be928ea02 finishing update of solution 06 to 1.5, removing 3 files diff -r 61e4c4c120fd -r 5e73778cc1f1 task1/solution06/src/org/apidesign/apifest08/currency/Bid.java --- a/task1/solution06/src/org/apidesign/apifest08/currency/Bid.java Tue Sep 30 12:24:45 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,74 +0,0 @@ -package org.apidesign.apifest08.currency; - -import java.math.BigDecimal; -import java.math.RoundingMode; -import java.util.Currency; -import static org.apidesign.apifest08.currency.Assert.*; - - -/** - * A bid representation. A did is defined as two currenncies and its value. - */ -public final class Bid { - private final Currency first; - private final Currency second; - private final BigDecimal bidValue; // a bid between the first currency and the second currency - public static final BigDecimal one = new BigDecimal(1); - - Bid(Currency first, Currency second, BigDecimal bid) { - notNull(first, "first"); - notNull(second, "second"); - this.first = first; - this.bidValue = bid; - this.second = second; - } - - Bid(Currency first, Currency second) { - notNull(first, "first"); - notNull(second, "second"); - this.first = first; - this.second = second; - this.bidValue = null; - } - - - BigDecimal getBidValue(Currency from, Currency to) { - if((from != first || from != second) || (to != first || to != second)) { - new IllegalArgumentException("This bid can be used only for: " + first + " " + second); - } - - notNull(bidValue, "bidValue"); - - BigDecimal retVal; - - if(first == from) { - retVal = bidValue; - } else { - //reverse bid - retVal = one.divide(bidValue, 10 ,RoundingMode.HALF_UP); - } - - return retVal; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + first.hashCode() + second.hashCode(); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - Bid other = (Bid) obj; - - return (this.first == other.first || this.first == other.second) && (this.second == other.second || this.second == other.first) ; - } -} \ No newline at end of file diff -r 61e4c4c120fd -r 5e73778cc1f1 task1/solution06/src/org/apidesign/apifest08/currency/ConvertorFactory.java --- a/task1/solution06/src/org/apidesign/apifest08/currency/ConvertorFactory.java Tue Sep 30 12:24:45 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,65 +0,0 @@ -package org.apidesign.apifest08.currency; - -import static org.apidesign.apifest08.currency.Assert.notNull; - -import java.math.BigDecimal; -import java.math.RoundingMode; -import java.util.ArrayList; -import java.util.Currency; -import java.util.List; - -/** - * The factory for {@link Convertor}. - * - * @see #newInstance() - */ -public final class ConvertorFactory { - //do not expose constructor - private ConvertorFactory(){} - - /** - * @return a convertor instance. - * - * @throws CurrencyException in a convertor cannot be instantiated - */ - public static Convertor newInstance() throws CurrencyException{ - return new DefaultConvertor(); - } - - private static final class DefaultConvertor extends Convertor { - private List bids; - - private DefaultConvertor() { - bids = new ArrayList(); - Bid usdCzk = new Bid(Currencies.USD, Currencies.CZK, new BigDecimal("17")); - bids.add(usdCzk); - Bid skCzk = new Bid(Currencies.SKK, Currencies.CZK, new BigDecimal("0.8")); - bids.add(skCzk); - } - - @Override - public Amount convert(Amount from, Currency targetCurrency) - throws CurrencyException { - return convert(from.getValue(), from.getCurrency(), targetCurrency); - - } - - @Override - public Amount convert(BigDecimal amount, Currency from, - Currency targetCurrency) throws ConversionException, - UnsupportedConversionException { - notNull(from, "from"); - notNull(targetCurrency, "targetCurrency"); - - int index = bids.indexOf(new Bid(from, targetCurrency)); - if(index == -1) { - throw new UnsupportedConversionException(from, targetCurrency); - } - Bid bid = bids.get(index); - BigDecimal bidValue = bid.getBidValue(from, targetCurrency); - BigDecimal result = bidValue.multiply(amount); - return new Amount(result, targetCurrency); - } - - } -} diff -r 61e4c4c120fd -r 5e73778cc1f1 task1/solution06/src/org/apidesign/apifest08/currency/Currencies.java --- a/task1/solution06/src/org/apidesign/apifest08/currency/Currencies.java Tue Sep 30 12:24:45 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,9 +0,0 @@ -package org.apidesign.apifest08.currency; - -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"); -}