예제 #1
0
        static void Main(string[] args)
        {
            LoanTerms loanTerms = new LoanTerms();

            while (loanTerms.Amount <= 0)
            {
                Console.WriteLine("amount:");
                if (loanTerms.HasAmountFailed)
                {
                    Console.WriteLine("Please type an integer above zero:");
                }
                var amount = Console.ReadLine();
                loanTerms.SetAmount(amount);
            }

            while (loanTerms.Interest <= 0)
            {
                Console.WriteLine("interest:");
                if (loanTerms.HasInterestFailed)
                {
                    Console.WriteLine("Please type a percentage above zero:");
                }
                var interest = Console.ReadLine();
                loanTerms.SetInterest(interest);
            }

            while (loanTerms.Downpayment <= 0)
            {
                Console.WriteLine("downpayment:");
                if (loanTerms.HasDownpaymentFailed)
                {
                    Console.WriteLine("Please type an integer above zero:");
                }
                var downpayment = Console.ReadLine();
                loanTerms.SetDownpayment(downpayment);
            }

            while (loanTerms.Term <= 0)
            {
                Console.WriteLine("term:");
                if (loanTerms.HasTermFailed)
                {
                    Console.WriteLine("Please type an integer above zero:");
                }
                var term = Console.ReadLine();
                loanTerms.SetTerm(term);
            }
            Console.WriteLine("");

            LoanProcessor loanProcessor = new LoanProcessor();

            loanProcessor.Calculate(loanTerms);
            Console.WriteLine(JsonConvert.SerializeObject(loanProcessor.LoanValues));
            Console.ReadKey();
        }
예제 #2
0
        public void Calculate(LoanTerms loanTerms)
        {
            double rate            = (loanTerms.Interest / 12) * .01;
            int    numberOfPeriods = loanTerms.Term * 12;
            int    principal       = loanTerms.Amount - loanTerms.Downpayment;

            double amortizationNumerator   = rate * Math.Pow((1 + rate), numberOfPeriods);
            double amortizationDenominator = Math.Pow((1 + rate), numberOfPeriods) - 1;

            decimal monthlyPayment = Convert.ToDecimal(principal * (amortizationNumerator / amortizationDenominator));
            decimal totalPayment   = (monthlyPayment * numberOfPeriods);
            decimal totalInterest  = totalPayment - principal;

            _loanValues = new Dictionary <string, decimal>()
            {
                { "monthly payment", decimal.Round(monthlyPayment, 2) }, { "total interest", decimal.Round(totalInterest, 2) }, { "total payment", decimal.Round(totalPayment, 2) }
            };
        }