Computing Interest
Using A Console Application and MFC
PART 1) of the assignment will compute
interest using the console application, and PART 2) will create a graphic user
interface (GUI) for the same problem.
1)
Write a
program that helps an investor compute information about a given savings
account.
a)
Given
an initial balance, P, an interest rate, r,
and the number of years that the money is invested, t, compute the total
value of the account if the interest is compounded continuously. (As a reminder, the total value after t
years is:
.
You can approximate e by declaring a constant double and setting it equal to 2.718281828459045)
Have
the main program prompt the user to input the variables, and call a
value-returning function computeTotal
that returns the final total
value.
b)
There
are four variables in the equation above
: total, P, r, and t.
Allow the user to put in ANY of the three variables, and compute and
output the fourth. For example, the user might input a starting amount (P),
ending amount (total)
and years (t), and the program would output the
interest rate(r) necessary to achieve that final amount.
One
way of doing this (a very simple way) would be to instruct the user to enter 0
(ZERO) for the variable that is unknown.
A more elegant approach would use a menu to allow the user to choose
which variable would be unknown. Feel
free to be creative in your approach.
c)
EXTRA
CREDIT: Allow the user to choose one of
two options: compounded continuously (as
above) or compounded monthly. ( total
=
)
2)
Much
of the user friendly software we see today has a graphical user interface
(GUI, pronounced gooey). This is the windows desktop that we have
become familiar with. If we supply a
GUI, users can click on buttons and enter text into boxes for input. The steps below will help you to write your
first GUI.
a)
Open
a new project choosing CLR (Microsoft’s Common Language Runtime protocols) as
the Project type. Choose Windows Forms
Application under Templates. Below it is
called compute interest.

a)
If you
wish your interface to be set up differently, you may use the view menu to open
the solution explorer, properties, and the toolbox. You can then dock all three windows. (See My first GUI example to get help with
this).
b)
Your
screen should then look like this:

c)
You
may link and run your program. When you
run your program, an empty form comes up.
Click on the close button at the corner to close that window.
d)
You
can see the Toolbox at the right hand side of the screen above. Looking at part a) of the problem as described for the console application in Part 1) above, we wish to create input
boxes for our three known variables in the interest computation program. This will
allow the user to enter values. Drag three labels (A) and three text boxes (ab|)
onto the form.

e)
For
each of the label boxes: Select the
label, then change the text of the label using the Properties window.

a)
Place
another text box, and label box for the output variable, changing the caption
of the label box as above.
Click
on each of the text boxes, and change the name in the properties window. This will allow you to use a meaningful name
in your program.
Right
click on each button, changing one caption to compute and one to clear.
For these buttons, you should also change the ID to something more meaningful.
The
compute button will be used to
calculate the final result, and the clear
button will be used to clear all input values.

b)
Drag
two buttons from the Toolbox onto the form.
Change the Text properties and the name of each of these buttons. You
may “Run without Debugging” again to see your program Compile, Link and
Execute.

c)
Double
click on the Clear button to go to the code of the event handler to enter the
C++ commands below.

d)
Double
click the compute button to create the event handler for compute. You wish the calculation of interest to be
performed each time the user clicks on the compute button. Modify your code from Part
I of this assignment to use the user’s input to set the value of the edit
box that has not been filled in.
Hint: First write the code that
allows the user to place the starting amount, interest rate and years and have
your program fill in the final balance. Then change your code so that the user
can input ANY of the three variables, and have the fourth computed. Remember,
to place a value from a text box into a double variable, you must use the
TryParse function. For example:
double
initialDeposit;
Double::TryParse(deposit->Text,initialDeposit); //this function takes the text from the
deposit textbox,
//converts it to a double, and places
//it
into the variable initialDeposit (see the add5 example for //more help on this)
How
can you check to see which of the boxes is empty and must be computed?
You
can compare a textbox to the empty string to see if it is empty:
if (deposit->Text == "")
but
to avoid errors instead use:
if (deposit->Text->Trim() == "")
The
expression above uses the Trim() function to remove all leading white
spaces and ending white spaces from the text that is in deposit, before it
compares the text to the empty string.
This way, a use cannot enter spaces (looks empty but is not!) and cause
the program to fail.
NOTE: TO CALCULATE THE ABOVE YOU SHOULD HAVE e
DECLARED AS A constant double, AND YOU MUST #include <cmath>.
BOTH THESE LINES CAN BE PLACED AT THE BEGINNING OF THE MAIN HEADER FILE
OF YOUR APPLIATION (LOOK UNDERTHE FILES TAB OF THE WORKSPACE WINDOW AND CHOOSE
THE HEADER FILE WITH THE NAME OF YOUR APPLICATION). THE BEGINNING OF THE CODE,
WITH THESE TWO LINES ADDED CAN BE:
e)
EXTRA
CREDIT: Allow the user to choose one of
two options: compounded continuously (as
above) or compounded monthly. ( total =).
You can do this with a few more buttons as above, or explore the help
files to use radio buttons.