Home

Example Application: Simple Interest

 

Introduction

Loan calculation or evaluation may involve various aspects. These include the present value (also called the principal), the interest rate, and the period. When it comes to the period, there are various ways you can consider it, such as the number of years, the number of months, or the number of days. In some cases, you can let the person who does the calculation specify the type of period that would be used. In some other cases, you can take care of that aspect yourself.

In this application, we will calculate the future value of a loan, based on the number of days as the period. In this exercise, we will provide the user with a calendar where he or she can specify the starting date and the ending date of the loan. We will calculate the number of days between these two dates and use it as the period.

Windows Controls:

Practical LearningPractical Learning: Creating the Application

  1. Start a new Window Application named SimpleInterest2
  2. Design the form as follows:
     
    Simple Interest
    Control Text Name TextAlign
    GroupBox GroupBox Loan Preparation    
    Label Label Principal    
    TextBox TextBox 0.00 txtPrincipal Right
    Label Label Interest Rate:    
    TextBox TextBox 0.00 txtInterestRate Right
    Label Label %    
    Label Label Loan Start Date:    
    DateTimePicker DateTimePicker   dtpStartDate  
    Label Label Loan End Date:    
    DateTimePicker DateTimePicker   dtpLoandEndDate  
    Label Label Periods:    
    TextBox TextBox 1 txtPeriods Right
    Label Label days    
    Button Button Calculate btnCalculate  
    GroupBox GroupBox Results    
    Label Label Interest Earned:    
    TextBox TextBox 0.00 txtInterestEarned Right
    Label Label Future Value:    
    TextBox TextBox 0.00 txtFutureValue Right
    Button Button Close btnClose  
  3. On the form, click the dtdEndDate date picker control
  4. In the Properties window, click the Events button and double-click CloseUp to generate its event
  5. Implement the event as follows:
     
    private void dtpEndDate_CloseUp(object sender, EventArgs e)
    {
                double Principal = 0.00D,
                       InterestRate = 0.00D,
                       Periods = 0.00D;
                double InterestEarned, FutureValue;
                DateTime StartDate, EndDate;
                TimeSpan spnPeriods;
    
                // Get the value of the principal
                try
                {
                    Principal = double.Parse(txtPrincipal.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("Invalid Principal Value");
                }
    
                // Get the interest rate
                try
                {
                    InterestRate  = double.Parse(txtInterestRate.Text) / 100;
                }
                catch (FormatException)
                {
                    MessageBox.Show("Invalid Interest Rate");
                }
    
                // Get the start and end dates
                StartDate = dtpStartDate.Value;
                EndDate = dtpEndDate.Value;
    
                // Make sure the end date doesn't occur before the start date
                if (EndDate < StartDate)
                {
                    MessageBox.Show("Invalid Date Sequence: " +
                              "the end date must occur after the start date");
                    dtpEndDate.Value = DateTime.Today;
                    return;
                }
    
                // Get the difference in days
                // The will be the periods
                spnPeriods = EndDate.Subtract(StartDate);
                int days = spnPeriods.Days;
                txtPeriods.Text = days.ToString();
                double p = 0.00D;
    
                // Because we will allow the user to directly specify 
                // the number of days, let's get the period from its text box
                try
                {
                    p = double.Parse(txtPeriods.Text);
                }
                catch (FormatException)
                {
                    MessageBox.Show("Invalid number of days");
                }
    
                // The actual period is gotten as follows
                Periods = p / 365;
                // Now we can perform the calculations
                InterestEarned = Principal * InterestRate * Periods;
                FutureValue = Principal + InterestEarned;
    
                // Display the values
                txtInterestEarned.Text = InterestEarned.ToString("C");
                txtFutureValue.Text = FutureValue.ToString("C");
    }
  6. Return to the form and click the Periods text box
  7. From the Events section of the Properties window, double-click Leave
  8. Implement the event as follows:
     
    private void txtPeriods_Leave(object sender, EventArgs e)
    {
            // If the user directly enters the number of days
            // in the Periods text box and press Tab,
            // we can perform the caltulation
            dtpEndDate_CloseUp(sender, e);
    }
  9. Return to the form and double-click the Calculate button
  10. Implement the event as follows:
     
    private void btnCalculate_Click(object sender, EventArgs e)
    {
            // If the user directly enters the number of days
            // in the Periods text box and clicks the Calculate
            // button, we can perform the caltulation
            dtpEndDate_CloseUp(sender, e);
    }
  11. Return to the form and double-click the Close button
  12. Implement the event as follows:
     
    private void btnClose_Click(object sender, EventArgs e)
    {
            Close();
    }
  13. Return to the form
  14. In the Properties window, click the Properties button Properties
  15. On the form, click the top date picker control
  16. In the Properties window, change the following values:
    CalendarForeColor: Blue
    CalendarMonthBackground: SkyBlue
    CalendarTitleBackColor: Navy
    CalendarTitleForeColor: Gold
    CalendarTrailingForeColor: DodgeBlue
  17. On the form, click the top date picker control
  18. In the Properties window, change the following values:
    CalendarForeColor: Maroon
    CalendarMonthBackground: Orange
    CalendarTitleBackColor: Sienna
    CalendarTitleForeColor: Khaki
    CalendarTrailingForeColor: White
  19. Execute the application and test the application
     
  20. Close the form and return to your programming environment
 

Home Copyright © 2007 FunctionX, Inc.