• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Replicating a simple bank account

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
kanaka tam
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Run away! Run away! Here, take this tiny ad with you:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic