-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTTYD.java
More file actions
125 lines (117 loc) · 6.19 KB
/
TTYD.java
File metadata and controls
125 lines (117 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class TTYD {
public static void main(String[] args)throws IOException {
createRecipe forCreation = new createRecipe();
List<Recipe> recipes = new ArrayList<Recipe>();
while (true) {
//Asking user to select
System.out.println("\n \n");
System.out.println("Enter a number to select from the following Menu Options: \n");
System.out.println("*-------------------------------------------------------* \n");
System.out.println(" 1. Create recipe \n");
System.out.println(" 2. Search All Recipes \n");
System.out.println(" 3. Search Recipe By Title \n");
System.out.println(" 4. How to use? \n");
System.out.println(" 5. Exit \n");
System.out.println("*-------------------------------------------------------* \n");
//User input
Scanner askMain = new Scanner(System.in);
String userAnswer = askMain.nextLine();
if (userAnswer.equals("5")) {
break;
} else if (userAnswer.equals("1")) {
System.out.println("\n");
Recipe addrecipe = forCreation.callAll();
recipes.add(addrecipe);
}
else if(userAnswer.equals("2")){
if(recipes.isEmpty()){
System.out.println("No saved recipes found");
}
else{
for(int index = 0; index < recipes.size(); index++) {
Recipe printRecipe = recipes.get(index);
System.out.println((index+1)+ ": " + printRecipe.gettitle() + "\n");
}
System.out.println("Select a recipe number to print");
int recipenumber = askMain.nextInt();
Recipe chosenRecipe = recipes.get(recipenumber-1);
//Forking into two differnet options
System.out.println(" ");
System.out.println("Enter S: Explore Step By Step ");
System.out.println("Enter E: Display the entire recipe ");
Scanner whichInput = new Scanner(System.in);
String selected = whichInput.nextLine();
ExploreRecipe exploreRecipe = new ExploreRecipe();
exploreRecipe.explore(selected, chosenRecipe);
System.out.println("press enter to continue");
askMain.nextLine();
askMain.nextLine();
}
}
else if (userAnswer.equals("3")) {
if(recipes.isEmpty()){
System.out.println("No saved recipes found");
}
else{
System.out.println("\nInsert the recipe title");
String input = askMain.nextLine();
List<Recipe> results = search(recipes, input);
if(results.isEmpty()){
System.out.println("No saved recipes found");
}
else{
for(int index=0; index < results.size(); index++){
Recipe match = results.get(index);
System.out.println("\n"+ (index+1)+ ": " + match.gettitle());
}
System.out.println("\nSelect a recipe number to print");
int recipenumber = askMain.nextInt();
askMain.nextLine();
Recipe chosen = recipes.get(recipenumber-1);
System.out.println("\nEnter S: Explore Step By Step ");
System.out.println("Enter E: Display the entire recipe ");
String selected = askMain.nextLine();
ExploreRecipe exploreRecipe = new ExploreRecipe();
exploreRecipe.explore(selected, chosen);
System.out.println("press enter to continue");
askMain.nextLine();
askMain.nextLine();
}
}
}
else if (userAnswer.equals("4")) {
System.out.println("Hello! This section will explain the functions of this program \n \nOn our main menue you are presented two options: \n 1. To create a recipe \n 2. to Search for previously created recipes.\n \n If you choose to create a recipee you will be asekd for: \n 1. Recipe Title \n 2. Recipe Description \n 3. Ingrediens \n 4. Instrcutions\n \n The information will automatically be saved. Note: For functions that require more than one input please type 'done' when finished to move on to the next step.\n \n As for searching for a previously saved recipe, there are two options: Either browsing through all saved recipes or by searching by keyword (title). Once you choose a recipe through one of these methods you can decide to view the recipe in its entierty or to view the instructions step by step to help you while cooking.");
System.out.println("\n Press enter to continue");
askMain.nextLine();
askMain.nextLine();
} else {
System.out.println("Invalid Command, Try Again :)");
continue;
}
}
writeFile(recipes);
}
public static void writeFile(List<Recipe> recipes)throws IOException{
File recipefile = new File("recipe.txt");
FileWriter writer = new FileWriter(recipefile);
for(Recipe writeRecipe : recipes){
String text = writeRecipe.toString();
writer.write(text);
}
writer.close();
}
public static List<Recipe> search(List<Recipe> searchRecipes, String keyword){
List<Recipe> searchresults = new ArrayList<Recipe>();
for(Recipe match : searchRecipes){
if(match.gettitle().contains(keyword)){
searchresults.add(match);
}
}
//Returns an arrayList
return searchresults;
}
}