1 package org.apidesign.apifest08.currency;
3 /** This is the skeleton class for your API. You need to make it public, so
4 * it is accessible to your client code (currently in Task1Test.java) file.
6 * Feel free to create additional classes or rename this one, just keep all
7 * the API and its implementation in this package. Do not spread it outside
10 public final class Convertor {
11 private String currency1;
12 private String currency2;
15 Convertor(String currency1, String currency2, Rate rate) {
16 if ((currency1 == null)||(currency2 == null)||(rate == null)) {
17 throw new IllegalArgumentException("All arguments have to be non-null.");
19 this.currency1 = currency1;
20 this.currency2 = currency2;
24 public double convert(String fromCurrency, String toCurrency, int amount) {
25 if ((fromCurrency == null)||(toCurrency == null)) {
26 throw new IllegalArgumentException("All arguments have to be non-null.");
29 if (currency1.equals(fromCurrency) && currency2.equals(toCurrency)) {
30 return rate.convertAtoB(amount);
31 } else if (currency2.equals(fromCurrency) && currency1.equals(toCurrency)) {
32 return rate.convertBtoA(amount);
34 throw new IllegalArgumentException("Convertor " + this.toString() +
35 " cannot work with currencies " + fromCurrency + " and " + toCurrency + ".");
39 public double convert(String fromCurrency, String toCurrency, double amount) {
40 if ((fromCurrency == null)||(toCurrency == null)) {
41 throw new IllegalArgumentException("All arguments have to be non-null.");
44 if (currency1.equals(fromCurrency) && currency2.equals(toCurrency)) {
45 return rate.convertAtoB(amount);
46 } else if (currency2.equals(fromCurrency) && currency1.equals(toCurrency)) {
47 return rate.convertBtoA(amount);
49 throw new IllegalArgumentException("Convertor " + this.toString() +
50 " cannot work with currencies " + fromCurrency + " and " + toCurrency + ".");
55 public String toString() {
56 return currency1+currency2;
60 public boolean equals(Object obj) {
64 if (getClass() != obj.getClass()) {
67 final Convertor other = (Convertor) obj;
68 if (this.currency1 != other.currency1 && (this.currency1 == null || !this.currency1.equals(other.currency1))) {
71 if (this.currency2 != other.currency2 && (this.currency2 == null || !this.currency2.equals(other.currency2))) {
74 if (this.rate != other.rate && (this.rate == null || !this.rate.equals(other.rate))) {
81 public int hashCode() {
83 hash = 67 * hash + (this.currency1 != null ? this.currency1.hashCode() : 0);
84 hash = 67 * hash + (this.currency2 != null ? this.currency2.hashCode() : 0);
85 hash = 67 * hash + (this.rate != null ? this.rate.hashCode() : 0);