solution 12, task4
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 17 Oct 2008 17:39:18 +0200
changeset 684de3a4b5445a
parent 67 bf7622ec1713
child 69 420baec87dc5
solution 12, task4
task4/solution12/src/org/apidesign/apifest08/currency/Convertor.java
task4/solution12/src/org/apidesign/apifest08/currency/ExchangeRate.java
task4/solution12/src/org/apidesign/apifest08/currency/ExchangeRateInstance.java
task4/solution12/test/org/apidesign/apifest08/test/Task4Test.java
     1.1 --- a/task4/solution12/src/org/apidesign/apifest08/currency/Convertor.java	Fri Oct 17 17:35:52 2008 +0200
     1.2 +++ b/task4/solution12/src/org/apidesign/apifest08/currency/Convertor.java	Fri Oct 17 17:39:18 2008 +0200
     1.3 @@ -1,7 +1,9 @@
     1.4  package org.apidesign.apifest08.currency;
     1.5  
     1.6  import java.util.ArrayList;
     1.7 +import java.util.Calendar;
     1.8  import java.util.Currency;
     1.9 +import java.util.Date;
    1.10  import java.util.Hashtable;
    1.11  import java.util.List;
    1.12  
    1.13 @@ -18,9 +20,9 @@
    1.14   */
    1.15  public class Convertor {
    1.16  
    1.17 -  private static Hashtable<String, ExchangeRate> exchangeRates;
    1.18 +  private static Hashtable<String, List<ExchangeRate>> exchangeRates;
    1.19  
    1.20 -  private List<String> currencyConvertors; 
    1.21 +  private List<ExchangeRateInstance> exchangeRateInstances;
    1.22  
    1.23    /**
    1.24     * Constructor. Checks if all selected currencies are not null and has defined exchange rates for both
    1.25 @@ -30,7 +32,7 @@
    1.26     * pair of currencies
    1.27     */
    1.28    private Convertor(Currency[] currencies) throws UnknownConvertorException {
    1.29 -    currencyConvertors = new ArrayList<String>();
    1.30 +    exchangeRateInstances = new ArrayList<ExchangeRateInstance>();
    1.31  
    1.32      for (Currency currency1 : currencies) {
    1.33        for (Currency currency2 : currencies) {
    1.34 @@ -43,9 +45,9 @@
    1.35            if (!exchangeRates.containsKey(key)) {
    1.36              throw new UnknownConvertorException("Selected convertor (" + currency1.getCurrencyCode() + "->"
    1.37                  + currency2.getCurrencyCode() + ") has not defined exchange rates!!!");
    1.38 -          } 
    1.39 +          }
    1.40            
    1.41 -          currencyConvertors.add(key);            
    1.42 +          exchangeRateInstances.add(new ExchangeRateInstance(key, null, null));            
    1.43          }
    1.44        }
    1.45      }
    1.46 @@ -73,16 +75,35 @@
    1.47      }
    1.48  
    1.49      if (exchangeRates == null) {
    1.50 -      exchangeRates = new Hashtable<String, ExchangeRate>();
    1.51 +      exchangeRates = new Hashtable<String, List<ExchangeRate>>();
    1.52      }
    1.53  
    1.54      String key12 = currency1.getCurrencyCode() + currency2.getCurrencyCode();
    1.55      String key21 = currency2.getCurrencyCode() + currency1.getCurrencyCode();
    1.56      double recountedRate = (unit / rate) * unit;
    1.57  
    1.58 -    exchangeRates.put(key12, new ExchangeRate(currency1, currency2, rate, unit));
    1.59 -    exchangeRates.put(key21, new ExchangeRate(currency2, currency1, recountedRate, unit));
    1.60 +    exchangeRates.put(key12, addNewExchangeRate(key12, currency1, currency2, rate, unit, null, null));
    1.61 +    exchangeRates.put(key21, addNewExchangeRate(key21, currency2, currency1, recountedRate, unit, null, null));
    1.62 +  }
    1.63 +  
    1.64 +  public static Convertor limitExchangeRatesValidity(Convertor convertor, Date validFrom, Date validTo) {
    1.65 +    if(convertor == null || validFrom == null || validTo == null) {
    1.66 +      throw new ConvertorException("None of parameters of method limitExchangeRatesValidity should be null!!!");
    1.67 +    }
    1.68 +    
    1.69 +    List<ExchangeRateInstance> exchangeRateInstances = convertor.getExchangeRateInstances();
    1.70 +    for (ExchangeRateInstance exchangeRateInstance : exchangeRateInstances) {
    1.71 +      // get actual convertor rates for actual validity
    1.72 +      ExchangeRate actualExchangeRate = getExchangeRate(exchangeRateInstance.getKey(), exchangeRateInstance.getValidFrom(), exchangeRateInstance.getValidTo());
    1.73 +      // set new validity for theese rates
    1.74 +      actualExchangeRate.setValidFrom(validFrom);
    1.75 +      actualExchangeRate.setValidTo(validTo);
    1.76 +      // and for selected currency convertor
    1.77 +      exchangeRateInstance.setValidFrom(validFrom);
    1.78 +      exchangeRateInstance.setValidTo(substractSecond(validTo));
    1.79 +    }
    1.80  
    1.81 +    return convertor;
    1.82    }
    1.83    
    1.84    /**
    1.85 @@ -97,7 +118,7 @@
    1.86        throw new ConvertorException("It's impossible to merge with null convertor!!!");
    1.87      }
    1.88      
    1.89 -    currencyConvertors.addAll(convertor.getCurrencyConvertors());
    1.90 +    exchangeRateInstances.addAll(convertor.getExchangeRateInstances());
    1.91  	  return this;
    1.92    }
    1.93  
    1.94 @@ -152,7 +173,8 @@
    1.95    }
    1.96  
    1.97    /**
    1.98 -   * Decides the direction of conversion and returns instance of actual exchange rate.
    1.99 +   * Decides the direction of conversion and returns actual exchange rate
   1.100 +   * for selected currencies and actual moment.
   1.101     * @param actualCurrency
   1.102     *          actual currency we want to convert
   1.103     * @return actual exchange rate of this convertor for selected currency
   1.104 @@ -162,8 +184,9 @@
   1.105      
   1.106      String key = originalCurrency.getCurrencyCode() + newCurrency.getCurrencyCode();
   1.107  
   1.108 -    if(currencyConvertors.contains(key)) {
   1.109 -      actualyUsedExchangeRate = exchangeRates.get(key);
   1.110 +    ExchangeRateInstance exchangeRateInstance = findExchangeRateInstance(key);
   1.111 +    if(exchangeRateInstance != null) {
   1.112 +      actualyUsedExchangeRate = getExchangeRate(exchangeRateInstance.getKey(), exchangeRateInstance.getValidFrom(), exchangeRateInstance.getValidTo());
   1.113      } else {
   1.114        throw new InvalidCurrencyException("This convertor could not be used for converting selected currencies (" + originalCurrency.getCurrencyCode() + "->"
   1.115            + newCurrency.getCurrencyCode() + ") !!!");
   1.116 @@ -173,11 +196,102 @@
   1.117    }
   1.118    
   1.119    /**
   1.120 +   * Finds instance of exchange rate for actual instance of convertor and actual moment.
   1.121 +   * @param key exchange rate instance key
   1.122 +   * @return exchange rate instance
   1.123 +   */
   1.124 +  private ExchangeRateInstance findExchangeRateInstance(String key) {
   1.125 +    ExchangeRateInstance instance = null;
   1.126 +    
   1.127 +    Date now = new Date();
   1.128 +    for (ExchangeRateInstance item : exchangeRateInstances) {
   1.129 +      if(item.getKey().equals(key) && item.getValidFrom() == null && item.getValidTo() == null) {
   1.130 +        instance = item;
   1.131 +        break;
   1.132 +      } else if(item.getKey().equals(key) && item.getValidFrom().compareTo(now) <= 0 && item.getValidTo().compareTo(now) >= 0) {
   1.133 +        instance = item;
   1.134 +        break;
   1.135 +      }
   1.136 +    }
   1.137 +    
   1.138 +    return instance;
   1.139 +  }
   1.140 +  
   1.141 +  
   1.142 +  /**
   1.143     * Returns currency convertors for actual instance of convertor.
   1.144     * @return currency convertors for actual instance of convertor
   1.145     */
   1.146 -  List<String> getCurrencyConvertors() {
   1.147 -	  return currencyConvertors;
   1.148 +  private List<ExchangeRateInstance> getExchangeRateInstances() {
   1.149 +    return exchangeRateInstances;
   1.150 +  }
   1.151 +  
   1.152 +  private static ExchangeRate getExchangeRate(String key, Date validFrom, Date validTo) {
   1.153 +    ExchangeRate exchangeRate = null;
   1.154 +    
   1.155 +    List<ExchangeRate> currencyExchangeRates = exchangeRates.get(key);
   1.156 +    for (ExchangeRate currencyExchangeRate : currencyExchangeRates) {
   1.157 +      Date actValidFrom = currencyExchangeRate.getValidFrom();
   1.158 +      Date actValidTo = currencyExchangeRate.getValidTo();
   1.159 +      
   1.160 +      if(actValidFrom == null && validFrom == null && actValidTo == null && validTo == null) {
   1.161 +        exchangeRate = currencyExchangeRate;
   1.162 +        break;
   1.163 +      } else if(actValidFrom != null && validFrom != null && actValidTo != null && validTo != null && actValidFrom.compareTo(validFrom) <= 0 && actValidTo.compareTo(validTo) > 0) {
   1.164 +        exchangeRate = currencyExchangeRate;
   1.165 +        break;
   1.166 +      }
   1.167 +    }
   1.168 +    
   1.169 +    if(exchangeRate == null) {
   1.170 +      throw new ConvertorException("Exchange rate for actual exchange rate instance not found!!!");
   1.171 +    }
   1.172 +    
   1.173 +    return exchangeRate;
   1.174 +  }
   1.175 +  
   1.176 +  private static List<ExchangeRate> addNewExchangeRate(String key, Currency currency1, Currency currency2, double rate, double unit, Date validFrom, Date validTo) {
   1.177 +    List<ExchangeRate> actualExchangeRates = new ArrayList<ExchangeRate>();
   1.178 +    ExchangeRate newExchangeRate = null;
   1.179 +    
   1.180 +    if(exchangeRates.containsKey(key)) {
   1.181 +      List<ExchangeRate> rates = exchangeRates.get(key);
   1.182 +      for (ExchangeRate exchangeRate : rates) {
   1.183 +        if(exchangeRate.getValidFrom() == null && exchangeRate.getValidTo() == null && validFrom == null && validTo == null) {
   1.184 +          newExchangeRate = exchangeRate;
   1.185 +          break;
   1.186 +        } else if(exchangeRate.getValidFrom() != null && exchangeRate.getValidTo() != null && validFrom != null && validTo != null) {
   1.187 +          if(exchangeRate.getValidFrom().compareTo(validFrom) == 0 && exchangeRate.getValidTo().compareTo(validTo) == 0) {
   1.188 +            newExchangeRate = exchangeRate;
   1.189 +            break;
   1.190 +          }
   1.191 +        }
   1.192 +      }
   1.193 +      actualExchangeRates.addAll(rates);
   1.194 +    }
   1.195 +    
   1.196 +    if(newExchangeRate == null) {
   1.197 +      actualExchangeRates.add(new ExchangeRate(currency1, currency2, rate, unit, validFrom, validTo));
   1.198 +    } else {
   1.199 +      newExchangeRate.setRate(rate);
   1.200 +      newExchangeRate.setUnit(unit);
   1.201 +      actualExchangeRates.add(newExchangeRate);
   1.202 +    }
   1.203 +    
   1.204 +    return actualExchangeRates;
   1.205 +  }
   1.206 +  
   1.207 +  /**
   1.208 +   * Substracts one second from selected date.
   1.209 +   * @param date date
   1.210 +   * @return date -1s
   1.211 +   */
   1.212 +  private static Date substractSecond(Date date) {
   1.213 +    Calendar calendar = Calendar.getInstance();
   1.214 +    calendar.setTime(date);
   1.215 +    calendar.set(Calendar.SECOND, -1);
   1.216 +    
   1.217 +    return calendar.getTime();
   1.218    }
   1.219    
   1.220  }
     2.1 --- a/task4/solution12/src/org/apidesign/apifest08/currency/ExchangeRate.java	Fri Oct 17 17:35:52 2008 +0200
     2.2 +++ b/task4/solution12/src/org/apidesign/apifest08/currency/ExchangeRate.java	Fri Oct 17 17:39:18 2008 +0200
     2.3 @@ -1,35 +1,101 @@
     2.4 -package org.apidesign.apifest08.currency;
     2.5 -
     2.6 -import java.util.Currency;
     2.7 -
     2.8 -public class ExchangeRate {
     2.9 -
    2.10 -  private Currency originalCurrency;
    2.11 -  private Currency newCurrency;
    2.12 -  private double unit;
    2.13 -  private double rate;
    2.14 -
    2.15 -  public ExchangeRate(Currency originalCurrency, Currency newCurrency, double rate, double unit) {
    2.16 -    this.newCurrency = newCurrency;
    2.17 -    this.originalCurrency = originalCurrency;
    2.18 -    this.rate = rate;
    2.19 -    this.unit = unit;
    2.20 -  }
    2.21 -
    2.22 -  public Currency getOriginalCurrency() {
    2.23 -    return originalCurrency;
    2.24 -  }
    2.25 -
    2.26 -  public Currency getNewCurrency() {
    2.27 -    return newCurrency;
    2.28 -  }
    2.29 -
    2.30 -  public double getUnit() {
    2.31 -    return unit;
    2.32 -  }
    2.33 -
    2.34 -  public double getRate() {
    2.35 -    return rate;
    2.36 -  }
    2.37 -
    2.38 -}
    2.39 +package org.apidesign.apifest08.currency;
    2.40 +
    2.41 +import java.util.Currency;
    2.42 +import java.util.Date;
    2.43 +
    2.44 +public class ExchangeRate {
    2.45 +
    2.46 +  private Currency originalCurrency;
    2.47 +  private Currency newCurrency;
    2.48 +  private double unit;
    2.49 +  private double rate;
    2.50 +  private Date validFrom;
    2.51 +  private Date validTo;
    2.52 +
    2.53 +  public ExchangeRate(Currency originalCurrency, Currency newCurrency, double rate, double unit) {
    2.54 +    this.newCurrency = newCurrency;
    2.55 +    this.originalCurrency = originalCurrency;
    2.56 +    this.rate = rate;
    2.57 +    this.unit = unit;
    2.58 +  }
    2.59 +  
    2.60 +  public ExchangeRate(Currency originalCurrency, Currency newCurrency, double rate, double unit, Date validFrom, Date validTo) {
    2.61 +    this.newCurrency = newCurrency;
    2.62 +    this.originalCurrency = originalCurrency;
    2.63 +    this.rate = rate;
    2.64 +    this.unit = unit;
    2.65 +    this.validFrom = validFrom;
    2.66 +    this.validTo = validTo;
    2.67 +  }
    2.68 +
    2.69 +  /**
    2.70 +   * @return the originalCurrency
    2.71 +   */
    2.72 +  public Currency getOriginalCurrency() {
    2.73 +    return originalCurrency;
    2.74 +  }
    2.75 +
    2.76 +  /**
    2.77 +   * @return the newCurrency
    2.78 +   */
    2.79 +  public Currency getNewCurrency() {
    2.80 +    return newCurrency;
    2.81 +  }
    2.82 +
    2.83 +  /**
    2.84 +   * @return the unit
    2.85 +   */
    2.86 +  public double getUnit() {
    2.87 +    return unit;
    2.88 +  }
    2.89 +
    2.90 +  /**
    2.91 +   * @param unit the unit to set
    2.92 +   */
    2.93 +  public void setUnit(double unit) {
    2.94 +    this.unit = unit;
    2.95 +  }
    2.96 +
    2.97 +  /**
    2.98 +   * @return the rate
    2.99 +   */
   2.100 +  public double getRate() {
   2.101 +    return rate;
   2.102 +  }
   2.103 +
   2.104 +  /**
   2.105 +   * @param rate the rate to set
   2.106 +   */
   2.107 +  public void setRate(double rate) {
   2.108 +    this.rate = rate;
   2.109 +  }
   2.110 +
   2.111 +  /**
   2.112 +   * @return the validFrom
   2.113 +   */
   2.114 +  public Date getValidFrom() {
   2.115 +    return validFrom;
   2.116 +  }
   2.117 +
   2.118 +  /**
   2.119 +   * @param validFrom the validFrom to set
   2.120 +   */
   2.121 +  public void setValidFrom(Date validFrom) {
   2.122 +    this.validFrom = validFrom;
   2.123 +  }
   2.124 +
   2.125 +  /**
   2.126 +   * @return the validTo
   2.127 +   */
   2.128 +  public Date getValidTo() {
   2.129 +    return validTo;
   2.130 +  }
   2.131 +
   2.132 +  /**
   2.133 +   * @param validTo the validTo to set
   2.134 +   */
   2.135 +  public void setValidTo(Date validTo) {
   2.136 +    this.validTo = validTo;
   2.137 +  }
   2.138 +
   2.139 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/task4/solution12/src/org/apidesign/apifest08/currency/ExchangeRateInstance.java	Fri Oct 17 17:39:18 2008 +0200
     3.3 @@ -0,0 +1,53 @@
     3.4 +package org.apidesign.apifest08.currency;
     3.5 +
     3.6 +import java.util.Date;
     3.7 +
     3.8 +public class ExchangeRateInstance {
     3.9 +
    3.10 +  private String key;
    3.11 +  private Date validFrom;
    3.12 +  private Date validTo;
    3.13 +  
    3.14 +  public ExchangeRateInstance(String key, Date validFrom, Date validTo) {
    3.15 +    super();
    3.16 +    this.key = key;
    3.17 +    this.validFrom = validFrom;
    3.18 +    this.validTo = validTo;
    3.19 +  }
    3.20 +
    3.21 +  /**
    3.22 +   * @return the validFrom
    3.23 +   */
    3.24 +  public Date getValidFrom() {
    3.25 +    return validFrom;
    3.26 +  }
    3.27 +
    3.28 +  /**
    3.29 +   * @param validFrom the validFrom to set
    3.30 +   */
    3.31 +  public void setValidFrom(Date validFrom) {
    3.32 +    this.validFrom = validFrom;
    3.33 +  }
    3.34 +
    3.35 +  /**
    3.36 +   * @return the validTo
    3.37 +   */
    3.38 +  public Date getValidTo() {
    3.39 +    return validTo;
    3.40 +  }
    3.41 +
    3.42 +  /**
    3.43 +   * @param validTo the validTo to set
    3.44 +   */
    3.45 +  public void setValidTo(Date validTo) {
    3.46 +    this.validTo = validTo;
    3.47 +  }
    3.48 +
    3.49 +  /**
    3.50 +   * @return the key
    3.51 +   */
    3.52 +  public String getKey() {
    3.53 +    return key;
    3.54 +  }
    3.55 +  
    3.56 +}
     4.1 --- a/task4/solution12/test/org/apidesign/apifest08/test/Task4Test.java	Fri Oct 17 17:35:52 2008 +0200
     4.2 +++ b/task4/solution12/test/org/apidesign/apifest08/test/Task4Test.java	Fri Oct 17 17:39:18 2008 +0200
     4.3 @@ -1,8 +1,11 @@
     4.4  package org.apidesign.apifest08.test;
     4.5  
     4.6 +import java.text.SimpleDateFormat;
     4.7 +import java.util.Currency;
     4.8  import java.util.Date;
     4.9  import junit.framework.TestCase;
    4.10  import org.apidesign.apifest08.currency.Convertor;
    4.11 +import org.apidesign.apifest08.currency.exceptions.UnknownConvertorException;
    4.12  
    4.13  /** The exchange rates are not always the same. They are changing. However
    4.14   * as in order to predict the future, one needs to understand own past. That is
    4.15 @@ -45,57 +48,104 @@
    4.16       * @return new convertor
    4.17       */
    4.18      public static Convertor limitTo(Convertor old, Date from, Date till) {
    4.19 -        return null;
    4.20 +        return Convertor.limitExchangeRatesValidity(old, from, till);
    4.21      }
    4.22  
    4.23  
    4.24      public void testCompositionOfLimitedConvertors() throws Exception {
    4.25 -        if (Boolean.getBoolean("ignore.failing")) {
    4.26 -            // implement me! then delete this if statement
    4.27 -            return;
    4.28 -        }
    4.29 -
    4.30 -        Date d1 = null; // 2008-10-01 0:00 GMT
    4.31 -        Date d2 = null; // 2008-10-02 0:00 GMT
    4.32 -        Date d3 = null; // 2008-10-03 0:00 GMT
    4.33 +        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm zzz");
    4.34 +        Date d1 = format.parse("2008-10-01 00:00 GMT"); // 2008-10-01 0:00 GMT
    4.35 +        Date d2 = format.parse("2008-10-02 00:00 GMT"); // 2008-10-02 0:00 GMT
    4.36 +        Date d3 = format.parse("2008-10-03 00:00 GMT"); // 2008-10-03 0:00 GMT
    4.37          
    4.38          Convertor c = Task2Test.merge(
    4.39              limitTo(Task1Test.createCZKtoUSD(), d1, d2),
    4.40              limitTo(Task1Test.createSKKtoCZK(), d2, d3)
    4.41          );
    4.42 +        
    4.43 +        Date now = new Date();
    4.44  
    4.45          // convert $5 to CZK using c:
    4.46 -        // cannot convert as no rate is applicable to current date
    4.47 +        double result = 0;
    4.48 +        boolean exceptionThrown = false;
    4.49 +        try {
    4.50 +          result = c.convert(5, Currency.getInstance("USD"), Currency.getInstance("CZK"));
    4.51 +        } catch (Exception e) {
    4.52 +          exceptionThrown = true;
    4.53 +        }
    4.54 +        
    4.55 +        if(d1.compareTo(now) <= 0 && d2.compareTo(now) > 0) {
    4.56 +          System.out.println("Result.");
    4.57 +          assertEquals("Result is not 85 CZK.", 85d, result);          
    4.58 +        } else {
    4.59 +          System.out.println("Exception.");
    4.60 +          assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
    4.61 +        }
    4.62  
    4.63          // convert $8 to CZK using c:
    4.64 -        // cannot convert as no rate is applicable to current date
    4.65 +        exceptionThrown = false;
    4.66 +        try {
    4.67 +          result = c.convert(8, Currency.getInstance("USD"), Currency.getInstance("CZK"));
    4.68 +        } catch (Exception e) {
    4.69 +          exceptionThrown = true;
    4.70 +        }
    4.71 +        
    4.72 +        if(d1.compareTo(now) <= 0 && d2.compareTo(now) > 0) {
    4.73 +          System.out.println("Result.");
    4.74 +          assertEquals("Result is not 136 CZK.", 136d, result);          
    4.75 +        } else {
    4.76 +          System.out.println("Exception.");
    4.77 +          assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
    4.78 +        }
    4.79  
    4.80          // convert 1003CZK to USD using c:
    4.81 -        // cannot convert as no rate is applicable to current date
    4.82 +        exceptionThrown = false;
    4.83 +        try {
    4.84 +          result = c.convert(1003, Currency.getInstance("CZK"), Currency.getInstance("USD"));
    4.85 +        } catch (Exception e) {
    4.86 +          exceptionThrown = true;
    4.87 +        }
    4.88 +        
    4.89 +        if(d1.compareTo(now) <= 0 && d2.compareTo(now) > 0) {
    4.90 +          System.out.println("Result.");
    4.91 +          assertEquals("Result is not 59 USD.", 59d, result);
    4.92 +        } else {
    4.93 +          System.out.println("Exception.");
    4.94 +          assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
    4.95 +        }
    4.96  
    4.97          // convert 16CZK using c:
    4.98 -        // cannot convert as no rate is applicable to current date
    4.99 +        exceptionThrown = false;
   4.100 +        try {
   4.101 +          result = c.convert(16, Currency.getInstance("CZK"), Currency.getInstance("SKK"));
   4.102 +        } catch (Exception e) {
   4.103 +          exceptionThrown = true;
   4.104 +        }
   4.105 +        
   4.106 +        if(d2.compareTo(now) <= 0 && d3.compareTo(now) > 0) {
   4.107 +          System.out.println("Result.");
   4.108 +          assertEquals("Result is not 20 SKK.", 20d, result);          
   4.109 +        } else {
   4.110 +          System.out.println("Exception.");
   4.111 +          assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
   4.112 +        }
   4.113  
   4.114          // convert 500SKK to CZK using c:
   4.115 -        // cannot convert as no rate is applicable to current date
   4.116 +        exceptionThrown = false;
   4.117 +        try {
   4.118 +          result = c.convert(500, Currency.getInstance("SKK"), Currency.getInstance("CZK"));
   4.119 +        } catch (Exception e) {
   4.120 +          exceptionThrown = true;
   4.121 +        }
   4.122 +        
   4.123 +        if(d2.compareTo(now) <= 0 && d3.compareTo(now) > 0) {
   4.124 +          System.out.println("Result.");
   4.125 +          assertEquals("Result is not 400 CZK.", 400d, result);          
   4.126 +        } else {
   4.127 +          System.out.println("Exception.");
   4.128 +          assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
   4.129 +        }
   4.130  
   4.131 -        // convert $5 to CZK using c at 2008-10-01 6:00 GMT:
   4.132 -        // assertEquals("Result is 85 CZK");
   4.133 -
   4.134 -        // convert $8 to CZK using c at 2008-10-01 6:00 GMT:
   4.135 -        // assertEquals("Result is 136 CZK");
   4.136 -
   4.137 -        // convert 1003CZK to USD using c at 2008-10-01 6:00 GMT:
   4.138 -        // assertEquals("Result is 59 USD");
   4.139 -
   4.140 -        // convert 16CZK using c at 2008-10-02 9:00 GMT:
   4.141 -        // assertEquals("Result is 20 SKK");
   4.142 -
   4.143 -        // convert 500SKK to CZK using c at 2008-10-02 9:00 GMT:
   4.144 -        // assertEquals("Result is 400 CZK");
   4.145 -
   4.146 -        // convert 500SKK to CZK using c at 2008-10-01 6:00 GMT:
   4.147 -        // cannot convert as no rate is applicable to current date
   4.148      }
   4.149  
   4.150      /** Create convertor that understands two currencies, CZK and
   4.151 @@ -104,18 +154,26 @@
   4.152       * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
   4.153       */
   4.154      public static Convertor createSKKtoCZK2() {
   4.155 -        return null;
   4.156 +      // set exchange rates
   4.157 +      Convertor.setConvertorRates(Currency.getInstance("SKK"), Currency.getInstance("CZK"), 90d, 100d);
   4.158 +
   4.159 +      // create new instance
   4.160 +      Convertor convertor = null;
   4.161 +      try {
   4.162 +        convertor = Convertor.getConvertorInstance(Currency.getInstance("SKK"), Currency.getInstance("CZK"));
   4.163 +      } catch (UnknownConvertorException e) {
   4.164 +        e.printStackTrace();
   4.165 +      }
   4.166 +
   4.167 +      return convertor;
   4.168      }
   4.169  
   4.170      public void testDateConvetorWithTwoDifferentRates() throws Exception {
   4.171 -        if (Boolean.getBoolean("ignore.failing")) {
   4.172 -            // implement me! then delete this if statement
   4.173 -            return;
   4.174 -        }
   4.175  
   4.176 -        Date d1 = null; // 2008-10-01 0:00 GMT
   4.177 -        Date d2 = null; // 2008-10-02 0:00 GMT
   4.178 -        Date d3 = null; // 2008-10-03 0:00 GMT
   4.179 +        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm zzz");
   4.180 +        Date d1 = format.parse("2008-10-01 00:00 GMT"); // 2008-10-01 0:00 GMT
   4.181 +        Date d2 = format.parse("2008-10-02 00:00 GMT"); // 2008-10-02 0:00 GMT
   4.182 +        Date d3 = format.parse("2008-10-03 00:00 GMT"); // 2008-10-03 0:00 GMT
   4.183  
   4.184          Convertor c = Task2Test.merge(
   4.185              limitTo(createSKKtoCZK2(), d1, d2),
   4.186 @@ -127,6 +185,27 @@
   4.187  
   4.188          // convert 500SKK to CZK using c at 2008-10-01 6:00 GMT:
   4.189          // assertEquals("Result is 450 CZK");
   4.190 +        Date now = new Date();
   4.191 +        
   4.192 +        // convert 500SKK to CZK using c:
   4.193 +        double result = 0;
   4.194 +        boolean exceptionThrown = false;
   4.195 +        try {
   4.196 +          result = c.convert(500, Currency.getInstance("SKK"), Currency.getInstance("CZK"));
   4.197 +        } catch (Exception e) {
   4.198 +          exceptionThrown = true;
   4.199 +        }
   4.200 +        
   4.201 +        if(d1.compareTo(now) <= 0 && d2.compareTo(now) > 0) {
   4.202 +          System.out.println("\nResult");
   4.203 +          assertEquals("Result is not 450 CZK.", 450d, result);
   4.204 +        } else if(d2.compareTo(now) <= 0 && d3.compareTo(now) > 0) {
   4.205 +          System.out.println("\nResult");
   4.206 +          assertEquals("Result is not 400 CZK.", 400d, result);
   4.207 +        } else {
   4.208 +          System.out.println("\nException");
   4.209 +          assertEquals("There is no Exception while using convertor at wrong day!", true, exceptionThrown);
   4.210 +        }
   4.211      }
   4.212  
   4.213  }