Link to home
Start Free TrialLog in
Avatar of Brayn66
Brayn66

asked on

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

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


Avatar of suprapto45
suprapto45
Flag of Singapore image

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.
http://thor.prohosting.com/~javarox/swing/swingtut2.html

more tutorial :) especially on JTable in Swing.
Avatar of Brayn66
Brayn66

ASKER

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.
 




Avatar of Brayn66

ASKER

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



Avatar of Brayn66

ASKER

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
Avatar of Brayn66

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Giant2
Giant2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial