Hi,
I've been working on this for two days. I need to pass an arraylist to another class. It gets the information, and will print out the menu, until the program goes back to the initial class, then the list empties. I don't get it, and I'm so confused.
public class Party
{
private String inputLine;
public Party()
{
reader = new Scanner(System.in);
}
public void start()
{
System.out.println("welcome. please enter food.");
enterfood();
}
public void enterfood() {
Food foodObject = new Food();
foodObject.printA();
printMenu();
}
public void printMenu()
{
System.out.println("Hello your menu will be here eventually.");
Food foodObject = new Food();
foodObject.printC();
}
Here is the second class
public class Food
{
/**
*instance variables
*///
private Scanner reader;
ArrayList<String> eat = new ArrayList<String>();
/**
* Constructor for objects of class Calculator
* Create the ArrayList object.
* Create the Scanner input object.
*/
public Food()
{
reader = new Scanner(System.in);
eat = new ArrayList<String>();
}
public void foodList()
{
System.out.println("Choose an action: (A) add food or (C) print list.");
String inputLine = reader.nextLine();
if(inputLine.equalsIgnoreCase("A")){
printA();
}
if(inputLine.equalsIgnoreCase("C")){
printC();
}
}
public void printA()
{
Scanner reader = new Scanner(System.in);
System.out.println("enter food: ");
String n = reader.nextLine();
eat.add(n);
foodList();
}
public void printC()
{
System.out.println(eat);
}
public void next()
{
System.out.println("To enter more food type F.");
String str= reader.nextLine();
if(str.equalsIgnoreCase("F"))
{
foodList();
}
}
public void eat(){
System.out.println("Menu: " + eat);
}
This is my output:
welcome. please enter food.
enter food:
pizza
Choose an action: (A) add food or (C) print list.
a
enter food:
soda
Choose an action: (A) add food or (C) print list.
c
[pizza, soda]
Hello your menu will be here eventually.
[]See, the menu empties. This is supposed to be pizza, soda like above.
Any help would be greatly appreciated. Thank you!