That is quite vague. I you really want an answer to that, then
(1) import the java.util.List and java.util.ArrayList classes
(2) define your field 'private List<Ingredient> preferences;'
(3) create the preferences List object 'preferences = new ArrayList<Ingredients>();' and
(4) add Ingredient objects to the list -- 'preferences.add(new Ingredient(...));'
My game is a diner dasher game where a customer orders and you have to catch the right ingredients.
How do you make the computer check if the burger is what the customer ordered
Have the Ingredient class get (by way of the constructor call), use for image filename (within the constructor), and saved in a field (by the constructor) for returning (using getter method) the string that represents which ingredient each object created from the class is. Then outside the Ingredient class, have two lists of strings, one for what was ordered and one for what was caught, and compare the size and contents of the sorted lists for success.
It would be similar to how the keys get their images in the Piano scenario (as far as getting the String and saving it) and similar to how a value is returned from a Counter class (as far as a getter method -- except it will return a String value instead of an int value).
public void setRandomPref()
{
prefs = new ArrayList<Ingredient>();
int amt = (int)(Math.random()*3) + 4;
for(int i =0; i < amt; i++)
{
prefs.add(new Mustard());
prefs.add(new Lettuce());
prefs.add(new Tomato());
prefs.add(new TopBun());
prefs.add(new Onions());
prefs.add(new Patty());
prefs.add(new Ketchup());
}
}
public void setOrder()
{
for(Ingredient i : prefs)
{
order += i + ", ";
}
}
public void makeOrder()
{
GreenfootImage o = new GreenfootImage(WIDTH,HEIGHT);
o.setColor(bckg);
o.fill();
o.setColor(ink);
Font f = o.getFont();
f = f.deriveFont(FONT_SIZE);
o.setFont(f);
String s = order + " ";
o.drawString(s,10,30);
setImage(o);
}
I created this code, but why is it not creating a random customer preference
First, your 'for' loop will add all seven items to the list repeatedly from between 4 to 6 times (meaning the list will be either 28, 35 or 42 in size with each ingredient in the list 4, 5 or 6 times). I really do not think that is what you wanted to do there. You should probably start with an ingredients list (with only one occurrence of each in the list), then get a random number (between 4 and 6 if desired) for the number of order prefs. Then you can use a 'for' loop to iterate that number of times to build a new prefs list by pulling a random selection from the ingredients list and adding it to the prefs list.
Because you only show bits and pieces of the class, I cannot say much else about the way the class is coded. Except for that it looks like your 'setOrder' method will not do what may think it will. The way it is coded, it will create (presuming that 'order' is a declared String field since you are assigning a String value to it) memory address references (unless you wrote some 'toString' methods in your Ingredient subclasses).
public void stack()
{
Actor a = getOneIntersectingObject(Ingredient.class);
if ((a != null) )
{
System.out.println(((Ingredient)a).name);
//getWorld().addObject(a, this.getX()-50, this.getY());
//if (stackW != 1)
Ingredient x = new Ingredient(((Ingredient)a).name);
getWorld().removeObject(a);
getWorld().addObject(new Ingredient("Tomato"),50,50);
{
getWorld().addObject(x, this.getX()-50, this.getY()+10);
getWorld().removeObject(a);
stackW=1;
System.out.println(stackW);
}
}
I wrote this code to do that, but all this code does is add the Ingredient to the Chef. Then if an ingredient fall on top another ingredient, then it switches with the ingredient that is already there. How would I fix this