Advertisement
adityak2207

RecipeSearch.java

Jan 28th, 2025 (edited)
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.16 KB | Source Code | 0 0
  1. import java.nio.file.Paths;
  2. import java.util.ArrayList;
  3. import java.util.Scanner;
  4.  
  5. public class RecipeSearch {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         System.out.print("File to read: ");
  11.         String fileName = scanner.nextLine();
  12.         System.out.println();
  13.  
  14.         try {
  15.             Scanner filScanner = new Scanner(Paths.get("/Users/adityak2207/Programming/tmc/vscode/mooc-java-programming-i/part07-Part07_07.RecipeSearch/" + fileName));
  16.             int lineCount = 0;
  17.             String name = "";
  18.             int cookingTime = 0;
  19.             RecipeList rList = new RecipeList();
  20.             ArrayList<String> ingredientList = new ArrayList<>();
  21.             while(filScanner.hasNextLine()) {
  22.                 String currLine = filScanner.nextLine();
  23.  
  24.                 if (currLine.isEmpty() && lineCount != 0) {
  25.                     Recipe recipe = new Recipe(name, cookingTime, ingredientList);
  26.                     rList.add(recipe);
  27.                     ingredientList.clear();
  28.                     lineCount = 0;
  29.                     continue;
  30.                 } else {
  31.                     if (lineCount == 0) {
  32.                         lineCount++;
  33.                         name = currLine;
  34.                     } else if (lineCount == 1) {
  35.                         lineCount++;
  36.                         cookingTime = Integer.valueOf(currLine);
  37.                     } else if (lineCount == 2) {
  38.                         ingredientList.add(currLine);
  39.                     }
  40.                 }
  41.             }
  42.  
  43.             rList.add(new Recipe(name, cookingTime, ingredientList));
  44.  
  45.             System.out.println("Commands: ");
  46.             System.out.println("list- lists the recipes");
  47.             System.out.println("stop - stops the program");
  48.             System.out.println("find name - searches recipes by name");
  49.             System.out.println("find cooking time - searches recipes by cooking time");
  50.             System.out.println("find ingredient - searches recipes by ingredient");
  51.    
  52.             while(true) {
  53.                 System.out.print("\nEnter command: ");
  54.                 String cmd = scanner.nextLine();
  55.  
  56.                 if (cmd.equals("list")) {
  57.                     rList.list();
  58.                     System.out.println();
  59.                 } else if (cmd.equals("find name")) {
  60.                     System.out.print("Searched Word: ");
  61.                     rList.findName(scanner.nextLine());
  62.                 } else if (cmd.equals("stop")) {
  63.                     break;
  64.                 } else if (cmd.equals("find cooking time")) {
  65.                     System.out.print("Max cooking time: ");
  66.                     rList.findCookingTime(Integer.valueOf(scanner.nextLine()));
  67.                 } else if (cmd.equals("find ingredient")) {
  68.                     System.out.print("Ingredient: ");
  69.                     rList.findIngredient(scanner.nextLine());
  70.                 }
  71.             }
  72.  
  73.  
  74.  
  75.             filScanner.close();
  76.             scanner.close();
  77.  
  78.         } catch (Exception e) {
  79.             // TODO: handle exception
  80.             System.out.println(e);
  81.         }
  82.        
  83.     }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement