*
The moose likes Beginning Java and the fly likes Replicating a simple bank account Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Replicating a simple bank account" Watch "Replicating a simple bank account" New topic
Author

Replicating a simple bank account

kanaka tam
Ranch Hand

Joined: Jan 19, 2004
Posts: 42
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
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You have made two instances of your Account class, one for checking and one for savings. But in the Account class you draw the same balance twice with both "checking" and "savings" descriptions. Each Account instance only knows one account, so it should only display one balance.

Now, how would an Account instance know what description to print? It should either say "checking" or "savings". Look for a way to tell the Account which one it is so it can display the description later.

I've never done drawing on graphics like that. The way you called checking.display(g) and savings.display(g) ... does that display one after the other or does one replace the other on screen?

Let me know if that helps! And look into the CODE button below the editor when you post code. That helps preserve your indentation and makes things easier for all to read.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
kanaka tam
Ranch Hand

Joined: Jan 19, 2004
Posts: 42
Stan,
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.



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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Replicating a simple bank account