1 package org.apidesign.apifest08.currency;
3 import java.util.Currency;
4 import java.util.Hashtable;
7 /** This is the skeleton class for your API. You need to make it public, so
8 * it is accessible to your client code (currently in Task1Test.java) file.
10 * Feel free to create additional classes or rename this one, just keep all
11 * the API and its implementation in this package. Do not spread it outside
14 public class Convertor {
16 private static final Map<String, Float> EXCHANGE_RATES = new Hashtable<String, Float>() { {
19 put("SKKCZK", 100/80F);
20 put("CZKSKK", 80/100F);
24 private Currency currencyFirst;
25 private Currency currencySecond;
27 private Convertor(Currency currencyFirst, Currency currencySecond) {
28 this.currencyFirst = currencyFirst;
29 this.currencySecond = currencySecond;
32 public static Convertor getInstanceFor(Currency currencyFirst, Currency currencySecond) {
33 return new Convertor(currencyFirst, currencySecond);
36 public float convert(float value, Currency toCurrency) {
37 if (!toCurrency.equals(currencyFirst) && !toCurrency.equals(currencySecond)) {
38 throw new IllegalArgumentException("Unsupported currency for this convertor!: " + toCurrency.getCurrencyCode());
42 if (toCurrency.equals(currencyFirst)) {
43 rate = EXCHANGE_RATES.get(currencyFirst.getCurrencyCode() + currencySecond.getCurrencyCode());
45 if (toCurrency.equals(currencySecond)) {
46 rate = EXCHANGE_RATES.get(currencySecond.getCurrencyCode() + currencyFirst.getCurrencyCode());
50 throw new IllegalStateException("Undefinied conversion!");