简体   繁体   中英

How to use arraylist in different methods but the methods are inside of the class

I want to use the Arraylist in different method to add into another Arraylist just like ordering but the error is always cannot find symbol for calling Arraylist in different methods.

import java.util.*;

class SelectMenu{

String prod;
double cost;

This is for Selecting Utilities:

public void selectMenu(){

    int utility;    
    Scanner in = new Scanner(System.in);

    System.out.printf("\n\n[1]Ordering\n[2]Add Item\n[3]Remove Item\n[4]Show Orders\n[5]Print Receipt");
    do{System.out.printf("\n\nChoose Utility:");
    utility = in.nextInt();
    }while(utility < 1 || utility > 5);

    switch(utility){
        case 1:
            System.out.println("\t\t\t------------------------HOTDOGGU HOUSE--------------------------");
            displayMenu();
            getOrder();
            selectMenu();
        break;
        case 2:
            \\adding Item
        break;
        case 3:
            \\Remove Item
        break;
        case 4:
            displayOrders();
        break;
        case 5:
            \\Printing Receipt
        break;
    }

}

This is for Displaying Menu:

public void displayMenu(){
        ArrayList<String> menu1 = new ArrayList<String>();
        String[] menu_name = new String[6];
        menu1.add("");
        menu1.add("Hotdog");
        menu1.add("Cheesedog");
        menu1.add("Corndog");
        menu1.add("Sausage");
        menu1.add("Chickendog");

        ArrayList<Double> menu2 = new ArrayList<Double>();
        Double[] menu_price = new Double[6];

        menu2.add(0.00);
        menu2.add(25.50);
        menu2.add(30.45);
        menu2.add(20.15);
        menu2.add(50.25);
        menu2.add(35.75);

        System.out.println("\t\t\tOrder No."+"\t\tProduct"+"\t\t\t    Price");

    for (int i = 1; 1 < menu1.size() && i < menu2.size(); i++){
        System.out.printf("\n\t\t\t  ["+i+"]"+"\t\t  %s\t\t\t"+"  %2.2f PHP",menu1.get(i), menu2.get(i));
    }
}

This is for Displaying Orders:

public void displayOrders(){

    ArrayList<String> product = new ArrayList<String>();
    for (int i = 1; i < product.size(); i++){
        product.add(prod);
    }

    ArrayList<Double> price = new ArrayList<Double>();
    for (int i = 1; i < price.size(); i++) {
        price.add(cost);
    }

    System.out.println("\t\t\tOrder No."+"\t\tProduct"+"\t\t\t    Price");

    for (int i = 1; i < product.size() && i < price.size(); i++){
        System.out.printf("\n\t\t\t  ["+i+"]"+"\t\t  %s\t\t\t"+"  %2.2f PHP",product.get(i), price.get(i));
     }
}

This is for getting orders:

public void getOrder(){
    int order;

    Scanner g1 = new Scanner(System.in);

    do{System.out.printf("\nWhat is your Order?:");
    order = g1.nextInt();
    }while(order < 1 || order > 6);

    switch(order){
        case 1:
            prod = menu_name.get(order); \\Error cannot find symbol
            cost = menu_price.get(order); \\Error cannot find symbol
        break;
    }
}

}

You have two options:

  1. Make it a field variable, or
  2. Pass it in as a parameter to every method that uses the ArrayList.

Of the two, I would definitely recommend that you use option 1 because it is much easier. All that you have to do is this:

class SelectMenu {
    String prod;
    double cost; //these two field variables that you already have
    ArrayList<String> menu1; //declare it as a field variable.

/*
    All of your other methods here
*/
}

When you do this, you don't need to worry about anything else, but when you initialize it (ie give it a value) inside method selectMenu() , make sure you do this: menu1 = new ArrayList<String>(); , and not this: ArrayList<String> menu1 = new ArrayList<String>(); .

Because you already have two field variables in your program, I doubt that you would need help understand how these work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM