Solved

java simple investment calculator by using a gui and result displayed in table

Posted on 2004-10-10
7
1,289 Views
Last Modified: 2008-03-17
I am having trouble understanding how to put together a program that will execute by gathering user input information through a GUI and then displaying the results in a table format.

By using a gui (breezy swing):

Collect (inputs) from user:
 1. the investment principle
 2. interest rate
 3. number of years to invest

Click calculate button.

The result (outputs) should show a table that displays:
1. 20 years worth of data
2. principle at the beginning of year
3. interests earned
4. principle at the end of the year

I have written javacode and understand calculations and the for loop that I will use for the 20 year output table. I also have an understanding breezy swing but cannot understand how to implement a gui and then display the results in a table format.

Any help I would appreciate.
lauren


0
Comment
Question by:Brayn66
  • 4
  • 2
7 Comments
 
LVL 16

Expert Comment

by:suprapto45
ID: 12274105
Mmm....is breezy swing the same as normal java swing? If so, it is quite easy then. At least, it would be similar :).

First of all, what you need to do is to place a frame on your Java application. After having that particular frame, you can locate any components there such as Button, Label, Textfield and etc. If you want the calculation done when you press the button, you can have some ActionListener on that. ActionListener will allow you to have some Java codes that will be executed when you press the component that is associated with that ActionListener.

Collect (inputs) from user:
 1. the investment principle
 2. interest rate
 3. number of years to invest

If you want your Java application to be able to receive those factors, you need to have three TextField on your application and get the text inside each of the TextField and assign them into variable for calculation.

This is good tutorial about Swing:
http://java.sun.com/docs/books/tutorial/uiswing/

I hope that helps. If you need some further help, please do not hesitate to contact me.
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 12274114
http://thor.prohosting.com/~javarox/swing/swingtut2.html

more tutorial :) especially on JTable in Swing.
0
 

Author Comment

by:Brayn66
ID: 12283694
Thank you for your help.
I will clarify the project requirements:

I.  By using a GUI:
    Collect user Inputs:
     1. interests rates
     2. initial principle
     3. number of years to stay in fund

II. On GUI, click evaluate
    Output should display a table whose columns contain:
     1. year number
     2. principle at the beginning of the year
     3. interests earned
     4. principle at the end of the year

 * calculate for a given number of years and compound annually for an end result.

I think that once the code is executed, a GUI should appear and have three labels and three text fields that will satisfy the input requirements. The user will click the <<<evaluate>>> button for the compounded annually calculation to occur.

Once the calculation occurs, it should display in a GUI that shows the four output requirements.


My code is a bit broken but I think I am close:

import javax.swing.*;
import Breezy.Swing.*;

public class mutualfund2 extends GBFrame{

                  //variables declared for windows object

      private JLabel  principleLabel;
      private JLabel  interestsLabel;
      private JLabel  investyearsLabel;

      private DoubleField principleField;
      private DoubleField interestrateField;
      private DoubleField investYearsField;
      private DoubleField yearEndField;

      private JButton calculateButton;
            }

                  //constructor
public mutualfund2(){
                  //instantiate and  add window objects to the window.

                  principleLabel = addlabel  ("Principle amount"    ,1,1,1,1);
                  interestsLabel = addlabel   ("Interests Rate"     ,1,2,1,1);
                  investyearsLabel = addlabel  ("Years Invested"    ,1,3,1,1);

                  principleField = addDoubleField (             ,2,1,1,1);
                  interestsField = addDoubleField (             ,2,2,1,1);
                  investyearsField = addDoubleField (           ,2,3,1,1);

                  calculateButton = addButton ("<<<<Evaluate>>>>"           ,3,2,1,1);
            }


                  // respond to button click events
public void buttonClicked (JButton buttonObj){
                  //local variables

// this is the area that I am having trouble implementing a strategy to carry out the calculations


            }
                  // execution begins in the method main as usual.
public static void main (String[] args) {

                  mutualfund2 theGUI = mutualfund2();
                  theGUI.SetLookAndFeel("Motif");
                  theGUI.SetSize  (300,200);
                  theGUI.SetVisible (true);

            }            


I'm having trouble with creating an output GUI format for the requirements as well as calculation help.
thanks, lauren

ps - I will send a close solution for a terminal window format in the next message. Maybe this can be incorporated somehow.
 




0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:Brayn66
ID: 12283733
here it is (the terminal window format):


import TerminalIO.KeyboardReader;
public class mutualfund1 {
      public static void main(String [] args) {

      KeyboardReader reader = new KeyboardReader();

      int yearsFund;
      double initialPrinciple;
      double interestRate;
      double compoundedInterest;
      double yearEndPrinciple;

      System.out.println();
initialPrinciple = reader.readDouble("Please enter a whole number for your initial principle amount to invest: $ ");
      System.out.println("");

interestRate = reader.readDouble("Please enter a whole number for the compounded annually interest rate: % ");
      System.out.println("");

yearsFund = reader.readInt("Please enter the number of years to stay in the fund:  ");
      System.out.println();
                System.out.println();

       interestRate = (interestRate / 100);
       compoundedInterest = (initialPrinciple * interestRate);
       yearEndPrinciple = (initialPrinciple + compoundedInterest);

      for(yearsFund = 1; yearsFund<=20; yearsFund = yearsFund+1)
            {
yearEndPrinciple += yearEndPrinciple + compoundedInterest;
System.out.println(" Year:" + yearsFund + "   Beginning Principle:$" + initialPrinciple + "   Interest Earned:$" + compoundedInterest + "   Year End Principle:$" + yearEndPrinciple);
                System.out.println();
            }
                            }
      }



////This code doesn't calculate the compounded interest because I am not sure how to incorporate part of the formula...

A=P(1+r)^n             (not sure how to set up the compound annually part)
 
A = compounded principle      (calculated result)
P = principle                             (user input)
r = interest rate                        (user input)
n = number of interest periods  (in this case it would be annually)


Thank you, lauren



0
 

Author Comment

by:Brayn66
ID: 12292063
Please review my code (last two entries) and let me know of my discrepancies. I am having a difficult time fiquring this one out. I have reviewed the  tutorials but they seem cloudy and I am not sure if the package I have to use, coincides with those online examples. I have imported javax and breezy swing to use for the GUI. This is in alignment with my book and software loaded (not to mention that the course syllabus dictates their use).

I do appreciate the advice and links previous to this.

Thanks alot!

lauren
0
 

Author Comment

by:Brayn66
ID: 12294091
Urgent help needed!
I am having trouble with this project.
The java program requires a created GUI that calculates for an annual rate of return on an investment.  
The specific specs are:
 
I.  By using a GUI:
    Collect user Inputs:
     1. interests rates
     2. initial principle
     3. number of years to stay in fund

II. On the GUI, click evaluate.
    Output should display a table whose columns contain:
     1. year number
     2. principle at the beginning of the year
     3. interests earned
     4. principle at the end of the year

III.Calculate for a given number of years and compound annually for an end result.

I think that once the code is executed, a GUI should appear and have three labels and three text fields that will satisfy the input requirements. The user will click the <<<evaluate>>> button for the compounded annually calculation to occur.

Once the calculation occurs, it should display the results in a GUI (table?) that shows the four output requirements.

Here is my code.

import javax.swing.*;
import Breezy.Swing.*;

public class Mutualfund3 extends GBFrame{
            //variables declared for windows object

            JLabel  principleLabel;
            JLabel  interestsLabel;
            JLabel  investyearsLabel;

            DoubleField interestrateField;
            DoubleField principleField;
            DoubleField yearEndField;
            DoubleField investYearsField;
            JButton calculateButton;
            }

            //constructor
      public Mutualfund3 (){
                  //instantiate and  add window objects to the window.

                  principleLabel = addlabel ("Principle amount"    ,1,1,1,1);
                  interestsLabel = addlabel ("Interests Rate"     ,1,2,1,1);
                  investyearsLabel = addlabel ("Years Invested"    ,1,3,1,1);

                  principleField = addDoubleField (0,             2,1,1,1);
                  interestsField = addDoubleField (0,             2,2,1,1);
                  investyearsField = addDoubleField (0,           2,3,1,1);

                  calculateButton = addButton ("<<<<Evaluate>>>>",3,1,1,1);
            }

            // respond to button click events
      public void buttonClicked (JButton buttonObj){
            //local variables
                  Evaluate eval = new Evaluate();


            }


            // execution begins in the method main as usual.

      public static void main (String[] args) {

                  Mutualfund3 theGUI = new Mutualfund3();
                  theGUI.setSize (300,150);                  //width = 300, height = 150 in pixels
                  theGUI.setVisible (true);                 //window is visible

            }



I'm having trouble with creating an output GUI format for the requirements as well as calculation help.
Any advice would be appreciated.
thanks, lauren
0
 
LVL 12

Accepted Solution

by:
Giant2 earned 500 total points
ID: 12433169
>I.  By using a GUI:
    Collect user Inputs:
     1. interests rates
     2. initial principle
     3. number of years to stay in fund

You must create a TextField for each

>II. On the GUI, click evaluate.
    Output should display a table whose columns contain:
     1. year number
     2. principle at the beginning of the year
     3. interests earned
     4. principle at the end of the year
>III.Calculate for a given number of years and compound annually for an end result.

You must create a Button and intercept the click over it:
btnBUTTON.setText("Evaluate");
        btnBUTTON.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBUTTONActionPerformed(evt);
            }
        });

And the method:
    private void btnBUTTONActionPerformed(java.awt.event.ActionEvent evt) {
//put here the code to do all you want
//create a JTable contained in a ScrollingPane
//for the JTable create a TableModel where you can specify the column names and the data to display
    }

I don't post full code because seems you are using not the standard swing.
Why you use Breezy.Swing?

Bye, Giant.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now