-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMenu.java
384 lines (381 loc) · 12.6 KB
/
Menu.java
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package recipeBook;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.*;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import recipeBook.Recipe;
import java.io.PrintWriter;
public class Menu{
public static void main(String[] args){
//Initialing default list to test user functions before transitioning to filewriting
List<Recipe> recipeList = new ArrayList<Recipe>();
// Read file and add to recipeList
try{
recipeList = readRecipes();
} catch (FileNotFoundException e){
System.err.println("File: 'recipes.txt' not found in current directory.");
System.exit(0);
}
boolean counter = true;
// continue prompting user
while(counter == true){
// print main menu
menuScreen();
Scanner menu = new Scanner(System.in);
int option = menu.nextInt();
// Option 1 -- add a recipe
if (option == 1) {
try{
Recipe a = addRecipe(menu);
} catch (IOException e){
e.printStackTrace();
}
// update recipeList
try{
recipeList = readRecipes();
} catch (FileNotFoundException e){
System.err.println("File: 'recipes.txt' not found in current directory.");
System.exit(0);
}
}
// Option 2 -- search and view a full singular recipe
else if (option == 2) {
searchRecipe(menu, recipeList);
}
// Option 3 -- search and view a recipe step by step
else if (option == 3) {
searchRecipeStepByStep(menu, recipeList);
}
// Option 4 -- view all recipes
else if (option == 4) {
viewAllRecipes(menu, recipeList);
}
// if user chooses 0, then exit program
else if (option == 0){
counter = false;
menu.close();
System.exit(0);
}
// anything else then reprompt
else{
String errorMSG = "> ERROR: Invalid Command!";
printHeader(errorMSG);
System.out.println(errorMSG);
printHeader(errorMSG);
continue;
}
}
}
// function to add a recipe
public static Recipe addRecipe(Scanner menu) throws IOException{
// Recipe Name
String prompt = "> What is the recipe's name?";
printHeader(prompt);
System.out.println(prompt);
printHeader(prompt);
Recipe a = new Recipe();
menu.nextLine();
String Name = (String) menu.nextLine();
a.setName(Name);
// Ingredients
String prompt1 = "> What are ingredients required for this recipe? Please enter one by one. If done please type 'done'.";
printHeader(prompt1);
System.out.println(prompt1);
printHeader(prompt1);
// user ingredient input
String ingredientBuild = menu.nextLine();
List<String> list = new ArrayList<String>();
// build ingredient arraylist
while (!(ingredientBuild.toLowerCase().equals("done"))){
list.add(ingredientBuild);
ingredientBuild = (String) menu.nextLine();
}
String[] ingredientsArray = list.toArray(new String[0]);
// construct recipe ingredients
a.setIngredientsList(ingredientsArray);
// Instructions
String prompt2 = "> What are the recipe's instructions? Please enter one by one. If done please type 'done'";
printHeader(prompt2);
System.out.println(prompt2);
printHeader(prompt2);
String recipeInstructions = "";
int count = 1;
String instructionBuild = (String) menu.nextLine();
// build instructions string
while (!(instructionBuild.toLowerCase().equals("done"))){
recipeInstructions += Integer.toString(count) + ". ";
recipeInstructions += instructionBuild;
recipeInstructions += "\n";
count++;
instructionBuild = (String) menu.nextLine();
}
a.setInstructionList(recipeInstructions);
// Name write
String nameSTR = "\nNAME: " + a.getName();
// Ingreds write
String ingredientsSTR = "\nINGREDIENTS: ";
for(int i = 0; i < list.size(); i++){
if (i == list.size()-1){
ingredientsSTR+= list.get(i);
}
else{
ingredientsSTR += list.get(i) + ",";
}
}
String instructSTR = "\n" + a.getInstructionList();
File file = new File("recipeBook/recipes.txt");
FileWriter fw = null;
try{
fw = new FileWriter(file, true);
fw.write("\n{");
fw.write(nameSTR);
fw.write(ingredientsSTR);
fw.write("\nINSTRUCTIONS: ");
fw.write(instructSTR);
fw.write("\n}");
} catch (IOException e){
e.printStackTrace();
} finally {
try{
fw.close();
} catch (IOException e){
e.printStackTrace();
}
}
// add recipe
return(a);
}
// function to search and view a singular recipe
public static void searchRecipe(Scanner menu, List<Recipe> recipeList){
String prompt = "> What would you like to search for?";
printHeader(prompt);
System.out.println(prompt);
printHeader(prompt);
menu.nextLine();
String recipeNameSearch = menu.nextLine();
// store matches to either name of recipe or ingredients of recipes
List<Recipe> recipeNameMatches = new ArrayList<>();
List<Recipe> recipeIngredMatches = new ArrayList<>();
// cycle through all recipes in default recipeList
for(Recipe recipe : recipeList) {
if (recipeNameSearch.toLowerCase().equals(recipe.getName().toLowerCase())){
recipeNameMatches.add(recipe);
}
else {
// cycle through all ingredients of each recipe
List<String> ingredients = recipe.getIngredientsList();
for(String ingredient : ingredients){
if (recipeNameSearch.toLowerCase().equals(ingredient.toLowerCase())){
recipeIngredMatches.add(recipe);
}
}
}
}
int nameMatchCount = recipeNameMatches.size();
int ingredMatchCount = recipeIngredMatches.size();
String nameMatchString = String.format("%d", nameMatchCount);
String ingredMatchString = String.format("%d", ingredMatchCount);
// if search word matches to name of at least one recipe
if (nameMatchCount>0){
// if no match count
for(int i = 0; i < nameMatchCount; i++){
if (nameMatchCount == 1){
String nameMatches = "> NAME MATCHES: " + nameMatchString;
printHeader(nameMatches);
System.out.println(nameMatches);
printHeader(nameMatches);
System.out.println(recipeNameMatches.get(i));
String[] lastInstruct = recipeNameMatches.get(i).getInstructionList().split("\n");
int lastInstructIndex = lastInstruct.length-1;
printHeader(lastInstruct[lastInstructIndex]);
}
else if (i == 0){
String nameMatches = "> NAME MATCHES: " + nameMatchString;
printHeader(nameMatches);
System.out.println(nameMatches);
printHeader(nameMatches);
System.out.println(recipeNameMatches.get(i));
System.out.println();
}
else if (i == nameMatchCount-1){
String[] lastInstruct = recipeNameMatches.get(i).getInstructionList().split("\n");
int lastInstructIndex = lastInstruct.length-1;
System.out.println(recipeNameMatches.get(i));
printHeader(lastInstruct[lastInstructIndex]);
}
else{
System.out.println(recipeNameMatches.get(i));
System.out.println();
}
}
}
if (ingredMatchCount > 0){
for(int i = 0; i < ingredMatchCount; i++){
if (ingredMatchCount == 1){
String ingredMatches = "> INGREDIENT MATCHES: " + ingredMatchString;
printHeader(ingredMatches);
System.out.println(ingredMatches);
printHeader(ingredMatches);
System.out.println(recipeIngredMatches.get(i));
String[] lastInstruct = recipeIngredMatches.get(i).getInstructionList().split("\n");
int lastInstructIndex = lastInstruct.length-1;
printHeader(lastInstruct[lastInstructIndex]);
}
else if (i == 0){
String ingredMatches = "> INGREDIENT MATCHES: " + ingredMatchString;
printHeader(ingredMatches);
System.out.println(ingredMatches);
printHeader(ingredMatches);
System.out.println(recipeIngredMatches.get(i));
System.out.println();
}
else if (i == ingredMatchCount-1){
String[] lastInstruct = recipeIngredMatches.get(i).getInstructionList().split("\n");
int lastInstructIndex = lastInstruct.length-1;
System.out.println(recipeIngredMatches.get(i));
printHeader(lastInstruct[lastInstructIndex]);
}
else{
System.out.println(recipeIngredMatches.get(i));
System.out.println();
}
}
}
return;
}
// function to search and view a recipe step by step
public static void searchRecipeStepByStep(Scanner menu, List<Recipe> recipeList){
String promptt = "> What recipe would you like to look at?";
printHeader(promptt);
System.out.println(promptt);
printHeader(promptt);
menu.nextLine();
String recipeNameSearch = menu.nextLine(); // name
for(Recipe recipe : recipeList) {
if (recipeNameSearch.toLowerCase().equals(recipe.getName().toLowerCase())){
// instructions stored in instructionString
String instructionString = recipe.getInstructionList();
// convert to array of strings
String[] instructionSteps = instructionString.split("\n");
for (String step : instructionSteps){
String promptt1 = "> Press enter to reveal next step. Enter any character to return to Menu.";
printHeader(promptt1);
System.out.println(promptt1);
printHeader(promptt1);
String prompt = menu.nextLine();
if (prompt.equals("")){
printHeader(step);
System.out.println(step);
printHeader(step);
}
else{
break;
}
}
}
}
}
// function for viewing all recipes
public static void viewAllRecipes(Scanner menu, List<Recipe> recipeList){
System.out.println();
for(int i = 0; i < recipeList.size(); i++) {
if (i == 0){
String namee = "NAME: " + recipeList.get(i).getName();
printHeader(namee);
System.out.println(recipeList.get(i));
System.out.println();
}
else if(i == (recipeList.size() - 1)){
String[] instructionListtt = recipeList.get(i).getInstructionList().split("\n");
int lastInstructionIndex = instructionListtt.length - 1;
System.out.println(recipeList.get(i));
printHeader(instructionListtt[lastInstructionIndex]);
}
else{
System.out.println(recipeList.get(i));
System.out.println();
}
}
return;
}
// readRecipes and return them in an arraylist
public static List<Recipe> readRecipes() throws FileNotFoundException{
// construct new recipeList
List<Recipe> recipeList = new ArrayList<Recipe>();
File recipeFile = new File("recipeBook/recipes.txt");
try{
Scanner inFile = new Scanner(recipeFile);
// skip first line
inFile.nextLine();
while (inFile.hasNextLine()){
String currentLine = inFile.nextLine();
// valid recipe
if (currentLine.equals("{")){
// create recipe
Recipe b = new Recipe();
// move one line down
String nameLine = inFile.nextLine();
// set name
String name = nameLine.split(": ", -1)[1];
if (!name.isEmpty()){
b.setName(name);
}
else{
b.setName("Empty");
}
// move one line down
String ingredientsline = inFile.nextLine();
// set ingredients
String ingredientsString = ingredientsline.split(": ", -1)[1];
String[] ingredients = ingredientsString.split(",");
if (ingredients.length != 0){
b.setIngredientsList(ingredients);
}
else{
String[] emptyIngredients = {"none"};
b.setIngredientsList(emptyIngredients);
}
// move two lines down
inFile.nextLine();
// construct instructionLine
String instructionLine = inFile.nextLine();
String instructionsString = "";
while (!instructionLine.equals("}")){
instructionsString += instructionLine + "\n";
instructionLine = inFile.nextLine();
}
String stripped = instructionsString.strip();
b.setInstructionList(stripped);
recipeList.add(b);
}
}
} catch (FileNotFoundException e){
System.err.println("File: 'recipes.txt' not found in current directory.");
System.exit(0);
}
return recipeList;
}
public static void printHeader(String a){
String b = "-";
for (int i = 0; i < a.length(); i++){
b += "-";
}
System.out.println(b);
}
// display main options
public static void menuScreen(){
System.out.println();
printHeader("> Welcome to your Recipe Book");
System.out.println("> Welcome to your Recipe Book");
System.out.println("> To add a recipe, please press 1");
System.out.println("> To search a recipe, please press 2");
System.out.println("> To view a recipe step-by-step, please press 3");
System.out.println("> To view all recipes, please press 4");
System.out.println("> To exit, press 0");
printHeader("> To exit, press 0");
}
}