1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/task2/solution14/src/org/apidesign/apifest08/currency/Convertor.java Wed Oct 01 10:43:05 2008 +0200
1.3 @@ -0,0 +1,88 @@
1.4 +package org.apidesign.apifest08.currency;
1.5 +
1.6 +/** This is the skeleton class for your API. You need to make it public, so
1.7 + * it is accessible to your client code (currently in Task1Test.java) file.
1.8 + * <p>
1.9 + * Feel free to create additional classes or rename this one, just keep all
1.10 + * the API and its implementation in this package. Do not spread it outside
1.11 + * to other packages.
1.12 + */
1.13 +public final class Convertor {
1.14 + private String currency1;
1.15 + private String currency2;
1.16 + private Rate rate;
1.17 +
1.18 + Convertor(String currency1, String currency2, Rate rate) {
1.19 + if ((currency1 == null)||(currency2 == null)||(rate == null)) {
1.20 + throw new IllegalArgumentException("All arguments have to be non-null.");
1.21 + }
1.22 + this.currency1 = currency1;
1.23 + this.currency2 = currency2;
1.24 + this.rate = rate;
1.25 + }
1.26 +
1.27 + public double convert(String fromCurrency, String toCurrency, int amount) {
1.28 + if ((fromCurrency == null)||(toCurrency == null)) {
1.29 + throw new IllegalArgumentException("All arguments have to be non-null.");
1.30 + }
1.31 +
1.32 + if (currency1.equals(fromCurrency) && currency2.equals(toCurrency)) {
1.33 + return rate.convertAtoB(amount);
1.34 + } else if (currency2.equals(fromCurrency) && currency1.equals(toCurrency)) {
1.35 + return rate.convertBtoA(amount);
1.36 + } else {
1.37 + throw new IllegalArgumentException("Convertor " + this.toString() +
1.38 + " cannot work with currencies " + fromCurrency + " and " + toCurrency + ".");
1.39 + }
1.40 + }
1.41 +
1.42 + public double convert(String fromCurrency, String toCurrency, double amount) {
1.43 + if ((fromCurrency == null)||(toCurrency == null)) {
1.44 + throw new IllegalArgumentException("All arguments have to be non-null.");
1.45 + }
1.46 +
1.47 + if (currency1.equals(fromCurrency) && currency2.equals(toCurrency)) {
1.48 + return rate.convertAtoB(amount);
1.49 + } else if (currency2.equals(fromCurrency) && currency1.equals(toCurrency)) {
1.50 + return rate.convertBtoA(amount);
1.51 + } else {
1.52 + throw new IllegalArgumentException("Convertor " + this.toString() +
1.53 + " cannot work with currencies " + fromCurrency + " and " + toCurrency + ".");
1.54 + }
1.55 + }
1.56 +
1.57 + @Override
1.58 + public String toString() {
1.59 + return currency1+currency2;
1.60 + }
1.61 +
1.62 + @Override
1.63 + public boolean equals(Object obj) {
1.64 + if (obj == null) {
1.65 + return false;
1.66 + }
1.67 + if (getClass() != obj.getClass()) {
1.68 + return false;
1.69 + }
1.70 + final Convertor other = (Convertor) obj;
1.71 + if (this.currency1 != other.currency1 && (this.currency1 == null || !this.currency1.equals(other.currency1))) {
1.72 + return false;
1.73 + }
1.74 + if (this.currency2 != other.currency2 && (this.currency2 == null || !this.currency2.equals(other.currency2))) {
1.75 + return false;
1.76 + }
1.77 + if (this.rate != other.rate && (this.rate == null || !this.rate.equals(other.rate))) {
1.78 + return false;
1.79 + }
1.80 + return true;
1.81 + }
1.82 +
1.83 + @Override
1.84 + public int hashCode() {
1.85 + int hash = 5;
1.86 + hash = 67 * hash + (this.currency1 != null ? this.currency1.hashCode() : 0);
1.87 + hash = 67 * hash + (this.currency2 != null ? this.currency2.hashCode() : 0);
1.88 + hash = 67 * hash + (this.rate != null ? this.rate.hashCode() : 0);
1.89 + return hash;
1.90 + }
1.91 +}