task4/solution14/src/org/apidesign/apifest08/currency/Rate.java
changeset 61 58ec6da75f6f
parent 50 03c5c5dc94e7
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/task4/solution14/src/org/apidesign/apifest08/currency/Rate.java	Sat Oct 11 23:38:46 2008 +0200
     1.3 @@ -0,0 +1,68 @@
     1.4 +
     1.5 +package org.apidesign.apifest08.currency;
     1.6 +
     1.7 +public final class Rate {
     1.8 +
     1.9 +    private double rate;
    1.10 +
    1.11 +    public Rate(int amountA, int amountB) {
    1.12 +        rate = amountA / (double)amountB;
    1.13 +        if (rate <= 0) {
    1.14 +            throw new IllegalArgumentException("Exchange rate must be positive.");
    1.15 +        }
    1.16 +    }
    1.17 +
    1.18 +    public Rate(double amountA, double amountB) {
    1.19 +        rate = amountA / amountB;
    1.20 +        if (rate <= 0) {
    1.21 +            throw new IllegalArgumentException("Exchange rate must be positive.");
    1.22 +        }
    1.23 +    }
    1.24 +    
    1.25 +    public Rate(double rate) {
    1.26 +        this.rate = rate;
    1.27 +        if (this.rate <= 0) {
    1.28 +            throw new IllegalArgumentException("Exchange rate must be positive.");
    1.29 +        }
    1.30 +    }
    1.31 +
    1.32 +    public double convertAtoB(int a) {
    1.33 +        return a / rate;
    1.34 +    }
    1.35 +
    1.36 +    public double convertAtoB(double a) {
    1.37 +        return a / rate;
    1.38 +    }
    1.39 +
    1.40 +    public double convertBtoA(int b) {
    1.41 +        return b * rate;
    1.42 +    }
    1.43 +
    1.44 +    public double convertBtoA(double b) {
    1.45 +        return b * rate;
    1.46 +    }
    1.47 +
    1.48 +
    1.49 +    @Override
    1.50 +    public boolean equals(Object obj) {
    1.51 +        if (obj == null) {
    1.52 +            return false;
    1.53 +        }
    1.54 +        if (getClass() != obj.getClass()) {
    1.55 +            return false;
    1.56 +        }
    1.57 +        final Rate other = (Rate) obj;
    1.58 +        return true;
    1.59 +    }
    1.60 +
    1.61 +    @Override
    1.62 +    public int hashCode() {
    1.63 +        int hash = 5;
    1.64 +        return hash;
    1.65 +    }
    1.66 +
    1.67 +    @Override
    1.68 +    public String toString() {
    1.69 +        return ""+rate;
    1.70 +    }
    1.71 +}