|
Example Application: Simple Interest |
|
|
This application shows how to calculate the interest
accumulated on a loan and the total value that will paid at the end of the loan.
|
Practical
Learning: Creating the Application
|
|
- To start a new application, on the main menu, click File -> New ->
Project
- In the middle list, click Windows Application and change the Name
to SimpleInterest1
- Click OK
- If the Resource View is not visible, on the main menu, click View
-> Other Windows -> Resource View.
In the Resource View,
right-click the name of the project -> Add -> Resource...
- In the Add Resource dialog box, click Icon
- Click New
- Design the icon as follows:
- Right-click 32x32, 4 bit, BMP and click Delete Image Type
- Design the icon as follows:
- Right-click 32x32, 4 bit, BMP and click Delete Image Type
- Save all
- Access the form and design it as follows:
|
Control |
Text |
Name |
Other Properties |
GroupBox |
|
&Loan Preparation |
|
|
Label |
|
&Principal |
|
|
TextBox |
|
0.00 |
txtPrincipal |
TextAlign: Right |
Label |
|
Interest &Rate: |
|
|
TextBox |
|
0.00 |
txtInterestRate |
TextAlign: Right |
Label |
|
% |
|
|
Label |
|
Perio&ds: |
|
|
TextBox |
|
1 |
txtPeriods |
TextAlign: Right |
Label |
|
Months |
|
|
Button |
|
C&alculate |
btnCalculate |
ImageAlign: TopCenter TextAlign: BottomCenter |
Button |
|
&Close |
btnClose |
ImageAlign: TopCenter TextAlign: BottomCenter |
GroupBox |
|
&Results |
|
|
Label |
|
Interest &Earned: |
|
|
TextBox |
|
0.00 |
txtInterestEarned |
TextAlign: Right |
Label |
|
&Future Value |
|
|
TextBox |
|
0.00 |
txtFutureValue |
TextAlign: Right |
|
- On the form, click the Calculate button
- In the Properties window, click Image and click its ellipsis
button
- In the open dialog box, locate the SimpleInterest sub-folder
inside the SimpleInterest folder of the current project
- Select the icon that represents a calculator
- Click Open
- On the form, click the Close button
- In the Properties window, click Image and click its ellipsis
button
- As done for the Calculate button, locate and select the other icon
- Click Open
- On the form, double-click the Calculate button and implement its
Click event as follows:
System::Void btnCalculate_Click(System::Object^ sender,
System::EventArgs^ e)
{
double Principal = 0.00,
InterestRate = 0.00,
InterestEarned,
FutureValue;
double Periods = 0.00;
try {
Principal = double::Parse(txtPrincipal->Text);
}
catch(FormatException ^)
{
MessageBox::Show(L"The value you entered for the "
L"principal is not valid");
}
try {
InterestRate = double::Parse(txtInterestRate->Text);
}
catch(FormatException ^)
{
MessageBox::Show(L"Wrong Value: The interest rate must "
L"be a value between 0 and 100");
}
try {
Periods = double::Parse(txtPeriods->Text);
}
catch(FormatException ^)
{
MessageBox::Show(L"You entered an invalid value for the periods");
}
double I = InterestRate / 100;
double p = Periods / 12;
InterestEarned = Principal * I * p;
FutureValue = Principal + InterestEarned;
txtInterestEarned->Text = InterestEarned.ToString(L"F");
txtFutureValue->Text = FutureValue.ToString(L"F");
}
- Return to the form and double-click the Close button
- Implement its Click event as follows:
System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
Close();
}
- To execute the application, press F5
- Enter 22580 for the principal, 12.75 for the interest rate, and 60
for the periods
- Click Calculate
- Close the form and return to your programming environment
|
|