Search code examples
javamethodscannot-find-symbol

Cannot find symbol method - Where should I define my methods?


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);
            }
        }
    } 
}

Solution

  • 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);
              }
         }
     }