Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.nio.file.Paths;
- import java.util.ArrayList;
- import java.util.Scanner;
- public class RecipeSearch {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- System.out.print("File to read: ");
- String fileName = scanner.nextLine();
- System.out.println();
- try {
- Scanner filScanner = new Scanner(Paths.get("/Users/adityak2207/Programming/tmc/vscode/mooc-java-programming-i/part07-Part07_07.RecipeSearch/" + fileName));
- int lineCount = 0;
- String name = "";
- int cookingTime = 0;
- RecipeList rList = new RecipeList();
- ArrayList<String> ingredientList = new ArrayList<>();
- while(filScanner.hasNextLine()) {
- String currLine = filScanner.nextLine();
- if (currLine.isEmpty() && lineCount != 0) {
- Recipe recipe = new Recipe(name, cookingTime, ingredientList);
- rList.add(recipe);
- ingredientList.clear();
- lineCount = 0;
- continue;
- } else {
- if (lineCount == 0) {
- lineCount++;
- name = currLine;
- } else if (lineCount == 1) {
- lineCount++;
- cookingTime = Integer.valueOf(currLine);
- } else if (lineCount == 2) {
- ingredientList.add(currLine);
- }
- }
- }
- rList.add(new Recipe(name, cookingTime, ingredientList));
- System.out.println("Commands: ");
- System.out.println("list- lists the recipes");
- System.out.println("stop - stops 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.print("\nEnter command: ");
- String cmd = scanner.nextLine();
- if (cmd.equals("list")) {
- rList.list();
- System.out.println();
- } else if (cmd.equals("find name")) {
- System.out.print("Searched Word: ");
- rList.findName(scanner.nextLine());
- } else if (cmd.equals("stop")) {
- break;
- } else if (cmd.equals("find cooking time")) {
- System.out.print("Max cooking time: ");
- rList.findCookingTime(Integer.valueOf(scanner.nextLine()));
- } else if (cmd.equals("find ingredient")) {
- System.out.print("Ingredient: ");
- rList.findIngredient(scanner.nextLine());
- }
- }
- filScanner.close();
- scanner.close();
- } catch (Exception e) {
- // TODO: handle exception
- System.out.println(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement