コード例 #1
0
        private void btnCalculateMortgage_Click(object sender, EventArgs e)
        {
            //clear all ValidationText and make them invisible

            lblTotalLoanAmountValidation.Text    = "ValidationText";
            lblTotalLoanAmountValidation.Visible = false;

            lblDownPaymentValidation.Text    = "ValidationText";
            lblDownPaymentValidation.Visible = false;

            lblLoanTermValidation.Text    = "ValidationText";
            lblLoanTermValidation.Visible = false;

            lblInterestRateValidation.Text    = "ValidationText";
            lblInterestRateValidation.Visible = false;

            lblExtraPaymentsStartDateValidation.Text    = "ValidationText";
            lblExtraPaymentsStartDateValidation.Visible = false;


            //Add the Mortgage Value Information
            mortgageInput.TotalLoanAmount = decimal.Parse(txtTotalLoanAmount.Text);
            mortgageInput.DownPayment     = decimal.Parse(txtDownPayment.Text);
            mortgageInput.InterestRate    = decimal.Parse(txtInterestRate.Text);
            mortgageInput.LoanTerm        = Int32.Parse(txtLoanTerm.Text);
            mortgageInput.StartDate       = dtMortgageStartDate.Value;
            mortgageInput.ExtraPayments   = extraPaymentsList.ToList();

            //Add base mortgageInput
            baseMortgageInput.TotalLoanAmount = decimal.Parse(txtTotalLoanAmount.Text);
            baseMortgageInput.DownPayment     = decimal.Parse(txtDownPayment.Text);
            baseMortgageInput.InterestRate    = decimal.Parse(txtInterestRate.Text);
            baseMortgageInput.LoanTerm        = Int32.Parse(txtLoanTerm.Text);
            baseMortgageInput.StartDate       = dtMortgageStartDate.Value;

            //Validate
            var mortgageInputValidator = new MortgageInputExtraPaymentsValidator();
            var result = mortgageInputValidator.Validate(mortgageInput);

            if (result.IsValid)
            {
                //Show Group Results Box
                gbResults.Visible = true;

                //Create Amortization Schedule
                var amortizationSchedule = mortgageInput.CalculatedPeriodMortgageData();

                //Create Base Amortization Schedule
                var baseAmortizationSchedule1 = baseMortgageInput.CalculatedPeriodMortgageData();
                basicAmmortizationSchedule = baseAmortizationSchedule1;

                //Map schedule to dgv in results
                amortizationScheduleBindingList = new BindingList <MonthlyCalculatedValuesExtraPayments>(amortizationSchedule);

                //Amortization Schedule
                dgvAmortizationSchedule.AutoGenerateColumns = false;
                //dgvAmortizationSchedule.AutoSize = true;
                var source = new BindingSource(amortizationScheduleBindingList, null);
                dgvAmortizationSchedule.DataSource = source;

                // Initialize and add column IDNumber
                DataGridViewColumn column0 = new DataGridViewTextBoxColumn();
                column0.DataPropertyName = "IDNumber";
                column0.Frozen           = false;
                column0.Name             = "No";
                dgvAmortizationSchedule.Columns.Add(column0);

                // Initialize and add column DateColumn
                DataGridViewColumn column1 = new DataGridViewTextBoxColumn();
                column1.DataPropertyName = "DateColumn";
                column1.Frozen           = false;
                column1.Name             = "Date";
                dgvAmortizationSchedule.Columns.Add(column1);

                // Initialize and add column RemainingBalance
                DataGridViewColumn column2 = new DataGridViewTextBoxColumn();
                column2.DataPropertyName = "RemainingBalance";
                column2.Frozen           = false;
                column2.Name             = "Balance";
                dgvAmortizationSchedule.Columns.Add(column2);

                // Initialize and add column fixedPayment
                DataGridViewColumn column3 = new DataGridViewTextBoxColumn();
                column3.DataPropertyName = "fixedPayment";
                column3.Frozen           = false;
                column3.Name             = "Monthly Payment";
                dgvAmortizationSchedule.Columns.Add(column3);

                // Initialize and add column PrincipalPaid
                DataGridViewColumn column4 = new DataGridViewTextBoxColumn();
                column4.DataPropertyName = "PrincipalPaid";
                column4.Frozen           = false;
                column4.Name             = "Principal";
                dgvAmortizationSchedule.Columns.Add(column4);

                // Initialize and add column InterestPaid
                DataGridViewColumn column5 = new DataGridViewTextBoxColumn();
                column5.DataPropertyName = "InterestPaid";
                column5.Frozen           = false;
                column5.Name             = "Interest";
                dgvAmortizationSchedule.Columns.Add(column5);

                // Initialize and add column TotalInterestPaid
                DataGridViewColumn column6 = new DataGridViewTextBoxColumn();
                column6.DataPropertyName = "TotalInterestPaid";
                column6.Frozen           = false;
                column6.Name             = "Cum. Interest";
                dgvAmortizationSchedule.Columns.Add(column6);

                // Initialize and add column ExtraPayment
                DataGridViewColumn column7 = new DataGridViewTextBoxColumn();
                column7.DataPropertyName = "ExtraPayment";
                column7.Frozen           = false;
                column7.Name             = "Extra Payments";
                dgvAmortizationSchedule.Columns.Add(column7);

                // Initialize and add column CumulativeExtraPayment
                DataGridViewColumn column8 = new DataGridViewTextBoxColumn();
                column8.DataPropertyName = "CumulativeExtraPayment";
                column8.Frozen           = false;
                column8.Name             = "Cum. Extra Payment";
                dgvAmortizationSchedule.Columns.Add(column8);

                //Format Grid
                dgvAmortizationSchedule.Columns["Balance"].DefaultCellStyle.Format            = "c";
                dgvAmortizationSchedule.Columns["Monthly Payment"].DefaultCellStyle.Format    = "c";
                dgvAmortizationSchedule.Columns["Principal"].DefaultCellStyle.Format          = "c";
                dgvAmortizationSchedule.Columns["Interest"].DefaultCellStyle.Format           = "c";
                dgvAmortizationSchedule.Columns["Cum. Interest"].DefaultCellStyle.Format      = "c";
                dgvAmortizationSchedule.Columns["Extra Payments"].DefaultCellStyle.Format     = "c";
                dgvAmortizationSchedule.Columns["Cum. Extra Payment"].DefaultCellStyle.Format = "c";
                dgvAmortizationSchedule.ScrollBars = ScrollBars.Vertical;

                //Calculate Summary Values
                var summaryMortgageInfo = new MortgageExecutiveSummary();
                summaryMortgageInfo.CalculateMortgageExecutiveSummary(amortizationSchedule, mortgageInput);
                execSummary = summaryMortgageInfo;

                //Map values to correct labels
                lblLoanAmount.Text          = summaryMortgageInfo.ActualLoanAmount.ToString("c");
                lblTotalTerm.Text           = summaryMortgageInfo.ExtraPaymentLoanTerm.ToString();
                lblInterestRate.Text        = $"{summaryMortgageInfo.InterestRate*100}";
                lblStartDate.Text           = summaryMortgageInfo.StartDate.ToShortDateString();
                lblTotalExtraPayments.Text  = summaryMortgageInfo.CumulativeExtraPayment.ToString("c");
                lblEndDate.Text             = summaryMortgageInfo.ExtraPaymentLastDate.ToShortDateString();
                lblTotalInterestPaid.Text   = summaryMortgageInfo.TotalInterestPaid.ToString("c");
                lblInterestReduction.Text   = summaryMortgageInfo.ExtraPaymentInterestReduction.ToString("c");
                lblTotalPaid.Text           = summaryMortgageInfo.TotalActualLoanPayment.ToString("c");
                lblConventionalLoanAmt.Text = summaryMortgageInfo.TotalConvetionalLoanPayment.ToString("c");

                if (amortizationScheduleBindingList.Count > 1)
                {
                    btnCreateExcelFile.Enabled = true;
                    btnCreateTextFile.Enabled  = true;
                }
                else
                {
                    btnCreateExcelFile.Enabled = false;
                    btnCreateTextFile.Enabled  = false;
                }
            }
            else
            {
                //throw an error
                foreach (var failure in result.Errors)
                {
                    switch (failure.PropertyName)
                    {
                    case "TotalLoanAmount":
                        lblTotalLoanAmountValidation.Text    = failure.ErrorMessage;
                        lblTotalLoanAmountValidation.Visible = true;
                        break;

                    case "DownPayment":
                        lblDownPaymentValidation.Text    = failure.ErrorMessage;
                        lblDownPaymentValidation.Visible = true;
                        break;

                    case "LoanTerm":
                        lblLoanTermValidation.Text    = failure.ErrorMessage;
                        lblLoanTermValidation.Visible = true;
                        break;

                    case "InterestRate":
                        lblInterestRateValidation.Text    = failure.ErrorMessage;
                        lblInterestRateValidation.Visible = true;
                        break;

                    case "StartDate":
                        lblExtraPaymentsStartDateValidation.Text    = failure.ErrorMessage;
                        lblExtraPaymentsStartDateValidation.Visible = true;
                        break;
                    }
                }
            }
        }
コード例 #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Mortgage Application");

            Console.ForegroundColor = ConsoleColor.Green;

            var mortgageValues = new MortgageInput()
            {
                LoanDescription = "This is a test loan",
                LoanTerm        = 30,
                InterestRate    = 0.03875M,
                DownPayment     = 11960M,
                TotalLoanAmount = 119600M,
                StartDate       = new DateTime(2021, 5, 1)
            };

            var validateMortgageInput          = new MortgageInputValidator();
            var resultTraditionalMortgageInput = validateMortgageInput.Validate(mortgageValues);

            var amortizationSchedule = mortgageValues.CalculatedPeriodMortgageData();
            //--------------------------------------------------------------------------------------------------------

            List <ExtraPayments> testExtraPayments = new List <ExtraPayments>();

            var extraPayment1 = new ExtraPayments()
            {
                ExtraPaymentAmount = 1000M,
                NumberofPayments   = 150,
                PaymentInterval    = 1,
                StartDate          = new DateTime(2021, 6, 1)
            };

            var validateExtraPayment1 = new ExtraPaymentsValidator();
            var result1 = validateExtraPayment1.Validate(extraPayment1);

            if (result1.IsValid)
            {
                testExtraPayments.Add(extraPayment1);
            }


            var extraPayment2 = new ExtraPayments()
            {
                ExtraPaymentAmount = 500M,
                NumberofPayments   = 20,
                PaymentInterval    = 3,
                StartDate          = new DateTime(2021, 6, 1)
            };

            var validateExtraPayment2 = new ExtraPaymentsValidator();
            var result2 = validateExtraPayment2.Validate(extraPayment2);

            if (result2.IsValid)
            {
                testExtraPayments.Add(extraPayment2);
            }

            var extraPayment3 = new ExtraPayments()
            {
                ExtraPaymentAmount = 10M,
                NumberofPayments   = 10,
                PaymentInterval    = 1,
                StartDate          = new DateTime(2021, 6, 1)
            };


            var validateExtraPayment3 = new ExtraPaymentsValidator();
            var result3 = validateExtraPayment3.Validate(extraPayment3);

            if (result3.IsValid)
            {
                testExtraPayments.Add(extraPayment3);
            }


            var testMortgageValuesExtraPayments = new MortgageInputExtraPayments()
            {
                LoanDescription = "This is a test loan",
                LoanTerm        = 30,
                InterestRate    = 0.03875M,
                DownPayment     = 11960M,
                TotalLoanAmount = 119600M,
                StartDate       = new DateTime(2021, 5, 1),
                ExtraPayments   = testExtraPayments
            };

            var validateMortgageValues = new MortgageInputExtraPaymentsValidator();
            var mortgageResult         = validateMortgageValues.Validate(testMortgageValuesExtraPayments);

            //Create Amortization Schedule

            var testAmortizationSchedule = testMortgageValuesExtraPayments.CalculatedPeriodMortgageData();

            //Calculate Summary Values
            var summaryMortgageInfo = new MortgageExecutiveSummary();

            summaryMortgageInfo.CalculateMortgageExecutiveSummary(testAmortizationSchedule, testMortgageValuesExtraPayments);


            //Print out results
            foreach (var period in testAmortizationSchedule)
            {
                Console.WriteLine($"Period No:{period.IDNumber}");
                Console.WriteLine($"Date:{period.DateColumn}");
                Console.WriteLine($"Remaining Balance:{String.Format("{0:C2}", period.RemainingBalance)}");
                Console.WriteLine($"Principal Paid:{String.Format("{0:C2}", period.PrincipalPaid)}");
                Console.WriteLine($"Interest Paid:{String.Format("{0:C2}", period.InterestPaid)}");
                Console.WriteLine($"Fixed Payment:{String.Format("{0:C2}", period.fixedPayment)}");
                Console.WriteLine($"Total Interest Paid:{String.Format("{0:C2}", period.TotalInterestPaid)}");
                Console.WriteLine($"Extra Payment:{String.Format("{0:C2}", period.ExtraPayment)}");
                Console.WriteLine($"Extra Payment Cumulative:{String.Format("{0:C2}", period.CumulativeExtraPayment)}");
                Console.WriteLine($"------------------------------------------------");
            }

            //Final Results
            Console.WriteLine("");
            Console.WriteLine($"------------------------------------------------");
            Console.WriteLine("FINAL RESULTS");

            Console.WriteLine($"------------------------------------------------");

            //--------------------------------------------------------------------------------

            //Excel Print
            var excelApp = new ExcelMortgageOutput();

            var excelbyteArray = excelApp.CreateExcelTestFile(amortizationSchedule, testAmortizationSchedule, summaryMortgageInfo);

            //Text File Print
            var textPrint = new TextMortgageOutput();

            var textByteArray = textPrint.CreateTextFile(testAmortizationSchedule);
        }