task2/solution12/src/org/apidesign/apifest08/currency/Convertor.java
changeset 33 80f2d8751f35
parent 29 f6073056b9fe
     1.1 --- a/task2/solution12/src/org/apidesign/apifest08/currency/Convertor.java	Wed Oct 01 10:43:05 2008 +0200
     1.2 +++ b/task2/solution12/src/org/apidesign/apifest08/currency/Convertor.java	Tue Oct 07 00:17:07 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.Currency;
     1.8  import java.util.Hashtable;
     1.9 +import java.util.List;
    1.10  
    1.11  import org.apidesign.apifest08.currency.exceptions.ConvertorException;
    1.12  import org.apidesign.apifest08.currency.exceptions.InvalidCurrencyException;
    1.13 @@ -18,20 +20,19 @@
    1.14  
    1.15    private static Hashtable<String, ExchangeRate> exchangeRates;
    1.16  
    1.17 -  private ExchangeRate exchangeRate12;
    1.18 -  private ExchangeRate exchangeRate21;
    1.19 +  private Hashtable<String, ExchangeRate> convertorInstanceExchangeRates; 
    1.20  
    1.21 -  private Convertor(Currency currency1, Currency currency2) throws UnknownConvertorException {
    1.22 -    String key12 = currency1.getCurrencyCode() + currency2.getCurrencyCode();
    1.23 -    String key21 = currency2.getCurrencyCode() + currency1.getCurrencyCode();
    1.24 -
    1.25 -    if (!(exchangeRates.containsKey(key12) && exchangeRates.containsKey(key21))) {
    1.26 -      throw new UnknownConvertorException("Selected convertor (" + currency1.getCurrencyCode() + "->"
    1.27 -          + currency2.getCurrencyCode() + ") has not defined any rates!!!");
    1.28 -    }
    1.29 -
    1.30 -    this.exchangeRate12 = exchangeRates.get(key12);
    1.31 -    this.exchangeRate21 = exchangeRates.get(key21);
    1.32 +  private Convertor(List<Currency> currencies) throws UnknownConvertorException {
    1.33 +	  convertorInstanceExchangeRates = new Hashtable<String, ExchangeRate>();
    1.34 +	  for (Currency currency1 : currencies) {
    1.35 +		for (Currency currency2 : currencies) {
    1.36 +			if(!currency1.getCurrencyCode().equals(currency2.getCurrencyCode())) {
    1.37 +				String key = currency1.getCurrencyCode() + currency2.getCurrencyCode();
    1.38 +				ExchangeRate exchangeRate = exchangeRates.get(key);
    1.39 +				convertorInstanceExchangeRates.put(key, exchangeRate);
    1.40 +			}
    1.41 +		}
    1.42 +	}
    1.43    }
    1.44  
    1.45    /**
    1.46 @@ -67,6 +68,18 @@
    1.47      exchangeRates.put(key21, new ExchangeRate(currency2, currency1, recountedRate, unit));
    1.48  
    1.49    }
    1.50 +  
    1.51 +  /**
    1.52 +   * Merge exchange rates of actual convertor with exchange rates from selected 
    1.53 +   * convertor. If there are same currencies in both convertors, these from selected
    1.54 +   * convertor rewrites these in actual convertor.
    1.55 +   * @param convertor convertor to merge with actual one
    1.56 +   * @return convertor with merged exchange rates 
    1.57 +   */
    1.58 +  public Convertor merge(Convertor convertor) {
    1.59 +	  convertorInstanceExchangeRates.putAll(convertor.getInstanceExchangeRates());
    1.60 +	  return this;
    1.61 +  }
    1.62  
    1.63    /**
    1.64     * Creates new instance of convertor.
    1.65 @@ -78,11 +91,33 @@
    1.66     * @throws UnknownConvertorException
    1.67     *           thrown if convertor for selected currencies has not been defined
    1.68     */
    1.69 -  public static Convertor getConvertorInstance(Currency currency1, Currency currency2) throws UnknownConvertorException {
    1.70 -    if (currency1 == null || currency2 == null) {
    1.71 -      throw new ConvertorException("None of the currencies should be null!!!");
    1.72 -    }
    1.73 -    return new Convertor(currency1, currency2);
    1.74 +  public static Convertor getConvertorInstance(Currency... currencies) throws UnknownConvertorException {
    1.75 +   	List<Currency> currencyList = new ArrayList<Currency>();
    1.76 +   	
    1.77 +   	if(currencies.length < 2) {
    1.78 +   		throw new ConvertorException("To get convertor instance, you have to select at least two currencies!!!");
    1.79 +   	}
    1.80 +
    1.81 +   	for (Currency currency1 : currencies) {
    1.82 +   		for (Currency currency2 : currencies) {
    1.83 +   	   		if(currency1 == null || currency2 == null) {
    1.84 +   	   			throw new ConvertorException("None of the currencies should be null!!!");
    1.85 +   	   		}
    1.86 +   	   		
    1.87 +   			if(!currency1.getCurrencyCode().equals(currency2.getCurrencyCode())) {
    1.88 +   				String key12 = currency1.getCurrencyCode() + currency2.getCurrencyCode();
    1.89 +   				String key21 = currency2.getCurrencyCode() + currency1.getCurrencyCode();
    1.90 +   				if (!(exchangeRates.containsKey(key12) && exchangeRates.containsKey(key21))) {
    1.91 +   					throw new UnknownConvertorException("Selected convertor (" + currency1.getCurrencyCode() + "<->"
    1.92 +   		    			+ currency2.getCurrencyCode() + ") has not defined exchange rates!!!");
    1.93 +   				}
    1.94 +   			}
    1.95 +		}
    1.96 +   		
    1.97 +   		currencyList.add(currency1);
    1.98 +	}
    1.99 +    
   1.100 +    return new Convertor(currencyList);
   1.101    }
   1.102  
   1.103    /**
   1.104 @@ -125,25 +160,25 @@
   1.105     */
   1.106    private ExchangeRate getExchangeRate(Currency originalCurrency, Currency newCurrency) throws InvalidCurrencyException {
   1.107      ExchangeRate actualyUsedExchangeRate = null;
   1.108 +    
   1.109 +    String key = originalCurrency.getCurrencyCode() + newCurrency.getCurrencyCode();
   1.110  
   1.111 -    if (originalCurrency.getCurrencyCode().equals(exchangeRate12.getOriginalCurrency().getCurrencyCode())
   1.112 -        && newCurrency.getCurrencyCode().equals(exchangeRate12.getNewCurrency().getCurrencyCode())) {
   1.113 -      actualyUsedExchangeRate = exchangeRate12;
   1.114 -    } else if (originalCurrency.getCurrencyCode().equals(exchangeRate21.getOriginalCurrency().getCurrencyCode())
   1.115 -        && newCurrency.getCurrencyCode().equals(exchangeRate21.getNewCurrency().getCurrencyCode())) {
   1.116 -      actualyUsedExchangeRate = exchangeRate21;
   1.117 +    if(convertorInstanceExchangeRates.containsKey(key)) {
   1.118 +      actualyUsedExchangeRate = convertorInstanceExchangeRates.get(key);
   1.119      } else {
   1.120 -      throw new InvalidCurrencyException("This convertor " + this
   1.121 -          + " could not be used for converting selected currencies (" + originalCurrency.getCurrencyCode() + "->"
   1.122 +      throw new InvalidCurrencyException("This convertor could not be used for converting selected currencies (" + originalCurrency.getCurrencyCode() + "->"
   1.123            + newCurrency.getCurrencyCode() + ") !!!");
   1.124      }
   1.125  
   1.126      return actualyUsedExchangeRate;
   1.127    }
   1.128 -
   1.129 -  public String toString() {
   1.130 -    String currency1Code = exchangeRate12.getOriginalCurrency().getCurrencyCode();
   1.131 -    String currency2Code = exchangeRate12.getNewCurrency().getCurrencyCode();
   1.132 -    return "Converter [" + currency1Code + "->" + currency2Code + ", " + currency2Code + "->" + currency1Code + "]";
   1.133 +  
   1.134 +  /**
   1.135 +   * Returns exchange rates for actual instance of convertor.
   1.136 +   * @return exchange rates for actual instance of convertor
   1.137 +   */
   1.138 +  Hashtable<String, ExchangeRate> getInstanceExchangeRates() {
   1.139 +	  return convertorInstanceExchangeRates;
   1.140    }
   1.141 +  
   1.142  }