Exemple #1
0
        /// <summary>
        /// Convert the amount in the currency to the other currency. This allows to convert the amount even when
        /// the rate is reversed.
        /// </summary>
        /// <param name="amount">The amount to be converted</param>
        /// <param name="currency">The currency of the amount. Note that when the currency given is not one of the
        /// two currencies in the exchane rate an exception will be thrown.</param>
        /// <returns>The converted amount</returns>
        public decimal Convert(decimal amount, Currency currency)
        {
            if (currency == null)
            {
                throw new ArgumentNullException(nameof(currency));
            }

            if (currency.Equals(From))
            {
                return(amount * this);
            }
            else if (currency.Equals(To))
            {
                return(amount / this);
            }
            else
            {
                throw new ArgumentOutOfRangeException(nameof(currency));
            }
        }
Exemple #2
0
        /// <summary>
        /// Create an exchange rate for the given currencies. 1 FROM = &lt;rate&gt; TO
        /// </summary>
        /// <param name="from">the currency that will be used to convert a money amount from</param>
        /// <param name="to">the currency that will be used to convert a money amount to</param>
        /// <param name="rate">The actual rate to convert the currencies. 1 FROM = &lt;rate&gt; TO</param>
        /// <exception cref="ArgumentNullException">The <paramref name="from"/> or <paramref name="to"/> currency is <c>null</c></exception>
        /// <exception cref="ArgumentOutOfRangeException">The <paramref name="rate"/>is non-positive</exception>
        /// <exception cref="ArgumentException">The <paramref name="from"/> is equal to <paramref name="to"/></exception>
        public ExchangeRate(Currency from, Currency to, decimal rate)
        {
            if (from == null)
            {
                throw new ArgumentNullException(nameof(from));
            }
            if (to == null)
            {
                throw new ArgumentNullException(nameof(to));
            }
            if (from.Equals(to))
            {
                throw new ArgumentException(Resources.ExchangeRate_SameCurrency);
            }
            if (rate <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(rate));
            }

            From = from;
            To   = to;
            Rate = rate;
        }