Advertisement
korobushk

Untitled

Jul 19th, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. public static void main(String[] args) {
  2. Scanner scan = new Scanner(System.in);
  3.  
  4. ArrayList<String> list = new ArrayList<>();
  5. ArrayList<Lista> recipes = new ArrayList<>();
  6. ArrayList<String> ingredientes = new ArrayList<>();
  7.  
  8. System.out.println("File to read:");
  9. String fileName = scan.nextLine();
  10. String command = "";
  11. String findName = "";
  12. String findIngredients = "";
  13.  
  14. int findCookingTime = 0;
  15. try (Scanner reader = new Scanner(Paths.get(fileName))) {
  16.  
  17. while (reader.hasNextLine()) {
  18.  
  19. String name = reader.nextLine();
  20. int cookingTime = Integer.valueOf(reader.nextLine());
  21. while (true) {
  22.  
  23. String ingredient = reader.nextLine();
  24.  
  25. if (ingredient.isEmpty()) {
  26. break;
  27. }
  28.  
  29. ingredientes.add(ingredient);
  30. }
  31. recipes.add(new Lista(name, cookingTime, ingredientes));
  32.  
  33.  
  34.  
  35. }
  36.  
  37. } catch (Exception e) {
  38. System.out.println("Error: " + e.getMessage());
  39. }
  40.  
  41. System.out.println("Commands:");
  42. System.out.println("list - list the recipes");
  43. System.out.println("stop - stop the program");
  44. System.out.println("find name - searches recipes by name");
  45. System.out.println("find cooking time - searches recipes by cooking time");
  46. System.out.println("find ingredient - searches recipes by ingredient");
  47.  
  48. while (true) {
  49.  
  50. System.out.println("Enter command");
  51. command = scan.nextLine();
  52. if (command.equals("find name")) {
  53. System.out.println("Searched word:");
  54.  
  55. findName = scan.nextLine();
  56. for (Lista model : recipes) {
  57. if (model.getName().contains(findName)) {
  58. System.out.println("Recipes:");
  59. System.out.println(model.getName() + ", cooking time: " + model.getDuration());
  60. }
  61. }
  62.  
  63. }
  64.  
  65. if (command.equals("find cooking time")) {
  66. System.out.println("Max cooking time:");
  67. findCookingTime = Integer.valueOf(scan.nextLine());
  68. for (Lista model : recipes) {
  69. if (model.getDuration() <= findCookingTime) {
  70. System.out.println("Recipes:");
  71. System.out.println(model.getName() + ", cooking time: " + model.getDuration());
  72. }
  73. }
  74. }
  75.  
  76. if (command.equals("find ingredient")) {
  77. for (Lista model : recipes) {
  78. System.out.println(model);
  79. }
  80.  
  81. }
  82. if (command.equals("list")) {
  83. System.out.println(recipes);
  84. }
  85. if (command.equals("stop")) {
  86. break;
  87.  
  88. }
  89. }
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement