lbgladson 0 Junior Poster in Training

I have a program to write an application that takes the initial balance, annual rate, and number of years and finds the balance of the account at the end of each year. I have the following code but my Annual Rate label is in the incorrect place, the calculate button doesn't display the answer in the text area, and it doesn't calculate the amount correctly. Every time I try to find out where my errors are I get lost in the code. Any help would be great.

/**
       A bank account has a balance that can be changed by 
       deposits and withdrawals
*/
public class BankAccount
{ 
	private double balance; 
	
	public BankAccount()
	{  
		balance = 0;
	}
	
	public BankAccount(double initialBalance)
	{  
		
		
	}
	
	public void deposit(double amount) 
	{  
		balance = balance + amount;
    }
	
	public void withdraw(double amount) 
	{  
		balance = balance - amount;
	}
	
	public double getBalance()
	{  
		return balance; 
	}
	
    public void transfer(double amount, BankAccount other)
    {  
    	withdraw(amount);
    	other.deposit(amount);
    }
}
public class SavingsAccount extends BankAccount
{  
	private double interestRate;
	private double initialBalance;
	
	public SavingsAccount(double rate, double balance) 
	{  
		interestRate = rate;
		initialBalance = balance;
	}
	
	public void addInterest() 
	{
		double interest = initialBalance * interestRate / 100;
		deposit(interest); 
	}
}
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;	 

public class SavingsCalculatorApplication extends JFrame 
{
	private JTextField rateField;
	private JTextField balanceField;
	private JTextField yearField;
	private JButton button;
	private JPanel panel;
	private BankAccount account;
	private JLabel rateLabel;
	private JLabel balanceLabel;
	private JLabel yearLabel;
	private TextArea result;
	
	public SavingsCalculatorApplication() 
	{
		account = new BankAccount();
		createTextField();
		createButton();
		createPanel();
		
		setSize(300, 300);
	}
	
	private void createTextField() 
	{
		rateLabel = new JLabel("Annual Rate: ");
		balanceLabel = new JLabel("Initial Balance:");
		yearLabel = new JLabel("Number of years:");
		balanceField = new JTextField(10);
		rateField = new JTextField(10);
		yearField = new JTextField(10);
		result = new TextArea();
	}
	
	private void createButton() 
	{
		button = new JButton("Calculate");
		
		class AddCalculateListener implements ActionListener 
		{
			
			public void actionPerformed(ActionEvent event) 
			{
				double balance = Double.parseDouble(balanceField.getText());
				double rate = Double.parseDouble(rateField.getText());
				double years = Double.parseDouble(yearField.getText());
				
				account = new BankAccount(balance);
				rate = rate / 100;
				
				for (int i = 1; i <= years; i++) 
				{
					double newbalance = account.getBalance() * rate;
					account.deposit(newbalance);
					String test = String.valueOf(account.getBalance()) + "\n";
					System.out.println(test);
					result.append(test);
				}
			}
		}
		
		ActionListener listener = new AddCalculateListener();
		button.addActionListener(listener);
	}
	
	private void createPanel() 
	{
		panel = new JPanel();
		panel.add(balanceLabel);
		panel.add(balanceField);
		panel.add(rateLabel);
		panel.add(rateField);
		panel.add(yearLabel);
		panel.add(yearField);
		panel.add(button);
		panel.add(result);
		add(panel);
	}
}
import javax.swing.JFrame;
public class SavingsCalculator
{
	public static void main(String[] args)
	{
		JFrame frame = new SavingsCalculatorApplication();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setTitle("SavingsCalculator");
		frame.setVisible(true);
	}
}
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.