0

The program I need is to create a Test Account program with an account class. Here is what I have come up with:

package accounts;

import java.util.Date;

public class TestAccount {


public static void main(String[] args) {
// TODO Auto-generated method stub
java.util.Date date = new java.util.Date();



Account firstaccount = new Account (1111, 10000.00, 0.045);

System.out.println("Transaction started: " + date.toString());

System.out.println("User's ID is: " + firstaccount.getId());

System.out.println("User's balance is: " + firstaccount.getBalance());

System.out.println("The montlhly interest rate is: " + firstaccount.getMonthlyInterestRate());

System.out.println("Balance after withdraw is: " + firstaccount.withdraw(1000));

System.out.println("Balance after deposit is: " + firstaccount.deposit(3000));

System.out.println("Transaction complete."); }


class Account {



private int id = 0;

private double balance = 0.0;

private double annualInterestRate = 0.0; 


public Account (int newId, double newBalance, double newAnnualInterestRate) {

id = newId;

balance = newBalance;

annualInterestRate = newAnnualInterestRate;
}

public int getId() {

return id;
}

public double getBalance () {

return balance;
}

public double getAnnualInterestRate () {

return annualInterestRate;
}

public double getMonthlyInterestRate () {

return annualInterestRate/12;
}

public double withdraw (double value) {

return balance -= value;
}

public double deposit (double value) {

return balance += value;
}

public void setId (int newId) {

id = newId;
}

public void setBalance (double newBalance) {

balance = newBalance;
}

public void setAnnualInterestRate (double newAnnualInterestRate) {

annualInterestRate = newAnnualInterestRate;
}

} 

}

Here is the error I am getting:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: No enclosing instance of type TestAccount is accessible. Must qualify the allocation with an enclosing instance of type TestAccount (e.g. x.new A() where x is an instance of TestAccount).

at accounts.TestAccount.main(TestAccount.java:12)

Please, help me to understand what I need to do. Thank you.

alex.feigin
  • 386
  • 1
  • 11
Mokonalove
  • 19
  • 3

1 Answers1

1

You have a nested class. In java when a non static class is nested inside a non static class the new statement needs to be like the error message says:

TestAccount ta = new TestAccount();
TestAccount.Account ta_a = ta.new Account();

the syntax is ugly buy then again - so are nested classes.

See documentation for more info.

Hope this helps.

alex.feigin
  • 386
  • 1
  • 11