简体   繁体   中英

How do I access an ArrayList written in one class from another class?

So I'm doing a Java project for college where I have to simulate a restaurant. The two classes that are giving me trouble are Menu and Bill. I have written four ArrayLists in Menu for all possible starters, main course, desserts and drinks. I need to be able to access these ArrayLists in Bill to generate the bill / receipt, but I'm having trouble.

The Menu class:

import Foods.Desserts;
import Foods.Drinks;
import Foods.Main;
import Foods.Starter;

import java.util.ArrayList;

public class Menu {

        /**
         * @author Max Huddlestan
         */
        //Created Array lists for each course to track the prices
        ArrayList<Starter> starters;
        ArrayList<Main> mains;
        ArrayList<Desserts> desserts;
        ArrayList<Drinks> drinks;

        //this constructor should make it so each class had each of these array lists

    public Menu(){
        addStarters();
        addMain();
        addDesserts();
        addDrinks();
    }

        //using an object of each class i can add a name and a price to the food in its respective course
        public void addStarters(){
            starters = new ArrayList<Starter>();
            starters.add(new Starter("Soup", 8.00));
            starters.add(new Starter("Garlic Bread", 8.00));
            starters.add(new Starter("Chicken Wings", 9.00));
            starters.add(new Starter("Caesar Salad", 10));
            starters.add(new Starter("N/A", 0));
        }

        public void addMain(){
            mains = new ArrayList<Main>();
            mains.add(new Main ("Beef Burger", 16.5));
            mains.add(new Main("Steak", 18.50));
            mains.add(new Main("Spaghetti Bolognese", 14.00));
            mains.add(new Main("Pizza", 14.75));
            mains.add(new Main("Vegan Lasagne", 15.30));
            mains.add(new Main("N/A", 0));
        }

        public void addDesserts(){
            desserts = new ArrayList<Desserts>();
            desserts.add(new Desserts("Sticky Toffee Pudding", 7.5));
            desserts.add(new Desserts("Vegan Brownie", 7.5));
            desserts.add(new Desserts("Ice Cream Sundae", 7.5));
            desserts.add(new Desserts("Apple Tart", 7.5));
            desserts.add(new Desserts("N/A", 0));
        }

        public void addDrinks() {
            drinks = new ArrayList<Drinks>();
            drinks.add(new Drinks("Beer", 5.3));
            drinks.add(new Drinks("Wine", 7.0));
            drinks.add(new Drinks("Coca Cola", 3.30));
            drinks.add(new Drinks("Fanta", 3.30));
            drinks.add(new Drinks("Water", 0));
            drinks.add(new Drinks("N/A", 0));
        }

            public ArrayList<Starter> getStarters() {return starters;}
    public ArrayList<Main> getMains() {return mains;}

    public ArrayList<Desserts> getDesserts() {return desserts;}
    public ArrayList<Drinks> getDrinks() {return drinks;}



    @Override
    public String toString() {
        String startersList = "+";
            for (Starter s : starters) {
                startersList += s.toString();
            }
            return startersList;
    }

}

The Bill class:

package BillsIncome;

import Foods.Desserts;
import Foods.Drinks;
import Foods.Main;
import Foods.Starter;

import java.awt.*;
import java.util.ArrayList;

public class Bill {
    public static void main(String[] args) {
        Menu menu = new Menu();
        TakeOrder orders = new TakeOrder();
        ArrayList<String> order = new ArrayList<String>();
        order.add(orders.selectStarter());
        order.add(orders.selectMain());
        order.add(orders.selectDessert());
        order.add(orders.selectDrink());
        System.out.println(menu.getStarters());
    }
}

The line

System.out.println(menu.getStarters());

Is giving me an error: "Cannot resolve method getStarters in Menu"

Help?? :P

Basically, like @tgdavies said, your problem is that your program assumes the Menu object you're trying to create is from java.awt.Menu .

To get your main method to work, all you'll have to do is create a package for your Menu.java , with something like this as the header:

package Restaurant;
import java.util.ArrayList;

public class Menu {

    /**
     * @author Max Huddlestan
     */
    // ... All other class stuff below
}

Now your Menu class will be associated with the Restaurant package, which you can import in your Bill program by doing import Restaurant.Menu; at the top of the program.

If you don't want to have to import your Menu class in the Bill program, you need to make sure the two files are in the same directory.

I hope this helps clarify things!

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