Example #1
0
 public async Task <CalculateAmortizationResponse> Calculate(CalculateAmortizationRequest request)
 {
     return(await Task.FromResult(new CalculateAmortizationResponse()
     {
         Loan = request.Loan,
         Period = request.Period,
         Rate = request.Rate,
         Amortization = new List <AmortizationLine>()
     }));
 }
        // ------------------------------------------------

        private void OnCalculate(object sender, RoutedEventArgs e)
        {
            double reader;

            if (!string.IsNullOrEmpty(tbLoanAmount.Text) && double.TryParse(tbLoanAmount.Text, out reader) && reader > 0)
            {
                var svc = new AmortSvc();

                var req = new CalculateAmortizationRequest();

                // ----------------------------------------------------------------------
                // Reader got the Loan Amount value in order to get into this code block.

                req.LoanAmount = reader;

                if (double.TryParse(tbTax.Text, out reader))
                {
                    req.AnnualTax = reader;
                }
                if (double.TryParse(tbAPR.Text, out reader))
                {
                    req.InterestRate = reader;
                }
                if (double.TryParse(tbPeriod.Text, out reader))
                {
                    req.NumberOfPayments = reader;
                }
                if (double.TryParse(tbDownPayment.Text, out reader))
                {
                    req.DownPayment = reader;
                }
                if (double.TryParse(tbAltPmt.Text, out reader))
                {
                    req.AlternatePaymentAmt = reader;
                }
                if (double.TryParse(tbInsurance.Text, out reader))
                {
                    req.AnnualInsurancePmt = reader;
                }

                int intReader;

                if (int.TryParse(tbAltStart.Text, out intReader))
                {
                    req.AlternatePaymentNo = intReader;
                }

                response = svc.CalculateAmortization(req);

                var totalPayments  = 0d;
                var totalInterest  = 0d;
                var totalInsurance = 0d;
                var totalTax       = 0d;

                var totalPrinciple = 0d;

                foreach (var detail in response.PaymentDetails)
                {
                    totalPayments  += detail.Payment;
                    totalInterest  += detail.Interest;
                    totalInsurance += detail.Insurance;
                    totalTax       += detail.Tax;
                    totalPrinciple += detail.Principle;
                }

                dgOut.ItemsSource = response.PaymentDetails;

                tbTotalTax.Text       = totalTax.ToString("c");
                tbTotalPayments.Text  = totalPayments.ToString("c");
                tbTotalInterest.Text  = totalInterest.ToString("c");
                tbTotalInsurance.Text = totalInsurance.ToString("c");

                tbNumberOfPayments.Text = response.PaymentDetails.Count.ToString();
                btnPrint.IsEnabled      = true;
            }
        }
Example #3
0
        // ------------------------------------------------

        public CalculateAmortizationResponse CalculateAmortization(CalculateAmortizationRequest req)
        {
            var retVal = new CalculateAmortizationResponse();

            var rate       = req.InterestRate;
            var loanAmount = req.LoanAmount - req.DownPayment;

            // ---------------------------------------------------------
            // Tax and Insurance are constant, and are simply the Annual
            // amount spread over twelve months.

            var tax       = (req.AnnualTax / 12);
            var insurance = (req.AnnualInsurancePmt / 12);

            var balance = req.LoanAmount;
            var payment = (double)CalculatePayment(new CalculatePaymentRequest(req)).PaymentAmount;

            var paymentNo = 0;

            while (balance != 0 && balance > 0)
            {
                if (req.AlternatePaymentAmt > 0)
                {
                    if (paymentNo >= req.AlternatePaymentNo)
                    {
                        payment = req.AlternatePaymentAmt;
                    }
                }

                // ------------------------------------------------------
                // calculate interest rate based on remaining loan amount

                var interest = (loanAmount * rate) / 12;


                if (loanAmount < payment)
                {
                    payment = loanAmount + interest + tax + insurance;
                }

                // -------------------------------------------------------------------
                // calculate principle by subtracting the interest, tax, and insurance

                var principle = payment - interest - tax - insurance;

                // -----------------------------------------------------------------------
                // The new loan amount is the old loan amount minus the principal payment.

                loanAmount -= principle;

                balance = (int)loanAmount;

                // -----------------------------------------------
                // prevent a negative number once loan is paid off

                if (balance <= 0)
                {
                    loanAmount = 0;
                }

                var detail = new PaymentDetail();

                detail.PaymentNo = ++paymentNo;

                detail.Tax       = Math.Round(tax, 2);
                detail.Payment   = Math.Round(payment, 2);
                detail.Interest  = Math.Round(interest, 2);
                detail.Balance   = Math.Round(loanAmount, 2);
                detail.Principle = Math.Round(principle, 2);
                detail.Insurance = Math.Round(insurance, 2);

                retVal.PaymentDetails.Add(detail);
            }

            return(retVal);
        }