コード例 #1
0
        public void CalculateTest()
        {
            var rateComparasionCalculator = new RateComparasionCalculator();
            var left = new Models.Currency("EUR", "321");
            var right = new Models.Currency("RUB", "123");
            var one = new CurrencyRate(left, right, 1000);
            var two = new CurrencyRate(left, right, 1100);
            var res = rateComparasionCalculator.Calculate(one, two);

            Assert.IsTrue(res.Currency.Equals(right));
            Assert.AreEqual(100m, res.Difference);
            Assert.AreEqual(10m, res.Percent);
            Assert.AreEqual(RateComparasionType.Increased, res.RateComparasionType);

            one = new CurrencyRate(left, right, 1000);
            two = new CurrencyRate(left, right, 700);
            res = rateComparasionCalculator.Calculate(one, two);

            Assert.IsTrue(res.Currency.Equals(right));
            Assert.AreEqual(300m, res.Difference);
            Assert.AreEqual(30m, res.Percent);
            Assert.AreEqual(RateComparasionType.Decreased, res.RateComparasionType);

            one = new CurrencyRate(left, right, 1000);
            two = new CurrencyRate(left, right, 1000);
            res = rateComparasionCalculator.Calculate(one, two);

            Assert.IsTrue(res.Currency.Equals(right));
            Assert.AreEqual(0m, res.Difference);
            Assert.AreEqual(0m, res.Percent);
            Assert.AreEqual(RateComparasionType.NotChanged, res.RateComparasionType);
        }
コード例 #2
0
 public RateComparison Calculate(CurrencyRate one, CurrencyRate two)
 {
     if (!one.Left.Equals(two.Left))
     {
         throw new FormatException("Left currency must be equals");
     }
     if (!one.Right.Equals(two.Right))
     {
         throw new FormatException("Right currency must be equals");
     }
     var type = one.Rate < two.Rate
         ? RateComparasionType.Increased
         : one.Rate > two.Rate
             ? RateComparasionType.Decreased
             : RateComparasionType.NotChanged;
     var onePercent = (one.Rate / 100);
     var percent = 100m - (two.Rate/onePercent);
     return new RateComparison(one.Right, Math.Abs(percent), Math.Abs(one.Rate - two.Rate), type);
 }
コード例 #3
0
 public bool TryLoad(CurrencyRateLoadModel currencyRateLoadModel, out CurrencyRate rate)
 {
     return _cache.TryGetValue(currencyRateLoadModel, out rate);
 }
コード例 #4
0
 public bool TryLoad(string leftCharCode, string rightCharCode, DateTime day, out CurrencyRate rate)
 {
     return TryLoad(new CurrencyRateLoadModel(leftCharCode, rightCharCode, day), out rate);
 }
コード例 #5
0
 public void Save(CurrencyRate rate, DateTime day)
 {
     var currencyRateLoadModel = new CurrencyRateLoadModel(rate.Left.CharCode, rate.Right.CharCode, day);
     _cache.Add(currencyRateLoadModel, rate);
 }