task1/solution04/src/org/apidesign/apifest08/currency/Convertor.java
changeset 17 37c9921c653e
parent 6 97662396c0fd
     1.1 --- a/task1/solution04/src/org/apidesign/apifest08/currency/Convertor.java	Sun Sep 28 14:12:38 2008 +0200
     1.2 +++ b/task1/solution04/src/org/apidesign/apifest08/currency/Convertor.java	Tue Sep 30 11:50:09 2008 +0200
     1.3 @@ -2,8 +2,6 @@
     1.4  
     1.5  
     1.6  import java.math.BigDecimal;
     1.7 -import java.math.MathContext;
     1.8 -import java.math.RoundingMode;
     1.9  import java.util.Currency;
    1.10  
    1.11  
    1.12 @@ -13,93 +11,20 @@
    1.13   * @author D'Arcy Smith
    1.14   * @version 1.0
    1.15   */
    1.16 -public final class Convertor
    1.17 +public interface Convertor
    1.18  {
    1.19      /**
    1.20 -     * The currency to cvonvert from.
    1.21 -     */
    1.22 -    private final Currency currencyA;
    1.23 -
    1.24 -    /**
    1.25 -     * The currency to cvonvert from.
    1.26 -     */
    1.27 -    private final Currency currencyB;
    1.28 -    
    1.29 -    /**
    1.30 -     * Constructs a convertor with the specified currencies.
    1.31 +     * Convert an amount from one currency to another.
    1.32       * 
    1.33 -     * @param a the currency to convert from.
    1.34 -     * @param b the currency to convert to.
    1.35 -     * @throws IllegalArgumentException if either a or b are null.
    1.36 -     */
    1.37 -    public Convertor(final Currency a,
    1.38 -                     final Currency b)
    1.39 -    {
    1.40 -        if(a == null)
    1.41 -        {
    1.42 -            throw new IllegalArgumentException("a cannot be null");
    1.43 -        }
    1.44 -
    1.45 -        if(b == null)
    1.46 -        {
    1.47 -            throw new IllegalArgumentException("a cannot be null");
    1.48 -        }
    1.49 -
    1.50 -        currencyA = a;
    1.51 -        currencyB = b;
    1.52 -    }
    1.53 -    
    1.54 -    /**
    1.55 -     * Convert from currency "b" to currency "a".
    1.56 -     * 
    1.57 +     * @param from the currency to convert from.
    1.58 +     * @param to the currency to convert to.
    1.59       * @param amount the amount to convert.
    1.60       * @return the converted amount.
    1.61 -     * @throws IllegalArgumentException if amount is null.
    1.62 +     * @throws IllegalArgumentException if any of the arguments are null.
    1.63 +     * @throws InvalidConversionException if either from or to are not valid for the convertor.
    1.64       */
    1.65 -    public BigDecimal convertFrom(final BigDecimal amount)
    1.66 -    {
    1.67 -        final BigDecimal aInUSD;
    1.68 -        final BigDecimal bInUSD;
    1.69 -        final BigDecimal temp;
    1.70 -        final BigDecimal result;
    1.71 -        
    1.72 -        if(amount == null)
    1.73 -        {
    1.74 -            throw new IllegalArgumentException("amount cannot be null");
    1.75 -        }
    1.76 -        
    1.77 -        aInUSD = CurrencyValues.getValue(currencyA);
    1.78 -        bInUSD = CurrencyValues.getValue(currencyB);
    1.79 -        temp   = amount.divide(bInUSD, MathContext.DECIMAL32);
    1.80 -        result = temp.multiply(aInUSD);
    1.81 -        
    1.82 -        return (result.setScale(2, RoundingMode.HALF_DOWN));
    1.83 -    }
    1.84 -    
    1.85 -    /**
    1.86 -     * Convert from currency "a" to currency "b".
    1.87 -     * 
    1.88 -     * @param amount the amount to convert.
    1.89 -     * @return the converted amount.
    1.90 -     * @throws IllegalArgumentException if amount is null.
    1.91 -     */
    1.92 -    public BigDecimal convertTo(final BigDecimal amount)
    1.93 -    {
    1.94 -        final BigDecimal aInUSD;
    1.95 -        final BigDecimal bInUSD;
    1.96 -        final BigDecimal temp;
    1.97 -        final BigDecimal result;
    1.98 -        
    1.99 -        if(amount == null)
   1.100 -        {
   1.101 -            throw new IllegalArgumentException("amount cannot be null");
   1.102 -        }
   1.103 -        
   1.104 -        aInUSD = CurrencyValues.getValue(currencyA);
   1.105 -        bInUSD = CurrencyValues.getValue(currencyB);
   1.106 -        temp      = amount.divide(aInUSD, MathContext.DECIMAL32);
   1.107 -        result = temp.multiply(bInUSD);
   1.108 -        
   1.109 -        return (result.setScale(2, RoundingMode.HALF_DOWN));
   1.110 -    }
   1.111 +    BigDecimal convert(Currency   from, 
   1.112 +                       Currency   to, 
   1.113 +                       BigDecimal amount)
   1.114 +        throws InvalidConversionException;
   1.115  }