Results 1 to 2 of 2

Thread: Account.java trouble

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    1

    Account.java trouble

    //*******************************************************
    // 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 double balance;
    private String name;
    private long acctNum;
    private static int numAccounts;
    private double newAccount;
    //----------------------------------------------
    //Constructor -- initializes balance, owner, and account number
    //----------------------------------------------
    public Account(double initBal, String owner, long number)
    {
    balance = initBal;
    name = owner;
    acctNum = number;
    numAccounts++;

    }

    //----------------------------------------------
    // 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 double getAcctNumber()
    {
    return acctNum;
    }

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

    //----------------------------------------------
    // Returns the number of accounts.
    //----------------------------------------------
    public static int getNumAccounts()
    {
    return numAccounts;
    }


    public void close()
    {
    name = "CLOSED";
    balance = 0;
    numAccounts--;
    }

    //----------------------------------------------
    // Consolidates two accounts into one account.
    //----------------------------------------------
    public static Account AccountConsolidate(Account acct1, Account acct2)
    {
    newAccount = acct1.getBalance() + acct2.getBalance();
    newAccount = newAccount.getAcctNum();
    close acct2;

    String name1 = acct1.getName();
    String name2 = acct2.getName();
    if(name1.equals(name2));
    newAccount = newAccount.getAcctNum();
    close acct2;
    return newAccount;
    }

    }




    ERRORS:
    ----jGRASP exec: javac -g C:\Documents and Settings\carlo\Desktop\Account.java

    Account.java:95: non-static variable newAccount cannot be referenced from a static context
    newAccount = acct1.getBalance() + acct2.getBalance();
    ^
    Account.java:96: non-static variable newAccount cannot be referenced from a static context
    newAccount = newAccount.getAcctNum();
    ^
    Account.java:96: non-static variable newAccount cannot be referenced from a static context
    newAccount = newAccount.getAcctNum();
    ^
    Account.java:96: double cannot be dereferenced
    newAccount = newAccount.getAcctNum();
    ^
    Account.java:97: cannot find symbol
    symbol : class close
    location: class Account
    close acct2;
    ^
    Account.java:97: acct2 is already defined in AccountConsolidate(Account,Account)
    close acct2;
    ^
    Account.java:99: cannot find symbol
    symbol : method getName()
    location: class Account
    String name1 = acct1.getName();
    ^
    Account.java:102: non-static variable newAccount cannot be referenced from a static context
    newAccount = newAccount.getAcctNum();
    ^
    Account.java:102: non-static variable newAccount cannot be referenced from a static context
    newAccount = newAccount.getAcctNum();
    ^
    Account.java:102: double cannot be dereferenced
    newAccount = newAccount.getAcctNum();
    ^
    Account.java:103: cannot find symbol
    symbol : class close
    location: class Account
    close acct2;
    ^
    Account.java:104: non-static variable newAccount cannot be referenced from a static context
    return newAccount;
    ^
    12 errors



    Thank you

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Account.java trouble

    Err, haven't you just posted the same thread a few days ago?

    It's all about static and non-static stuff. Look it up in your favourite Java reference.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width