Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- ArrayList<String> list = new ArrayList<>();
- ArrayList<Lista> recipes = new ArrayList<>();
- ArrayList<String> ingredientes = new ArrayList<>();
- System.out.println("File to read:");
- String fileName = scan.nextLine();
- String command = "";
- String findName = "";
- String findIngredients = "";
- int findCookingTime = 0;
- try (Scanner reader = new Scanner(Paths.get(fileName))) {
- while (reader.hasNextLine()) {
- String name = reader.nextLine();
- int cookingTime = Integer.valueOf(reader.nextLine());
- while (true) {
- String ingredient = reader.nextLine();
- if (ingredient.isEmpty()) {
- break;
- }
- ingredientes.add(ingredient);
- }
- recipes.add(new Lista(name, cookingTime, ingredientes));
- }
- } catch (Exception e) {
- System.out.println("Error: " + e.getMessage());
- }
- System.out.println("Commands:");
- System.out.println("list - list the recipes");
- System.out.println("stop - stop the program");
- System.out.println("find name - searches recipes by name");
- System.out.println("find cooking time - searches recipes by cooking time");
- System.out.println("find ingredient - searches recipes by ingredient");
- while (true) {
- System.out.println("Enter command");
- command = scan.nextLine();
- if (command.equals("find name")) {
- System.out.println("Searched word:");
- findName = scan.nextLine();
- for (Lista model : recipes) {
- if (model.getName().contains(findName)) {
- System.out.println("Recipes:");
- System.out.println(model.getName() + ", cooking time: " + model.getDuration());
- }
- }
- }
- if (command.equals("find cooking time")) {
- System.out.println("Max cooking time:");
- findCookingTime = Integer.valueOf(scan.nextLine());
- for (Lista model : recipes) {
- if (model.getDuration() <= findCookingTime) {
- System.out.println("Recipes:");
- System.out.println(model.getName() + ", cooking time: " + model.getDuration());
- }
- }
- }
- if (command.equals("find ingredient")) {
- for (Lista model : recipes) {
- System.out.println(model);
- }
- }
- if (command.equals("list")) {
- System.out.println(recipes);
- }
- if (command.equals("stop")) {
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement