author | japod@localhost |
Tue, 30 Sep 2008 16:11:32 +0200 | |
changeset 28 | 36331f7244bd |
permissions | -rw-r--r-- |
japod@25 | 1 |
package org.apidesign.apifest08.currency; |
japod@25 | 2 |
|
japod@25 | 3 |
/** This is the skeleton class for your API. You need to make it public, so |
japod@25 | 4 |
* it is accessible to your client code (currently in Task1Test.java) file. |
japod@25 | 5 |
* <p> |
japod@25 | 6 |
* Feel free to create additional classes or rename this one, just keep all |
japod@25 | 7 |
* the API and its implementation in this package. Do not spread it outside |
japod@25 | 8 |
* to other packages. |
japod@25 | 9 |
*/ |
japod@25 | 10 |
public final class Convertor { |
japod@25 | 11 |
private String currency1; |
japod@25 | 12 |
private String currency2; |
japod@25 | 13 |
private Rate rate; |
japod@25 | 14 |
|
japod@25 | 15 |
Convertor(String currency1, String currency2, Rate rate) { |
japod@25 | 16 |
if ((currency1 == null)||(currency2 == null)||(rate == null)) { |
japod@25 | 17 |
throw new IllegalArgumentException("All arguments have to be non-null."); |
japod@25 | 18 |
} |
japod@25 | 19 |
this.currency1 = currency1; |
japod@25 | 20 |
this.currency2 = currency2; |
japod@25 | 21 |
this.rate = rate; |
japod@25 | 22 |
} |
japod@25 | 23 |
|
japod@25 | 24 |
public double convert(String fromCurrency, String toCurrency, int amount) { |
japod@25 | 25 |
if ((fromCurrency == null)||(toCurrency == null)) { |
japod@25 | 26 |
throw new IllegalArgumentException("All arguments have to be non-null."); |
japod@25 | 27 |
} |
japod@25 | 28 |
|
japod@25 | 29 |
if (currency1.equals(fromCurrency) && currency2.equals(toCurrency)) { |
japod@25 | 30 |
return rate.convertAtoB(amount); |
japod@25 | 31 |
} else if (currency2.equals(fromCurrency) && currency1.equals(toCurrency)) { |
japod@25 | 32 |
return rate.convertBtoA(amount); |
japod@25 | 33 |
} else { |
japod@25 | 34 |
throw new IllegalArgumentException("Convertor " + this.toString() + |
japod@25 | 35 |
" cannot work with currencies " + fromCurrency + " and " + toCurrency + "."); |
japod@25 | 36 |
} |
japod@25 | 37 |
} |
japod@25 | 38 |
|
japod@25 | 39 |
public double convert(String fromCurrency, String toCurrency, double amount) { |
japod@25 | 40 |
if ((fromCurrency == null)||(toCurrency == null)) { |
japod@25 | 41 |
throw new IllegalArgumentException("All arguments have to be non-null."); |
japod@25 | 42 |
} |
japod@25 | 43 |
|
japod@25 | 44 |
if (currency1.equals(fromCurrency) && currency2.equals(toCurrency)) { |
japod@25 | 45 |
return rate.convertAtoB(amount); |
japod@25 | 46 |
} else if (currency2.equals(fromCurrency) && currency1.equals(toCurrency)) { |
japod@25 | 47 |
return rate.convertBtoA(amount); |
japod@25 | 48 |
} else { |
japod@25 | 49 |
throw new IllegalArgumentException("Convertor " + this.toString() + |
japod@25 | 50 |
" cannot work with currencies " + fromCurrency + " and " + toCurrency + "."); |
japod@25 | 51 |
} |
japod@25 | 52 |
} |
japod@25 | 53 |
|
japod@25 | 54 |
@Override |
japod@25 | 55 |
public String toString() { |
japod@25 | 56 |
return currency1+currency2; |
japod@25 | 57 |
} |
japod@25 | 58 |
|
japod@25 | 59 |
@Override |
japod@25 | 60 |
public boolean equals(Object obj) { |
japod@25 | 61 |
if (obj == null) { |
japod@25 | 62 |
return false; |
japod@25 | 63 |
} |
japod@25 | 64 |
if (getClass() != obj.getClass()) { |
japod@25 | 65 |
return false; |
japod@25 | 66 |
} |
japod@25 | 67 |
final Convertor other = (Convertor) obj; |
japod@25 | 68 |
if (this.currency1 != other.currency1 && (this.currency1 == null || !this.currency1.equals(other.currency1))) { |
japod@25 | 69 |
return false; |
japod@25 | 70 |
} |
japod@25 | 71 |
if (this.currency2 != other.currency2 && (this.currency2 == null || !this.currency2.equals(other.currency2))) { |
japod@25 | 72 |
return false; |
japod@25 | 73 |
} |
japod@25 | 74 |
if (this.rate != other.rate && (this.rate == null || !this.rate.equals(other.rate))) { |
japod@25 | 75 |
return false; |
japod@25 | 76 |
} |
japod@25 | 77 |
return true; |
japod@25 | 78 |
} |
japod@25 | 79 |
|
japod@25 | 80 |
@Override |
japod@25 | 81 |
public int hashCode() { |
japod@25 | 82 |
int hash = 5; |
japod@25 | 83 |
hash = 67 * hash + (this.currency1 != null ? this.currency1.hashCode() : 0); |
japod@25 | 84 |
hash = 67 * hash + (this.currency2 != null ? this.currency2.hashCode() : 0); |
japod@25 | 85 |
hash = 67 * hash + (this.rate != null ? this.rate.hashCode() : 0); |
japod@25 | 86 |
return hash; |
japod@25 | 87 |
} |
japod@25 | 88 |
} |