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