task2/solution14/src/org/apidesign/apifest08/currency/Convertor.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 01 Oct 2008 11:23:11 +0200
changeset 32 2198184978d5
parent 25 a022dd2a5d30
child 49 de033c457bed
permissions -rw-r--r--
Task2
     1 package org.apidesign.apifest08.currency;
     2 
     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.
     5  * <p>
     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
     8  * to other packages.
     9  */
    10 public final class Convertor {
    11     private String currency1;
    12     private String currency2;
    13     private Rate rate;
    14     
    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.");
    18         }
    19         this.currency1 = currency1;
    20         this.currency2 = currency2;
    21         this.rate = rate;
    22     }
    23 
    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.");            
    27         }
    28         
    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);
    33         } else {
    34             throw new IllegalArgumentException("Convertor " + this.toString() + 
    35                     " cannot work with currencies " + fromCurrency + " and " + toCurrency + ".");
    36         }
    37     }
    38 
    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.");            
    42         }
    43         
    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);
    48         } else {
    49             throw new IllegalArgumentException("Convertor " + this.toString() + 
    50                     " cannot work with currencies " + fromCurrency + " and " + toCurrency + ".");
    51         }
    52     }
    53         
    54     @Override
    55     public String toString() {
    56         return currency1+currency2;
    57     }
    58     
    59     @Override
    60     public boolean equals(Object obj) {
    61         if (obj == null) {
    62             return false;
    63         }
    64         if (getClass() != obj.getClass()) {
    65             return false;
    66         }
    67         final Convertor other = (Convertor) obj;
    68         if (this.currency1 != other.currency1 && (this.currency1 == null || !this.currency1.equals(other.currency1))) {
    69             return false;
    70         }
    71         if (this.currency2 != other.currency2 && (this.currency2 == null || !this.currency2.equals(other.currency2))) {
    72             return false;
    73         }
    74         if (this.rate != other.rate && (this.rate == null || !this.rate.equals(other.rate))) {
    75             return false;
    76         }
    77         return true;
    78     }
    79 
    80     @Override
    81     public int hashCode() {
    82         int hash = 5;
    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);
    86         return hash;
    87     }
    88 }