EverBloom
EverBloom

Reputation: 21

Calling an arraylist from another class

here is my code for the arraylist class; package Restaurant;

 import java.util.ArrayList;



    public class Controlclass {

        public static void main(String[] args) {
            // Create ArrayList for foodItems subclasses
            ArrayList<Mains> MainsList = new ArrayList<Mains>();
    //add items to the MainsList
            Mains  CurriedFriedRice = new Mains (" Curried fried rice ", 4.00, " Yummy curried fried rice " , 500);
            Mains  LambChopsWithCarrotPuree = new Mains (" Lamb chops with carrot puree ", 6.00, " Delicious Lamb chops with carrot puree " , 580);
            Mains  PadThaiNoodles = new Mains (" Pad Thai noodles ", 5.00, " Amazing thai noodles " , 480);
            Mains  SalmonAndAsparagusFajitas = new Mains (" Salmon and asparagus fajitas ", 4.90, " Fajita you will always come back for " , 450);


    MainsList.add(CurriedFriedRice);
            MainsList.add(LambChopsWithCarrotPuree);
            MainsList.add(PadThaiNoodles);
            MainsList.add(SalmonAndAsparagusFajitas);


    for (Mains mains: MainsList){
                System.out.println(mains);
            }

and this is the GUI class i need to use these arraylists in;

*

public class TheFoodLoversLounge {
    private final GridLayout layout; // layout of this frame
    private JFrame frame;
    private final JPanel buttonbar;
    private final JPanel combobar;
    private final JPanel receiptbar;
private final JComboBox<String> mainsc; 
public class TheFoodLoversLounge {
    private final GridLayout layout; // layout of this frame
    private JFrame frame;
    private final JPanel buttonbar;
    private final JPanel combobar;
    private final JPanel receiptbar;
    private final JComboBox<String> starters; 
    private final JComboBox<String> mainsc; 
    private final JComboBox<String> dessert; 
    private final JButton getTotalPrice;
    private final JLabel l2;
    private final JLabel l3;
    private final JLabel l4;
    private final JLabel l5;
    private final JLabel l6;
    private final JLabel l7;
    private final JLabel l8;
    private final JLabel l9;
    private final JLabel l10;
    private final JLabel r1;
    private final JLabel r2;
    private final JLabel r3;
    private final JLabel r4;
    private final JLabel r5;
    private final JLabel r6;
    private final JLabel r7;
    private final JLabel r8;
    private final JLabel r9;
    private Controlclass control;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TheFoodLoversLounge window = new TheFoodLoversLounge();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    /**
     * Create the application.
     */
    public TheFoodLoversLounge() {
        MAXTABLES=10;
        MAXDINERS=10;
        initialize();
        control = new Controlclass();
        layout = new GridLayout(0,1,0,5);
        starters=new JComboBox<>(); 
        mainsc=new JComboBox<>(); 
        dessert=new JComboBox<>(); 

        for (Starters starterI: control.StarterList){
            starters.addItem(starterI.toString());
        }
        starters.setMaximumRowCount(5);
        for (Mains mainsI: control.MainsList){
            mainsc.addItem(mainsI.toString());
        }
        mainsc.setMaximumRowCount(5);
        for (Dessert dessertI: control.DessertList){
            dessert.addItem(dessertI.toString());
        }
buttonbar= new JPanel(layout);
        buttonbar.setBackground(Color.LIGHT_GRAY);
        buttonbar.add(getTotalPrice); buttonbar.add(l10);
combobar= new JPanel(layout);
        combobar.setBackground(Color.PINK);
        combobar.add(l1); combobar.add(starters);
        combobar.add(l2); combobar.add(mainsc);
        combobar.add(l4); combobar.add(dessert);
private void initialize() {
        frame = new JFrame();
        frame.setLocationRelativeTo(null);
        //frame.setResizable(false);
        frame.setSize(768, 800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
private class TheListener implements ActionListener
    {
        // process text field events
        @Override
        public void actionPerformed(ActionEvent event)
        { 
            String string = "";
            double arr [];
            Orders ord = new Orders();
            if (event.getSource() ==getTotalPrice){
                ord.diners= Integer.parseInt((String) diners.getItemAt(diners.getSelectedIndex()));
                ord.tableno=Integer.parseInt((String) tableno.getItemAt(tableno.getSelectedIndex()));
                ord.item[0]=starters.getItemAt(starters.getSelectedIndex());
                // ord.qty[1]=
                        ord.rate[0]=control.StarterList.get(starters.getSelectedIndex()).price;
                        ord.item[1]=mainsc.getItemAt(mainsc.getSelectedIndex());
                        // ord.qty[2]=
                        ord.rate[1]=control.MainsList.get(mainsc.getSelectedIndex()).price;
                        ord.item[2]=dessert.getItemAt(dessert.getSelectedIndex());
                        // ord.qty[3]=
                        ord.rate[2]=control.DessertList.get(dessert.getSelectedIndex()).price;
}
        }}}

* Can everyone ignore the syntax errors please because its not full code and i just want to focus on the arraylist because everytime i try to compile it gives me the following error while the actual code is syntactically correct. Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problems:

StarterList cannot be resolved or is not a field
    MainsList cannot be resolved or is not a field
    DessertList cannot be resolved or is not a field
    DrinksList cannot be resolved or is not a field
    FishList cannot be resolved or is not a field
    StarterList cannot be resolved or is not a field
    MainsList cannot be resolved or is not a field
    DessertList cannot be resolved or is not a field
    FishList cannot be resolved or is not a field
    DrinksList cannot be resolved or is not a field

    at Restaurant.TheFoodLoversLounge.<init>(TheFoodLoversLounge.java:121)
    at Restaurant.TheFoodLoversLounge$1.run(TheFoodLoversLounge.java:91)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

these are the errors that i get. I dont know how to fix these. can anyone help me with them please. The reason for not putting the full code up is as its my project and i cant risk it. So a fix from you guys would be highly appreciated.

Upvotes: 0

Views: 304

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347194

Controlclass doesn't provide any kind of access to any of the ArrayLists you defined within the main method (and there is a huge hint to your problem).

Start by defining instance fields in your Controlclass and the provide public getters to allow them access to those fields (through a method you can control - for example, you could return immutable lists or copy the list before returning it)

import java.util.ArrayList;
import java.util.List;

public class Controlclass {

        private List<...> starterList;
        private List<...> mainsList;
        private List<...> dessertList;
        private List<...> drinksList;
        private List<...> fishList;

    public static void main(String[] args) {
        Controlclass controlClass = new Controlclass();
    }

    public Controlclass() {
        starterList = new ArrayList<>(25);
        mainsList = new ArrayList<>(25);
        dessertList = new ArrayList<>(25);
        drinksList = new ArrayList<>(25);
        fishList = new ArrayList<>(25);

        // Populate the values...
    }

    public List<...> getStarterList() {
        return starterList;
    }

    public List<...> getMainsList() {
        return mainsList;
    }

    public List<...> getDessertList() {
        return dessertList;
    }

    public List<...> getDrinksList() {
        return drinksList;
    }

    public List<...> getFishList() {
        return fishList;
    }
}

(ps You will need to replace <...> with the appropriate generics, I didn't really want to dig through all your code to figure it out)

Next pass an instance of the Controlclass to your other classes

public class TheFoodLoversLounge {
    //...
    private Controlclass control;

    public TheFoodLoversLounge(Controlclass control) {
        this.control = control
        //...

Finally, make use of the properties of the Controlclass

for (Starters starterI: control.getStarterList()) {
    starters.addItem(starterI.toString());
}

(and make your mind which is you main entry point for the program, having two main methods is going to be very confusing)

Upvotes: 1

rdonuk
rdonuk

Reputation: 3956

Do not define your lists in main method. Define them as members and initialize them in constructor. Then write getter methods for each list.

public class Controlclass {
   private ArrayList<Mains> MainsList;

   public ControlClass() {
       MainsList = new ArrayList<Mains>()
   }

   public ArrayList<Mains> getMainsList() {
        return MainsList;
   }
}

Finally, use the getter methods to reach the lists.

for (Mains mainsI: control.getMainsList()){

Upvotes: 1

Related Questions