-1

I'm writing a banking program in Java, and am having some trouble getting a couple of my methods to be called correctly. When I try to compile my files, these are the errors I get:

MustangBanking.java:74: error: cannot find symbol
            displayAccounts();
            ^
  symbol:   method displayAccounts()
  location: class MustangBanking

MustangBanking.java:75: error: cannot find symbol
            deleteAccount();
            ^
  symbol:   method deleteAccount()
  location: class MustangBanking
2 errors

The two relevant files (only pasting relevant portions here) in my package are MustangBanking.java:

public class MustangBanking {

public static void main(String[] args) {

    //Declare and initialize data fields
//irrelevant code
case 3:
    displayAccounts();
    deleteAccount();
    break;
//more irrelevant code

and Account.java

public class Account {

//Declare and initialize data fields
//irrelevant code
    public void deleteAccount() {
        //method code
    }
    public void displayAccounts() {
        //method code
    }
//irrelevant code

I read elsewhere that my issue is that the two methods in question should be defined in the MustangBanking class and not the Account class, and that they should be defined outside of the main method. But when I do that, I get errors that all of my variables, etc cannot be found. What am I forgetting here? If you need more code/clarification, please let me know and I'll post it.

Thank you!

edit: MustangBanking class

import java.util.*;
import java.io.*;

//MustangBanking class
public class MustangBanking {

public static void main(String[] args) {

    //Declare and initialize data fields
    int id = 1000;
    double depositAmount = 0.0;
    double withdrawAmount = 0.0;
    double checkAmount = 0.0;
    double balance = 0.0;
    double annualInterestRate = 0.0;
    boolean run = true;
    int option;
    Scanner in = new Scanner(System.in);
    Account account = new Account();

    //Create ArrayList of type Account to store all Account objects
    ArrayList<Account> accounts = new ArrayList<>();

    //Loop to run the program inside of
    while (run) {

        //Display the menu
        System.out.println("\nMUSTANG BANKING MENU");
        System.out.println("\n1 -- Create a new checking account");
        System.out.println("2 -- Create a new savings account");
        System.out.println("3 -- Delete an existing account");
        System.out.println("4 -- View a specific account");
        System.out.println("5 -- View all accounts");
        System.out.println("6 -- Write checks for a checking account");
        System.out.println("7 -- Deposit funds into an account");
        System.out.println("8 -- Withdraw funds from an account");
        System.out.println("9 -- Find account with the largest balance");
        System.out.println("10 -- Exit");
        System.out.println("\nEnter Option:");
        option = in.nextInt();

        //Switch statement to direct program based on option entered by user
        switch (option) {

            //create a new checking account
            case 1:
            CheckingAccount c1 = new CheckingAccount(id, balance, annualInterestRate); //Create a new CheckingAccount object
            id++; //increment id by 1
            accounts.add(c1); //add the new CheckingAccount to the Arraylist accounts
            break;

            //create a new savings account
            case 2:
            SavingsAccount s1 = new SavingsAccount(id, balance, annualInterestRate); //create a new SavingsAccount object
            id++; //increment id by 1
            accounts.add(s1); //add the new SavingsAccount to the ArrayList accounts
            break;

            //delete an existing account
            case 3:
            //c1.displayAccounts();
            //c1.deleteAccount();
            break;

- Account class

import java.util.*;
import java.io.*;

//Account class
public class Account {

//Declare and initialize data fields
protected int id = 1000;
protected double balance = 0.0;
protected double annualInterestRate = 0.0;
protected double monthlyInterestRate;
protected Date dateCreated;
protected double depositAmount = 0.0;
protected int pendingChecks = 0;

Scanner in = new Scanner(System.in);

//Create ArrayList of type Account to store all Account objects
ArrayList<Account> accounts = new ArrayList<>(); //do i need to create the arraylist in every class?

//Delete account
public void deleteAccount() {
    System.out.println("\nPlease enter the ID of the account you wish to delete:");
    id = in.nextInt(); //take user input of id to delete
    accounts.remove(id); //remove the account
}

//Display all accounts
public void displayAccounts() {
    System.out.println("\nAvailable accounts:\n");
    for (int i = 0; i < accounts.size(); i++) {
        System.out.println(accounts.get(i).getId()); //print the id instead of the index
    }
}

//Display one account
public void displayAccount() {
    //Prompt user for the account they want to view
    System.out.println("\nPlease enter the ID of the account you would like to view:");
    id = in.nextInt();

    //Print the account information
    for (int i = 0; i < accounts.size(); i++) {
        if (accounts.get(i).getId() == id) {
            //if savings account
            if (accounts.get(i) instanceof SavingsAccount) { 
                System.out.println("Account type: Savings");
            }
            //if checking account
            else if (accounts.get(i) instanceof CheckingAccount) {
                System.out.println("Account type: Checking");
            }
        }
    }
    System.out.println("Account: " + id); //Print ID
    System.out.println("Balance: " + balance); //Print balance
    System.out.println("Date created: " + dateCreated); //Print date created
    System.out.println("Annual interest rate: " + annualInterestRate + "%"); //Print annual interest rate   
    //if checking account, print number of pending checks
    for (int i = 0; i < accounts.size(); i++) {
        if (accounts.get(i).getId() == id) {
            if (accounts.get(i) instanceof CheckingAccount) {
                System.out.println("Number of pending checks: " + pendingChecks);
            }
        }
    } 
}
njwoodard
  • 37
  • 1
  • 5

2 Answers2

0

You are calling Methods of Account class in MustangBanking class without using the object of Account class. This is causing the issue.

Try to change the below main method code:

public static void main(String[] args) {
    Account account = new Account();
    //Declare and initialize data fields
    //irrelevant code

    case 3:
        account.displayAccounts();
        account.deleteAccount();
        break;
    //more irrelevant code

Account Class add the below method to Account class

public void add(Account a){
    accounts.add(a)
}

call this method from all the switch cases:

    //Switch statement to direct program based on option entered by user
    switch (option) {

        //create a new checking account
        case 1:
        CheckingAccount c1 = new CheckingAccount(id, balance, annualInterestRate); //Create a new CheckingAccount object
        id++; //increment id by 1
        account.add(c1); //add the new CheckingAccount to the Arraylist accounts
        break;

        //create a new savings account
        case 2:
        SavingsAccount s1 = new SavingsAccount(id, balance, annualInterestRate); //create a new SavingsAccount object
        id++; //increment id by 1
        account.add(s1); //add the new SavingsAccount to the ArrayList accounts
        break;

        //delete an existing account
        case 3:
        //c1.displayAccounts();
        //c1.deleteAccount();
        break;

Delete Method

//Delete account
public void deleteAccount() {
     System.out.println("\nPlease enter the ID of the account you wish to    delete:");
     id = in.nextInt(); //take user input of id to delete
     Iterator<Account> iAccount = accounts.iterator();
     while(iAccount.hasNext()){
          Account account = iAccount.next();
          if(account.getId()==id){
                accounts.remove(account);
          }
     }
 }
Vivek Singh
  • 2,047
  • 11
  • 24
  • Assuming you meant "Account.displayAccounts();" etc, that then gives me the error that "non-static method displayAccounts() cannot be referenced from a static context". Can't recall the fix for this off the top of my head. – njwoodard Dec 04 '15 at 05:15
  • You need to give object Name `account.displayAccounts();`. What you are using is directly the class name – Vivek Singh Dec 04 '15 at 05:16
  • In that case I just get another cannot find symbol error, but this time it's seeing account as a variable and saying that it cannot find that. Should I just declare a random account variable somewhere? Or should I be using something else that is in my code somewhere. – njwoodard Dec 04 '15 at 05:21
  • did you add this line `Account account = new Account();` as mentioned in my answer – Vivek Singh Dec 04 '15 at 05:22
  • I have similar code earlier in a different case-- `CheckingAccount c1 = new CheckingAccount(id, balance, annualInterestRate);` and `SavingsAccount s1 = new SavingsAccount(id, balance, annualInterestRate);` (CheckingAccount and SavingsAccount both extend Account). Do those count, per se? If so how would I use that syntax to solve the issue? – njwoodard Dec 04 '15 at 05:24
  • Whichever instance you need From `CheckingAccount` or `SavingsAccount` you can use it for calling the methods. But you need to add the object first as mentioned in you comment and use the object for calling the methods – Vivek Singh Dec 04 '15 at 05:27
  • Did it solved yous issue? – Vivek Singh Dec 04 '15 at 05:51
  • I need both instances, but they both extend Account. Working with the code you gave me gets rid of errors, but I'm running into separate problems while the code is running. Not sure if it has to do with the code you gave me or something else in my own code. – njwoodard Dec 04 '15 at 05:58
  • Can you tell me that what you actually want to achieve through this code. Which account you want to delete and which account you need to display. – Vivek Singh Dec 04 '15 at 06:18
  • `displayAccounts` method loops over an ArrayList of Account objects and displays the int id variable associated with them. in the `deleteAccount` method, the user enters the id of the account object they want to delete, and then the id is removed from the ArrayList. Currently the issue is that `displayAccounts` will not display anything when I call it, and I'm not sure if it's an issue with how I call the method per your solution, or if it is a different issue. – njwoodard Dec 04 '15 at 06:22
  • `displayAccounts()` does not show any accounts as you dont have any linked to `Account` class. *You need to mantain a list of Accounts (`CheckingAccount` or `SavingsAccount`) whichever you are creating in the Account class*. while deleting as well you can delete the same from the list using its id – Vivek Singh Dec 04 '15 at 06:25
  • How is it not linked if it is defined in the Account class? There are two other classes that I have--CheckingAccount and SavingsAccount that both extend Account. When I create instances of these they should be getting indexed in the ArrayList, but I do not know why it doesn't come up when I call the method. – njwoodard Dec 04 '15 at 06:31
  • Can you show how and where you maintaining the list and How Account object is initiated. – Vivek Singh Dec 04 '15 at 06:52
  • I edited the original question with the relevant parts of both classes. Sorry if it is a lot to sift through. – njwoodard Dec 04 '15 at 06:58
  • I have updated my answer with the things to do, Make this changes in your class – Vivek Singh Dec 04 '15 at 07:06
  • That worked very well, thank you. Now only issue is that when I enter an ID to delete, I get an IndexOutOfBounds exception. I assume this is because the `deleteAccount()` method references the ArrayList, which we removed references to in cases 1 and 2? – njwoodard Dec 04 '15 at 07:20
  • No the issue is that delete function should check which id to remove, the id is not same as that of the index position – Vivek Singh Dec 04 '15 at 08:11
  • I have added the method which should be used for deleting the account – Vivek Singh Dec 04 '15 at 08:16
  • Also can you mark this as an answer if this solves your issue. – Vivek Singh Dec 04 '15 at 08:22
  • Only error now is `Account.java:41: error: method getId in class Account cannot be applied to given types; if(account.getId(id)){ ^ required: no arguments found: int reason: actual and formal argument lists differ in length` – njwoodard Dec 04 '15 at 18:13
  • Update the deleteAccount() method, try the new one – Vivek Singh Dec 04 '15 at 19:56
  • That fixed it, thank you for all of the help. Hopefully I can build the rest off of this. – njwoodard Dec 04 '15 at 20:16
0

you are calling displayAccounts() and deleteAccount() without creating object of Account class and so you are getting the error

There are 2 ways to call the method with using object.

  1. if the methods are in the same class
  2. If the there is inheritance(MustangBanking extends Account )

If you dont have any other methods in Account class then you can directly extend Account

other wise create an object of Account in MustangBanking class and then call the above methods as below

Account account =new Account ();
account.displayAccounts();
account.deleteAccount();
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
  • I've tried putting the methods into the same class, but when I put them in the MustangBanking class outside of the main method, I then get errors that my variables cannot be found (which makes sense, since the variables' scope is only inside of the main method). – njwoodard Dec 04 '15 at 05:16
  • @njwoodard Please elaborate as I did not understand your comment – SpringLearner Dec 04 '15 at 05:18
  • When I tried fixing my code like you said to put the methods in the MustangBanking class, when I compile I get cannot find symbol errors for all of my variables that I use in the methods. – njwoodard Dec 04 '15 at 05:22
  • @njwoodard your main method is static and if you have just copied the method from MustangBanking to account then I hope you are not making the methods as static. So you are getting the error. So please make the methods static – SpringLearner Dec 04 '15 at 05:24