Exemple #1
0
        public string getAmortizationTable()
        {
            StringBuilder table = new StringBuilder();
            bool          valid;

            valid = calculateAmortizationTable();

            if (valid == true)
            {
                try
                {
                    for (int count = 0; count <= amortizationTable.GetUpperBound(0); count++)
                    {
                        table.Append(amortizationTable[count].Period.ToString() + "\t");
                        table.Append(amortizationTable[count].LoanBalance_Formatted + "\t");
                        table.Append(amortizationTable[count].PrincipalBalance_Formatted + "\t");
                        table.Append(amortizationTable[count].InterestBalance_Formatted + "\t");
                        table.Append(amortizationTable[count].PrincipalPaid_Formatted + "\t");
                        table.Append(amortizationTable[count].InterestPaid_Formatted + "\n");
                    }
                }
                catch (Exception ex)
                {
                    InputUtilities.showErrorMessage(ex, "Unable to create Amortization Table");
                    table.Length = 0;
                }
            }
            return(table.ToString());
        }
        private void setInputErrorField(TextBox txtBox)
        {
            string prompt = "Please re-enter the " + txtBox.Tag + " information";

            InputUtilities.showErrorMessage(prompt, "Invalid " + txtBox.Tag);
            txtBox.Focus();
            txtBox.SelectAll();
        }
 private void clearErrorProvider()
 {
     try
     {
         errorProvider1.SetError(txtMortgageAmount, "");
         errorProvider1.SetError(txtInterestRate, "");
         errorProvider1.SetError(txtMortgageTerm, "");
         errorProvider1.SetError(rtfAmortTable, "");
     }
     catch (System.Exception ex)
     {
         InputUtilities.clearException(ex);
     }
 }
        private void findMortgagePayment()
        {
            string prompt;
            bool   validPV   = false;
            bool   validAPR  = false;
            bool   validTerm = false;
            double principal = 0.0;
            double apr       = 0.0;
            int    term      = 0;

            try
            {
                //***********************************************************************
                //implement input validation and set local variables  here
                //***********************************************************************
                if (validPV && validAPR && validTerm)
                {
                    //***********************************************************************
                    //declare and create a new mortgage data object
                    //invoke this class's displayPayment method
                    //invoke this class's displayAmoritizationTable method
                    //***********************************************************************
                }
                else
                {
                    lblAmount.Text = "";
                    toolTip1.SetToolTip(lblAmount, "");
                    toolTip1.SetToolTip(rtfAmortTable, "");
                    if (!validPV)
                    {
                        setInputErrorField(txtMortgageAmount);
                    }
                    else if (!validAPR)
                    {
                        setInputErrorField(txtInterestRate);
                    }
                    else
                    {
                        setInputErrorField(txtMortgageTerm);
                    }
                }
            }
            catch (Exception ex)
            {
                prompt = "Please re-enter the mortgage information and try again";
                InputUtilities.showErrorMessage(ex, prompt);
                clearFields();
            }
        }
        private void calculateMortgagePayment()
        {
            double ir;
            int    np;

            try
            {
                ir       = APR / 12 / 100;
                np       = Term * 12;
                mPayment = (mPrincipal * ir) / (1 - Math.Pow(1 + ir, -np));
            }
            catch (System.InvalidOperationException ex)
            {
                InputUtilities.showErrorMessage(ex, "Unable to calculate mortgage payment");
            }
        }
        private bool validateInputField(TextBox txtBox, double min, double max)
        {
            double val;
            string errorMessage;
            bool   valid;

            errorMessage = "Enter a number greater than or equal to " + min
                           + " and less than or equal to " + max
                           + " in the " + txtBox.Tag;

            try
            {
                errorProvider1.SetError(txtBox, "");
                if (txtBox.Text == "")
                {
                    errorProvider1.SetError(txtBox, errorMessage);
                    valid = false;
                }
                else
                {
                    val = Double.Parse(txtBox.Text);
                    if (val < min || val > max)
                    {
                        errorProvider1.SetError(txtBox, errorMessage);
                        valid = false;
                    }
                    else
                    {
                        errorProvider1.SetError(txtBox, "");
                        valid = true;
                    }
                }
            }
            catch (Exception ex)
            {
                InputUtilities.showErrorMessage(ex, errorMessage);
                valid = false;
                txtBox.SelectAll();
            }
            return(valid);
        }
        private void clearFields()
        {
            try
            {
                txtMortgageAmount.Clear();
                txtInterestRate.Clear();
                txtMortgageTerm.Clear();
                rtfAmortTable.Clear();
                clearErrorProvider();

                clearPayment();
            }
            catch (System.Exception ex)
            {
                InputUtilities.showErrorMessage(ex, "Delete loan field information and try again");
            }
            finally
            {
                txtMortgageAmount.Focus();
            }
        }
        private void txtInput_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            TextBox txtBox;

            //if a navigation key do not react
            try
            {
                txtBox = (TextBox)(sender);
                if (InputUtilities.isNavigationKey(e))
                {
                    errorProvider1.SetError(txtBox, "");
                    //allow navigation keys to be entered
                    nonNumberEntered = false;
                    e.Handled        = false;
                }
                else if (InputUtilities.isNumericKey(e))
                {
                    errorProvider1.SetError(txtBox, "");
                    nonNumberEntered = false;
                    e.Handled        = false;
                }
                else
                {
                    string errorMessage = "Please enter only numeric values for the " + txtBox.Tag;
                    errorProvider1.SetError(txtBox, errorMessage);
                    //stop value form being entered since it is non-numeric
                    nonNumberEntered = true;
                    e.Handled        = true;
                }
            }
            catch (Exception ex)
            {
                //do nothing, catch the error later before calculations are done
                InputUtilities.clearException(ex);
            }
        }
 private void setMortgageInformation(int selectedMortgage)
 {
     try
     {
         clearFields();
         if ((selectedMortgage >= PredefinedMortgages.FirstMortgageNumber) && (selectedMortgage <= PredefinedMortgages.LastMortgageNumber))
         {
             txtMortgageAmount.Text = PredefinedMortgages.DefaultMortgageData[selectedMortgage].Principal.ToString();
             txtInterestRate.Text   = PredefinedMortgages.DefaultMortgageData[selectedMortgage].APR.ToString();
             txtMortgageTerm.Text   = PredefinedMortgages.DefaultMortgageData[selectedMortgage].Term.ToString();
         }
         else
         {
             txtMortgageAmount.Text = PredefinedMortgages.DEFAULT_PV.ToString();
             txtInterestRate.Text   = PredefinedMortgages.DEFAULT_PV.ToString();
             txtMortgageTerm.Text   = PredefinedMortgages.DEFAULT_TERM.ToString();
         }
         clearErrorProvider();
     }
     catch (System.Exception ex)
     {
         InputUtilities.showErrorMessage(ex, "Unable to set mortgage default mortgage information.");
     }
 }
Exemple #10
0
        private bool calculateAmortizationTable()
        {
            double principal   = mortgageInformation.Principal;
            double intRate     = mortgageInformation.APR / 12 / 100;
            int    numPayments = mortgageInformation.Term * 12;
            double payment     = mortgageInformation.Payment;

            double loanBalance       = 0.0;
            double interestPaid      = 0.0;
            double principalBalance  = 0.0;
            double interestBalance   = 0.0;
            double principalPaid     = 0.0;
            double totalPaid         = 0.0;
            double totalInterestPaid = 0.0;
            bool   valid             = false;

            try
            {
                //calculate total paid on the loan over the entire term of the loan
                totalPaid = numPayments * payment;

                //calculate total interest paid over the entire term of the loan
                totalInterestPaid = totalPaid - principal;

                //make room for the initial entries
                amortizationTable = new AmortizationData[numPayments + 1];

                //initialize the starting point of the array with the total values
                amortizationTable[0] = new AmortizationData(0, totalPaid, principal, totalInterestPaid, 0, 0);

                //initialized the Amortization Table to the initial loan values, so start counting at 1
                for (int count = 1; count <= amortizationTable.GetUpperBound(0); count++)
                {
                    //calculate loan balance from previous loan balance and mortgage payment
                    loanBalance = amortizationTable[count - 1].LoanBalance - payment;

                    //interest payment is the product of the interest rate per period and the remaining balance
                    //remaining balance is the previous periods Principal Balance
                    interestPaid = intRate * amortizationTable[count - 1].PrincipalBalance;

                    //calculate new principal balance from previous principle balance, payment, and interest paid
                    principalBalance = amortizationTable[count - 1].PrincipalBalance - (payment - interestPaid);

                    //calculate interest balance using previous interest balance and new interest payment
                    interestBalance = amortizationTable[count - 1].InterestBalance - interestPaid;

                    //Calculate principal paid using previous principle paid, payment, and interest paid
                    principalPaid = amortizationTable[count - 1].PrincipalPaid + (payment - interestPaid);

                    //Calculate total interest paid, using the previous total interest paid and current interest paid
                    totalInterestPaid = amortizationTable[count - 1].InterestPaid + interestPaid;

                    //'generate new data entry in the amortization table
                    //period, loanBalance, principleBalance, interestBalance, principalPaid, interestPaid
                    amortizationTable[count] = new AmortizationData(count,
                                                                    loanBalance,
                                                                    principalBalance,
                                                                    interestBalance,
                                                                    principalPaid,
                                                                    totalInterestPaid);
                }
                valid = true;
            }
            catch (Exception ex)
            {
                InputUtilities.showErrorMessage(ex, "Unable to create Amortization Table");
                valid = false;
            }
            return(valid);
        }