task4/solution04/src/org/apidesign/apifest08/currency/ExchangeRate.java
changeset 61 58ec6da75f6f
parent 55 14e78f48ac2b
child 69 420baec87dc5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task4/solution04/src/org/apidesign/apifest08/currency/ExchangeRate.java	Sat Oct 11 23:38:46 2008 +0200
     1.3 @@ -0,0 +1,125 @@
     1.4 +package org.apidesign.apifest08.currency;
     1.5 +
     1.6 +
     1.7 +import java.math.BigDecimal;
     1.8 +import java.util.Currency;
     1.9 +
    1.10 +
    1.11 +/**
    1.12 + * The exchange rate between two currencies.
    1.13 + *
    1.14 + * @author D'Arcy Smith
    1.15 + * @version 1.0
    1.16 + */
    1.17 +public final class ExchangeRate
    1.18 +{
    1.19 +    /**
    1.20 +     * 
    1.21 +     */
    1.22 +    private final Currency currencyA;
    1.23 +
    1.24 +    /**
    1.25 +     * 
    1.26 +     */
    1.27 +    private final Currency currencyB;
    1.28 +
    1.29 +    /**
    1.30 +     * 
    1.31 +     */
    1.32 +    private final BigDecimal rateAtoB;
    1.33 +
    1.34 +    /**
    1.35 +     * 
    1.36 +     */
    1.37 +    private final BigDecimal rateBtoA;
    1.38 +
    1.39 +    /**
    1.40 +     * Construct an ExchangeRate with the specified values.
    1.41 +     * 
    1.42 +     * @param a the first currency
    1.43 +     * @param b the second currency
    1.44 +     * @param ra the rate to convert a to b
    1.45 +     * @param rb the rate to covertt b to a
    1.46 +     * @throws IllegalArgumentException if any parameter is null.
    1.47 +     */
    1.48 +    public ExchangeRate(final Currency   a,
    1.49 +                        final Currency   b,
    1.50 +                        final BigDecimal ra,
    1.51 +                        final BigDecimal rb)
    1.52 +    {
    1.53 +        if(a == null)
    1.54 +        {
    1.55 +            throw new IllegalArgumentException("a cannot be null");
    1.56 +        }
    1.57 +        
    1.58 +        if(b == null)
    1.59 +        {
    1.60 +            throw new IllegalArgumentException("b cannot be null");
    1.61 +        }
    1.62 +        
    1.63 +        if(ra == null)
    1.64 +        {
    1.65 +            throw new IllegalArgumentException("ra cannot be null");
    1.66 +        }
    1.67 +        
    1.68 +        if(rb == null)
    1.69 +        {
    1.70 +            throw new IllegalArgumentException("rb cannot be null");
    1.71 +        }
    1.72 +        
    1.73 +        if(ra.compareTo(BigDecimal.ZERO) <= 0)
    1.74 +        {
    1.75 +            throw new IllegalArgumentException("ra cannot be <= 0, was: " + ra);
    1.76 +        }
    1.77 +        
    1.78 +        if(rb.compareTo(BigDecimal.ZERO) <= 0)
    1.79 +        {
    1.80 +            throw new IllegalArgumentException("rb cannot be <= 0, was: " + ra);
    1.81 +        }
    1.82 +        
    1.83 +        currencyA = a;
    1.84 +        currencyB = b;
    1.85 +        rateAtoB  = ra;
    1.86 +        rateBtoA  = rb;
    1.87 +    }
    1.88 +
    1.89 +    /**
    1.90 +     * Get the first currency.
    1.91 +     * 
    1.92 +     * @return the first currency.
    1.93 +     */
    1.94 +    public Currency getCurrencyA()
    1.95 +    {
    1.96 +        return currencyA;
    1.97 +    }
    1.98 +
    1.99 +    /**
   1.100 +     * Get the second currency.
   1.101 +     * 
   1.102 +     * @return the second currency.
   1.103 +     */
   1.104 +    public Currency getCurrencyB()
   1.105 +    {
   1.106 +        return currencyB;
   1.107 +    }
   1.108 +
   1.109 +    /**
   1.110 +     * Get the conversion rate from currencyA to currencyB.
   1.111 +     * 
   1.112 +     * @return the conversion rate from currencyA to currencyB.
   1.113 +     */
   1.114 +    public BigDecimal getRateAtoB()
   1.115 +    {
   1.116 +        return rateAtoB;
   1.117 +    }
   1.118 +
   1.119 +    /**
   1.120 +     * Get the conversion rate from currencyB to currencyA.
   1.121 +     * 
   1.122 +     * @return the conversion rate from currencyB to currencyA.
   1.123 +     */
   1.124 +    public BigDecimal getRateBtoA()
   1.125 +    {
   1.126 +        return rateBtoA;
   1.127 +    }
   1.128 +}