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 Learning: Creating the Application
1. Start a new Window Application named SimpleInterest2
2. Design the form as follows:

 Control Text Name TextAlign GroupBox Loan Preparation Label Principal TextBox 0.00 txtPrincipal Right Label Interest Rate: TextBox 0.00 txtInterestRate Right Label % Label Loan Start Date: DateTimePicker dtpStartDate Label Loan End Date: DateTimePicker dtpLoandEndDate Label Periods: TextBox 1 txtPeriods Right Label days Button Calculate btnCalculate GroupBox Results Label Interest Earned: TextBox 0.00 txtInterestEarned Right Label Future Value: TextBox 0.00 txtFutureValue Right 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"); }```
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); }```
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); }```
12. Implement the event as follows:

 ```private void btnClose_Click(object sender, EventArgs e) { Close(); }```
14. In the Properties window, click the Properties button
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