Exemple #1
0
 public void LoadCurrencies(List <CurrencyData> list)
 {
     if (list is null)
     {
         return;
     }
     foreach (var c in list)
     {
         if (Currencies.Contains(c.ID))
         {
             continue;
         }
         Currencies.Add(c.ID);
     }
 }
Exemple #2
0
        //calculate exchanged rate
        public double CalculateExchangedRate(string currencyCross, string currency, double amount)
        {
            #region CalculateExchangedRate sanity
            //throw exception if currencyCross is empty,
            //whitespace, or otherwise not 6 characters
            if (String.IsNullOrWhiteSpace(currencyCross) || currency.Length != 6)
            {
                throw new ArgumentException("Currency cross must be exactly six characters.");
            }

            //throw exception if currencyCross does not match an entry in CurrencyCrosses
            if (!CurrencyCrosses.Contains(currencyCross))
            {
                throw new ArgumentException("Given currency cross does not exist.");
            }

            //throw exception if currency does not match an entry in Currencies
            if (!Currencies.Contains(currency))
            {
                throw new ArgumentException("Given currency does not exist.");
            }

            //throw exception if there is no exchange rate for currencyCross
            if (!ExchangeRates.ContainsKey(currencyCross))
            {
                throw new ArgumentException("Given currency cross does not have a specified exchange rate.");
            }

            //throw exception if amount is not positive
            if (amount <= 0)
            {
                throw new ArgumentException("Amount must be positive.");
            }
            #endregion

            double rate = ExchangeRates[currencyCross];
            if (currencyCross.Substring(0, 3) == currency)  //if they want the first currency of the cross currency (AAA from AAABBB),
            {
                amount *= rate;                             //multiply amount by rate
                return(amount);                             //and return amount
            }
            else                                            //ELSE
            {
                double reverseRate = 1 / rate;              //reverse the rate (if 1 AAA = 6.50 BBB, 1 BBB = 1 / 6.50 = 0.15 AAA),
                amount *= reverseRate;                      //multiply amount by reversed rate,
                return(amount);                             //and return amount
            }
        }