public List <Payment> GetPayments(LoanCalculationParameters loanCalculationParameters)
        {
            var ret = new List <Payment>();

            var    monthlyPayment     = GetMonthlyPayment(loanCalculationParameters);
            double montlyInterestRate = loanCalculationParameters.AnnualInterestRate / MagicNumbers.MonthsPerYear; //J

            var currentPrincipal = loanCalculationParameters.Principal;

            while (currentPrincipal > 0)
            {
                var currentMonthlyInterest   = currentPrincipal * montlyInterestRate;       //H
                var principalForCurrentMonth = monthlyPayment - currentMonthlyInterest;     //C
                var endingBalance            = currentPrincipal - principalForCurrentMonth; //Q

                ret.Add(new Payment
                {
                    Principal = Round(principalForCurrentMonth),
                    Interest  = Round(currentMonthlyInterest),
                    Balance   = Round(endingBalance)
                });

                currentPrincipal = endingBalance;
            }

            return(ret);
        }
Beispiel #2
0
        public void CanGenerateSchedule_MonthlyPaymentsShouldBeCorrect()
        {
            var sut = new FixedMonthlyRateCalculationStrategy();

            var parameters = new LoanCalculationParameters
            {
                Principal          = 100000,
                AnnualInterestRate = 0.06D,
                PaybackTimeInYears = 10
            };

            var ret = sut.GetPayments(parameters);

            ExpectMonthlyPayment(new Payment
            {
                Interest  = 500,
                Principal = 610.21D,
                Balance   = 99389.79D
            }, ret[0]);

            ExpectMonthlyPayment(new Payment
            {
                Interest  = 471.99D,
                Principal = 638.22D,
                Balance   = 93758.81D
            }, ret[9]);

            ExpectMonthlyPayment(new Payment
            {
                Interest  = 5.52D,
                Principal = 1104.68D,
                Balance   = 0
            }, ret[119]);
        }
Beispiel #3
0
        private void CanGetMonthlyPaymentTest(LoanCalculationParameters parameters, double expected)
        {
            var sut = new FixedMonthlyRateCalculationStrategy();

            var ret = sut.GetMonthlyPayment(parameters);

            Assert.AreEqual(expected, Math.Round(ret, MagicNumbers.DecimalPonitPrecision));
        }
        public double GetMonthlyPayment(LoanCalculationParameters loanCalculationParameters)
        {
            double montlyInterestRate = loanCalculationParameters.AnnualInterestRate / MagicNumbers.MonthsPerYear; //J

            int durationInMonths = loanCalculationParameters.PaybackTimeInYears * MagicNumbers.MonthsPerYear;      //N

            double j = montlyInterestRate;
            double n = durationInMonths;
            double p = loanCalculationParameters.Principal;

            return(p * (j / (1 - Math.Pow(1 + j, -n))));
        }
Beispiel #5
0
        public void CanGenerateSchedule_ShouldGenerateProperNumberOfPayments()
        {
            var sut = new FixedMonthlyRateCalculationStrategy();

            var parameters = new LoanCalculationParameters
            {
                Principal          = 100000,
                AnnualInterestRate = 0.06D,
                PaybackTimeInYears = 10
            };

            var ret = sut.GetPayments(parameters);

            Assert.AreEqual(120, ret.Count);
        }