Click to See Complete Forum and Search --> : Simulating a bank Account


kanakatam
04-01-2005, 03:01 PM
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

Khalid Ali
04-01-2005, 07:56 PM
you are not distinguishing about type of account balance in your Account class. there is one balance that is shown. Rethink your logic in that part

kanakatam
04-02-2005, 02:06 PM
Khalid,
Thank you. I figured what i was doing wrong, so i created two seperate variables for storing the balances from checking and saving account and displayed it in the paint method.


import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class Lab7 extends Applet
implements ActionListener
{
private Button savingsButton, checkingButton;
private TextField amountTF;
private int amount;
private int check;
private int save;
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();
saving = new Account();

}

public void actionPerformed (ActionEvent event)
{

if(event.getSource() == savingsButton)
{
amount = Integer.parseInt(amountTF.getText());

if(amount > 0)
{
saving.depositMoney(amount);
save = saving.getBalance();
}
else
if(amount < 0)

{
saving.withdrawMoney(amount);
save = saving.getBalance();
}
}

if(event.getSource() == checkingButton)
{
amount = Integer.parseInt(amountTF.getText());

if(amount > 0)
{
checking.depositMoney(amount);
check = checking.getBalance();
}


else
if(amount < 0)
{
checking.withdrawMoney(amount);
check = checking.getBalance();
}

}

repaint();
}

public void paint(Graphics g)
{
g.drawString("Your Checking Account Balance = $" + check, 70, 90);
g.drawString("Your Saving Account Balance = $" + save, 70, 110);
if(check < 0)
g.drawString("Warning!!! Negative Checking Balance", 70, 130);
if(save < 0)
g.drawString("Warning!!! Negative Saving Balance", 70, 150);

}
}


class Account
{
private int balance;


public Account()
{

}

public void depositMoney(int amount)
{
balance = balance + amount;

}
public void withdrawMoney(int amount)
{
balance = balance + amount;

}
public int getBalance()
{
return balance;

}
public void display()
{


}


}



But i am supposed to use the method display in the Account class and call it up at the paint method. I thought of putting the code inside display() instead of paint method but i realized it won't work because it will not recognize the variables check and save and so i am flustered now. My program works fine but i should use the display method and call it up at paint method which i am stuck at now.
Help appreciated,
Kanaka

Khalid Ali
04-02-2005, 10:35 PM
doesn't look like you are really doing any processingwith your messages, what you can do is pass the values to display method and then form the message string in the display method and get it returned to the paint method for display.