Hey, I'm new here (as you can tell), and I'm in need of some help..

I need to make a interest calculator that displays the years, beginning amount of each year, the interest of each year, and the end amount of each year. I have the GUI all set up correctly and everything, but I just can't seem to figure out how to do the math for the interest for each year, or to find the end balance and set that to the next year's beginning balance. Any help would be appreciated. Thank you

import java.awt.*;
import javax.swing.*;
import BreezySwing.*;

public class Interest extends GBFrame {


   // Declare variables for the window objects
   private JLabel       BeginLabel;
   private JLabel       YearLabel;
   private JLabel       RateLabel;
   private JLabel       CmpLabel;
   private DoubleField  BeginField;
   private DoubleField  YearField;
   private DoubleField  RateField;
   private DoubleField  CmpField;
   private JButton      CalcButton;
   private JButton      ClrButton;
   private JTextArea    output;

   // Define the other instance variables
   private double  totalInterest;        //The total of all sales 

   // Constructor
   public Interest(){
      // Define the table's header line
      String header = Format.justify('l', "Year", 8) +
                      Format.justify('l', "Begin Amount", 17) +
                      Format.justify('l', "Interest", 14) +
                      Format.justify('l', "End Amount", 14) + "\n";

      // Instantiate the window objects                
      BeginLabel   = addLabel       ("Beginning Amount"    ,1,1,1,1);
      YearLabel    = addLabel       ("Years"               ,2,1,1,1);
      RateLabel    = addLabel       ("Rate"                ,3,1,1,1);
      CmpLabel     = addLabel       ("Compound Rate"       ,4,1,1,1);
      BeginField   = addDoubleField (0                     ,1,2,1,1);
      YearField    = addDoubleField (0                     ,2,2,1,1);
      RateField    = addDoubleField (0                     ,3,2,1,1);
      CmpField     = addDoubleField (0                     ,4,2,1,1);
      CalcButton   = addButton      ("Calculate"           ,5,1,1,1);
      ClrButton    = addButton      ("Clear Table"         ,5,2,1,1);
      output       = addTextArea    (header                ,1,3,3,5);

      // Disable the text area because we don't want the user to change it.
      // Set the focus to name field in preparation for the user's first input.
      output.setEditable(false);  
      BeginField.requestFocus();

      //Change color so I won't forget..
      //BeginField.setBackground(Color.red);
      //BeginField.setSelectionColor(Color.blue);

      // Initialize the totals to 0.
      totalInterest = 0;         
   }

   // Respond to the command buttons
   public void buttonClicked (JButton buttonObj){
      if (buttonObj == CalcButton){
         processInputs();
         BeginField.requestFocus();   //Move the cursor to the begin field
      }if (buttonObj == ClrButton){
         clearInputs();
      }
   }

   // Read the inputs, compute the commissions, format and display the
   // name, sale, and commission.
   private void processInputs(){
      // Declare the local variables
      double IntEarned = 0;              // interest
      double Rate;                       // rate
      double Cmp;                        // compound rate
      double Begin;                      // beginning amount
      double Year;                       // years
      double End;                        // ending amount

      // Read the user input
      Begin = BeginField.getNumber();
      Rate  = RateField.getNumber();
      Year  = YearField.getNumber();

    for (int Tyrs = 0; Tyrs <= Year; Tyrs++ ){
      IntEarned = Begin * Math.pow((1.0 + Rate), Tyrs); 
      End = Begin + IntEarned;

      // Display the years, beginning amount, interest earned, and the ending amount
      displayNumbers (Tyrs, Begin, IntEarned, End);
    }
   }

   private void clearInputs(){
       // Clears the "output" text area
    }

   // Format another line and append to the text area.
   private void displayNumbers (double num1, double num2, double num3, double num4){
      String numberLine = Format.justify ('l', num1, 8, 2) +
                          Format.justify ('l', num3, 17, 2) +
                          Format.justify ('l', num3, 14, 2) +
                          Format.justify ('l', num4, 14, 2) ;
      output.append (numberLine + "\n");
   }

   public static void main (String[] args){
      Interest theGUI = new Interest();
      theGUI.setSize (600, 300);              
      theGUI.setVisible (true);    
      theGUI.setTitle("Interest Table");
      theGUI.setLookAndFeel("MOTIF");
   }
}

Recommended Answers

All 2 Replies

So you are thinking along these lines (interest = 10% or 0.10)?

Year 0 - 1.0
Year 1 - 1.10
Year 2 - 1.21
Year 3 - 1.331
Year 4 - 1.4641
Year 5 - 1.61051
etc.

But you don't know the formulas? Is this a loan where there are payments and the loan gets smaller or a simple savinsgs with interest account? The above numbers reflect a simple savings account with 10% interest per year, compounded yearly. You would multiply the starting balance by the relevant year to get the balance at that year. The above takes no account of commissions or anything like that (I saw code regarding commissions). Can you be more specific about the type of calculations you are looking at?

Well my teacher says that we need to have compound as an input (so we can change whether its compounded yearly, monthly, etc), but I'm not entirely sure how to compute that with the interest..

Anyways, mostly what I need help on now is figuring out what is wrong with the Begin thing and why it changes at all..:

private void processInputs(){
// Declare the local variables
double IntEarned = 0; // interest
double Rate; // rate
double Cmp; // compound rate
double Begin; // beginning amount
double Year; // years
double End; // ending amount

 

// Read the user input

Begin = BeginField.getNumber();

Rate = RateField.getNumber();

Year = YearField.getNumber();

 
for (int Tyrs = 0; Tyrs <= Year; Tyrs++ ){
  IntEarned = Begin * Math.pow((1.0 + Rate), Tyrs);
  End = Begin + IntEarned;
 

// Display the years, beginning amount, interest earned, and the ending amount

displayNumbers (Tyrs, Begin, IntEarned, End);
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.