Hello folks,
I am a beginner struggling to code and after much struggle I have created a checking and saving account and the purpose of this program is to dispaly the balance amount in each account after an user enters an amount to deposit or withdraw from a text box and it should also display an "overdraft message" when the balance in either accounts go red (below zero). I have gotten thus with the following piece of code.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Bank extends
Applet implements ActionListener
{
private Button savingsButton, checkingButton;
private TextField amountTF;
private int amount;
private Account checking, saving;
public void init()
{
Label inputLbl;
inputLbl = new Label("Enter an amount to deposit or withdraw:");
add(inputLbl);
amountTF = new TextField(10);
add(amountTF);
amountTF.addActionListener(this);
savingsButton = new Button("Savings");
add(savingsButton);
savingsButton.addActionListener(this);
checkingButton = new Button("Checking");
add(checkingButton);
checkingButton.addActionListener(this);
checking = new Account(100);
saving = new Account(100);
saving.getBalance();
checking.getBalance();
}
public void actionPerformed (ActionEvent event)
{
if(event.getSource() == savingsButton)
{
amount = Integer.parseInt(amountTF.getText());
if(amount > 0)
saving.depositMoney(amount);
else
if(amount < 0)
saving.withdrawMoney(amount);
}
if(event.getSource() == checkingButton)
{
amount = Integer.parseInt(amountTF.getText());
if(amount > 0)
checking.depositMoney(amount);
else
if(amount < 0)
checking.withdrawMoney(amount);
}
repaint();
}
public void paint(Graphics g)
{
saving.display(g);
checking.display(g);
}
}
class Account
{
private int balance;
public Account(int amount)
{
}
public void depositMoney(int amount)
{
balance = balance + amount;
}
public void withdrawMoney(int amount)
{
balance = balance + amount;
}
public int getBalance()
{
return balance;
}
public void display(Graphics g)
{
g.drawString("Your Savings Account Balance is: $" + balance, 70, 90);
g.drawString("Your Checking Account Balance is: $" + balance, 70, 110);
}
}
But i think i am making a mistake somewhere cause when i click on one button both account balances gets changed. I don't know what i am doing wrong here. Help appreciated..
Thanx,
kanakatam