PDA

Click to See Complete Forum and Search --> : Account.java trouble


cuzloc
Nov 19th, 2006, 02:51 PM
//*******************************************************
// 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

CornedBee
Nov 19th, 2006, 04:26 PM
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.