Welcome to Dream.In.Code
Become a Java Expert!

Join 150,407 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 951 people online right now. Registration is fast and FREE... Join Now!




Bank Account class trouble

 
Reply to this topicStart new topic

Bank Account class trouble

scorpionviolinist
3 Apr, 2008 - 08:47 AM
Post #1

New D.I.C Head
*

Joined: 29 Feb, 2008
Posts: 36

I am having trouble with the private static int numAccount and the static getNumAccounts. I have to declare a private static integer variable numAccounts to hold the number of accounts that exist, and increment it every time an account is created. Also, I need to add a static method getNumAccounts that returns the total number of accounts. The error message I get is that it cannot find symbol for variable numAccounts.


CODE
//*******************************************************
// Account1.java
//
// A bank account class with methods to deposit to, withdraw from,
// change the name on, and get a String representation
// of the account.
//*******************************************************

public class Account1
{
  private double balance;
  private String name;
  private long acctNum;

  //----------------------------------------------
  //Constructor -- initializes balance, owner, and account number
  //----------------------------------------------
  public Account1(double initBal, String owner, long number)
  {
    balance = initBal;
    name = owner;
    acctNum = number;
  }

  //----------------------------------------------
  // Checks to see if balance is sufficient for withdrawal.
  // If so, decrements balance by amount; if not, prints message.
  //----------------------------------------------
  public void withdraw(double amount)
  {
    if (balance >= amount)
       balance -= amount;
    else
       System.out.println("Insufficient funds");
  }

  //----------------------------------------------
  // Adds deposit amount to balance.
  //----------------------------------------------
  public void deposit(double amount)
  {
    balance += amount;
  }

  //----------------------------------------------
  // Returns balance.
  //----------------------------------------------
  public double getBalance()
  {
    return balance;
  }

  //----------------------------------------------
  // Returns account number.
  //----------------------------------------------
  public long getAcctNumber()
  {
    return acctNum;
  }

  //----------------------------------------------
  // Returns a string containing the name, account number, and balance.
  //----------------------------------------------
  public String toString()
  {
    return "Name: " + name +
    "\nAccount Number: " + acctNum +
    "\nBalance: " + balance;
  }

  private static int numAccounts()
  {
    numAccounts++;
  }

  public static int getNumAccounts()
  {
    return NumAccounts;
  }
  }



User is offlineProfile CardPM
+Quote Post

GWatt
RE: Bank Account Class Trouble
3 Apr, 2008 - 08:53 AM
Post #2

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,360



Thanked: 31 times
Dream Kudos: 500
My Contributions
That's because you don't ever declare numAccounts, you just try and use it. After the class declaration, put the line
private static int numAccounts = 0;
User is offlineProfile CardPM
+Quote Post

scorpionviolinist
RE: Bank Account Class Trouble
3 Apr, 2008 - 11:50 AM
Post #3

New D.I.C Head
*

Joined: 29 Feb, 2008
Posts: 36

Thank you. But now, when I test the class with the program, it doen't return the correct number of accounts. Each time, it says "0". why?...

CODE
//*******************************************************
// Account.java
//
// A bank account class with methods to deposit to, withdraw from,
// change the name on, and get a String representation
// of the account.
//*******************************************************

public class Account
{
  private static int numAccounts = 0;
  private double balance;
  private String name;
  private long acctNum;

  //----------------------------------------------
  //Constructor -- initializes balance, owner, and account number
  //----------------------------------------------
  public Account(double initBal, String owner, long number)
  {
    balance = initBal;
    name = owner;
    acctNum = number;
  }

  //----------------------------------------------
  // Checks to see if balance is sufficient for withdrawal.
  // If so, decrements balance by amount; if not, prints message.
  //----------------------------------------------
  public void withdraw(double amount)
  {
    if (balance >= amount)
       balance -= amount;
    else
       System.out.println("Insufficient funds");
  }

  //----------------------------------------------
  // Adds deposit amount to balance.
  //----------------------------------------------
  public void deposit(double amount)
  {
    balance += amount;
  }

  //----------------------------------------------
  // Returns balance.
  //----------------------------------------------
  public double getBalance()
  {
    return balance;
  }

  //----------------------------------------------
  // Returns account number.
  //----------------------------------------------
  public long getAcctNumber()
  {
    return acctNum;
  }

  //----------------------------------------------
  // Returns a string containing the name, account number, and balance.
  //----------------------------------------------
  public String toString()
  {
    return "Name: " + name +
    "\nAccount Number: " + acctNum +
    "\nBalance: " + balance;
  }

  private static int numAccounts()
  {
    numAccounts++;
    return numAccounts;
  }

  public static int getnumAccounts()
  {
    return numAccounts;
  }
  }]


program:
CODE

//***********************************************************
// TestAccounts1
// A simple program to test the numAccts method of the
// Account class.  
//***********************************************************
import java.util.Scanner;

public class TestAccounts1
{
    public static void main(String[] args)
    {
    Account testAcct;

    Scanner scan = new Scanner(System.in);

    System.out.println("How many accounts would you like to create?");
    int num = scan.nextInt();

    for (int i=1; i<=num; i++)
        {
        testAcct = new Account(100, "Name", i);
        System.out.println("\nCreated account " + testAcct);
        System.out.println("Now there are " + Account.getnumAccounts() +
                   " accounts");
        }
    }
}

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 07:33PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month