author | Jaroslav Tulach <jaroslav.tulach@apidesign.org> |
Wed, 01 Oct 2008 11:23:11 +0200 | |
changeset 32 | 2198184978d5 |
parent 25 | a022dd2a5d30 |
permissions | -rw-r--r-- |
japod@25 | 1 |
|
japod@25 | 2 |
package org.apidesign.apifest08.currency; |
japod@25 | 3 |
|
japod@25 | 4 |
public final class Rate { |
japod@25 | 5 |
|
japod@25 | 6 |
private double rate; |
japod@25 | 7 |
|
japod@25 | 8 |
public Rate(int amountA, int amountB) { |
japod@25 | 9 |
rate = amountA / (double)amountB; |
japod@25 | 10 |
if (rate <= 0) { |
japod@25 | 11 |
throw new IllegalArgumentException("Exchange rate must be positive."); |
japod@25 | 12 |
} |
japod@25 | 13 |
} |
japod@25 | 14 |
|
japod@25 | 15 |
public Rate(double amountA, double amountB) { |
japod@25 | 16 |
rate = amountA / amountB; |
japod@25 | 17 |
if (rate <= 0) { |
japod@25 | 18 |
throw new IllegalArgumentException("Exchange rate must be positive."); |
japod@25 | 19 |
} |
japod@25 | 20 |
} |
japod@25 | 21 |
|
japod@25 | 22 |
public Rate(double rate) { |
japod@25 | 23 |
this.rate = rate; |
japod@25 | 24 |
if (this.rate <= 0) { |
japod@25 | 25 |
throw new IllegalArgumentException("Exchange rate must be positive."); |
japod@25 | 26 |
} |
japod@25 | 27 |
} |
japod@25 | 28 |
|
japod@25 | 29 |
public double convertAtoB(int a) { |
japod@25 | 30 |
return a / rate; |
japod@25 | 31 |
} |
japod@25 | 32 |
|
japod@25 | 33 |
public double convertAtoB(double a) { |
japod@25 | 34 |
return a / rate; |
japod@25 | 35 |
} |
japod@25 | 36 |
|
japod@25 | 37 |
public double convertBtoA(int b) { |
japod@25 | 38 |
return b * rate; |
japod@25 | 39 |
} |
japod@25 | 40 |
|
japod@25 | 41 |
public double convertBtoA(double b) { |
japod@25 | 42 |
return b * rate; |
japod@25 | 43 |
} |
japod@25 | 44 |
|
japod@25 | 45 |
|
japod@25 | 46 |
@Override |
japod@25 | 47 |
public boolean equals(Object obj) { |
japod@25 | 48 |
if (obj == null) { |
japod@25 | 49 |
return false; |
japod@25 | 50 |
} |
japod@25 | 51 |
if (getClass() != obj.getClass()) { |
japod@25 | 52 |
return false; |
japod@25 | 53 |
} |
japod@25 | 54 |
final Rate other = (Rate) obj; |
japod@25 | 55 |
return true; |
japod@25 | 56 |
} |
japod@25 | 57 |
|
japod@25 | 58 |
@Override |
japod@25 | 59 |
public int hashCode() { |
japod@25 | 60 |
int hash = 5; |
japod@25 | 61 |
return hash; |
japod@25 | 62 |
} |
japod@25 | 63 |
|
japod@25 | 64 |
@Override |
japod@25 | 65 |
public String toString() { |
japod@25 | 66 |
return ""+rate; |
japod@25 | 67 |
} |
japod@25 | 68 |
} |