task2/solution03/src/org/apidesign/apifest08/currency/Convertor.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 01 Oct 2008 10:43:05 +0200
changeset 29 f6073056b9fe
parent 6 task1/solution03/src/org/apidesign/apifest08/currency/Convertor.java@97662396c0fd
permissions -rw-r--r--
Getting ready for task2: copying all solutions to new locations
     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 class Convertor {
    11 
    12     public static final int FIRST_TO_SECOND = 1;
    13 
    14     public static final int SECOND_TO_FIRST = 2;
    15 
    16     private double first;
    17 
    18     private double second;
    19 
    20     public Convertor(double first, double second) {
    21         this.first = first;
    22         this.second = second;
    23     }
    24 
    25     public double convertFirstToSecond(double value) {
    26         return (second / first) * value;
    27     }
    28 
    29     public double convertSecondToFirst(double value) {
    30         return (first / second) * value;
    31     }
    32 
    33     public double convert(double value, int typeOfConvert) {
    34         if (FIRST_TO_SECOND == typeOfConvert) {
    35             return convertFirstToSecond(value);
    36         } else if (SECOND_TO_FIRST == typeOfConvert) {
    37             return convertSecondToFirst(value);
    38         }
    39         throw new IllegalArgumentException("Unkown type of convert.");
    40     }
    41 }