コード例 #1
0
        /// <summary>
        /// Расчитывать выплаты.
        /// X = (S*p) / (1-(1+p)^(1-m))
        /// X - размер аннуитетного платежа;
        /// S - сумма кредита;
        /// p - 1/12 годовой процентной ставки, выраженная в сотых долях;
        /// ^ - означает в степени;
        /// m - срок кредита, в месяцах.
        /// </summary>
        /// <param name="amount">Объем долга.</param>
        /// <param name="interestRate">Процентная ставка.</param>
        /// <param name="loanTerm">Срок кредита(в месяцах).</param>
        /// <returns>Выплаты по кредиту.</returns>
        public IEnumerable<Payment> Calculate(
            Amount amount,
            InterestRate interestRate,
            LoanTerm loanTerm)
        {
            if (amount == null || interestRate == null || loanTerm == null)
            {
                throw new ArgumentException("Bad params for calculation");
            }

            var payments = new List<Payment>();

            var totalBalanceOwed = amount.Value;

            int number = 0;
            while (totalBalanceOwed > 0)
            {
                var loanPayment = totalBalanceOwed / (loanTerm.Value - number);
                var percentPayment = totalBalanceOwed * interestRate.Value / (100 * 12);
                totalBalanceOwed -= loanPayment;

                var payment = new Payment(number, percentPayment, loanPayment, totalBalanceOwed);
                payments.Add(payment);

                number++;
            }

            return payments;
        }
        public void TestForOneMonth()
        {
            var amount = new Amount() { Value = 100 };
            var interestRate = new InterestRate() { Value = 12 };
            var loanTerm = new LoanTerm() { Value = 1 };

            var payments = this.calculator.Calculate(amount, interestRate, loanTerm);
            Assert.AreEqual(1, payments.Count());

            var payment = payments.FirstOrDefault();
            Assert.IsNotNull(payment);
            Assert.AreEqual(0, payment.Number);
            Assert.AreEqual(1, payment.PercentPayment);
            Assert.AreEqual(100, payment.LoanPayment);
            Assert.AreEqual(0, payment.BalanceOwed);
        }
        public void TestForTwoMonths()
        {
            var amount = new Amount() { Value = 100 };
            var interestRate = new InterestRate() { Value = 12 };
            var loanTerm = new LoanTerm() { Value = 2 };

            var payments = this.calculator.Calculate(amount, interestRate, loanTerm);
            Assert.AreEqual(2, payments.Count());

            var firstPayment = payments.FirstOrDefault(item => item.Number == 0);
            Assert.IsNotNull(firstPayment);
            Assert.AreEqual(1, firstPayment.PercentPayment);
            Assert.AreEqual(50, firstPayment.LoanPayment);
            Assert.AreEqual(50, firstPayment.BalanceOwed);

            var secondPayment = payments.FirstOrDefault(item => item.Number == 1);
            Assert.IsNotNull(secondPayment);
            Assert.AreEqual(0.5m, secondPayment.PercentPayment);
            Assert.AreEqual(50, secondPayment.LoanPayment);
            Assert.AreEqual(0, secondPayment.BalanceOwed);
        }