I want to make the program choose from an ArrayList based on my Scanner Input. Like, I write breakfast and than sweet, and it has to randomize the list breakfastSweet and print me the randomized index.
I am still learning Java, I am just playing around and trying to code little projects to train it.
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// program begins here, you get asked what kind of lunch you want to eat
// after asking for the meal type and answering it, it goes to the next question
System.out.println("Hi, welcome to Recipe-Randomizer! What kind of meal do you want, breakfast, lunch or maybe dinner?");
System.out.print("Type one of the give choices now: ");
String mealType = scanner.nextLine();
System.out.print("So you want to eat for " + mealType + ". Do you want to eat some sweet or savory " + mealType + "?\nType in one of the given choices: ");
String flavor = scanner.nextLine();
System.out.println("A " + flavor + " " + mealType + "? Well, let's see what we have here.\nI am going to pick a random recipe.\nPlease wait...");
// list of meals, list name describes
ArrayList<String> breakfastSweet = new ArrayList();
ArrayList<String> breakfastSavory = new ArrayList();
ArrayList<String> lunchSweet = new ArrayList();
ArrayList<String> lunchSavory = new ArrayList();
ArrayList<String> dinnerSweet = new ArrayList();
ArrayList<String> dinnerSavory = new ArrayList();
GetRandomFromList.outputMeal(mealType, flavor, dinnerSavory); // doesn't make sense to put the list already in, I want it to automatically select the right list.
}
}
And here is the class I have already written:
import java.util.ArrayList;
import java.util.Random;
public class GetRandomFromList {
private static String randomList(ArrayList<String> list) {
Random rand = new Random();
return list.get(rand.nextInt(list.size()));
}
public static void outputMeal(String mealType, String flavor, ArrayList<String> list){ // the list should be chosen automatically, so my code doesn't work as I want it to work
if (mealType.equals("breakfast") && flavor.equals("sweet")){
System.out.println("What about " + GetRandomFromList.randomList() + "?");
}
}
}
Can I somehow store a list in a variable, maybe like this:
if (mealType.equals("breakfast") && flavor.equals("sweet")){
// here make a variable of the breakfastSweet list
}
I know it's hard to understand me, but English isn't my main language, hope its understandble.
Aucun commentaire:
Enregistrer un commentaire