Home

Example Application:
The Compound Interest

 

Compound Interest - Results

Introduction

The application we are going to create is used to calculate the amount owed on a loan using the principal, the interest rate, and the period.

Windows Controls:

Practical Learning Practical Learning: Creating the Application

  1. Start a new Windows Application named CompoundInterest1
  2. Design the form as followed:
     
    Compound Interest - Form Design
    Control Name Text Additional Properties
    GroupBox GroupBox   Loan Setup  
    Label Label   Principal:  
    TextBox TextBox txtPrincipal 0.00 TextAlign: Right
    Label Label   Interest Rate:  
    TextBox TextBox txtInterestRate 8.25 TextAlign: Right
    Label Label   %  
    Label Label   Number of Periods:  
    TextBox TextBox txtPeriods 1 TextAlign: Text
    Label Label   years  
    GroupBox GroupBox   Compound Frequency  
    RadioButton RadioButton rdoMonthly    
    RadioButton RadioButton rdoQuarterly    
    RadioButton RadioButton rdoSemiannually    
    RadioButton RadioButton rdoAnnually    
    GroupBox GroupBox   Results  
    Label Label   Interest Earned:  
    TextBox TextBox txtInterestEarned 0.00 TextAlign: Right
    ReadOnly: True
    Label Label   Amount Earned:  
    TextBox TextBox txtFutureValue 0.00 TextAlign: Right
    ReadOnly: True
    Button Button btnCalculate Calculate  
    Button Button btnClose Close  
  3. Test the application
  1. On the form, click Monthly
  2. In the Properties window, double-click Checked to set its value to True
  3. On the form, double-click the Calculate button and implement its Click() event as follows:
     
    private void btnCalculate_Click(object sender, EventArgs e)
    {
        double principal = 0.00,
               interestRate = 0.00,
               interestEarned = 0.00;
        double futureValue = 0.00,
               ratePerPeriod = 0.00,
               periods = 0;
        int compoundType = 0;
    
        // Retrieve the value of the principal
        try
        {
            principal = double.Parse(txtPrincipal.Text);
        }
        catch (FormatException)
        {
            MessageBox.Show("The value you entered for the principal " +
    			"is not valid\nPlease try again");
        }
    
        // Retrieve the interest rate
        try
        {
            interestRate = double.Parse(txtInterestRate.Text) / 100;
        }
        catch (FormatException)
        {
            MessageBox.Show("The value you entered for the interest " +
    			"rate is not valid\nPlease try again");
        }
    
        // Find out what radio button was clicked to 
        // apply the compound frequency
        if (rdoMonthly.Checked)
            compoundType = 12;
        else if (rdoQuarterly.Checked)
            compoundType = 4;
        else if (rdoSemiannually.Checked)
            compoundType = 2;
        else
            compoundType = 1;
    
        // Get the number of periods
        try
        {
            periods = double.Parse(txtPeriods.Text);
        }
        catch (FormatException)
        {
            MessageBox.Show("The value you entered for the number " +
    			"of periods is not valid\nPlease try again");
        }
    
                // These values will make the calculation easier to read
                double i = interestRate / compoundType;
                double n = compoundType * periods;
    
                // Perform the necessary calculations
                ratePerPeriod = interestRate / periods;
                futureValue = principal * Math.Pow(1 + i, n);
                interestEarned = futureValue - principal;
    
                // Display the values in the appropriate text boxes
                txtInterestEarned.Text = interestEarned.ToString("C");
                txtFutureValue.Text = futureValue.ToString("C");
    }
  4. On the form, double-click the Close button and implement its OnClick() event as follows:
     
    private void btnClose_Click(object sender, EventArgs e)
    {
          Close();
    }
  5. Test the application
     
    Compound Interest - Results
  6. After using it, close the form and return to your programming environment
 

Home Copyright © 2007 FunctionX, Inc.