-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecipeManager.java
More file actions
119 lines (110 loc) · 4.17 KB
/
RecipeManager.java
File metadata and controls
119 lines (110 loc) · 4.17 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
/*
* File name: RecipeManager.java
* Author: Quoc Phong Tran, 041134348
* Course: CST8284 – OOP
* Lab: Assignment 3
* Date: December 03, 2024
* Professor: Moshiur Rahman
* Purpose: This program manages a collection of recipes, allows for loading recipes from
* a text file and saves the shopping list to a specified file. It provides functionality
* for reading recipes and their ingredients.
* Class list: Recipe, RecipeManagerTest, RecipeManager
*/
package assn3;
/**
* The RecipeManager class is responsible for managing the recipe list,
* loading recipe data from a file, and saving shopping lists based on the
* ingredients required for each recipe.
* @author Quoc Phong Tran
* @version 1.0.0
* @see java.io.File
* @see java.io.IOException
* @see java.io.OutputStreamWriter
* @see java.io.FileNotFoundException
* @see java.util.List
* @see java.io.FileOutputStream
* @see java.util.ArrayList
* @see java.util.Scanner
* @see assn3
* @since 21.0.3
*/
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* Manages the list of recipes and provides functionality to load recipes,
* generate shopping lists, and save the list to a file.
*/
public class RecipeManager {
private List<Recipe> recipes = new ArrayList<>();
/**
* Loads recipes from the specified text file.
*
* @param filePath The path to the recipe file.
* @throws IOException If the file cannot be read.
*/
public void loadRecipes(String filePath) throws IOException {
File file = new File(filePath);
// Check if the file exists
if (!file.exists()) {
throw new FileNotFoundException("Recipe list file not found: " + filePath);
}
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine().trim();
if (line.startsWith("Recipe")) {
String name = line.substring(line.indexOf(" ")+1).trim();
float eggs = 0;
float yeast = 0;
float flour = 0;
float sugar = 0;
float butter = 0;
for (int i = 0; i < 5; i++) {
String[] parts = scanner.nextLine().split(" ");
switch (parts[0]) {
case "eggs": eggs = Float.parseFloat(parts[1]);
break;
case "yeast": yeast = Float.parseFloat(parts[1]);
break;
case "flour": flour = Float.parseFloat(parts[1]);
break;
case "sugar": sugar = Float.parseFloat(parts[1]);
break;
case "butter": butter = Float.parseFloat(parts[1]);
break;
}
}
Recipe recipe = new Recipe(name, eggs, yeast, flour, sugar, butter);
recipes.add(recipe);
}
}
}
}
/**
* Saves the shopping list to a file.
*
* @param filePath The file path to save the shopping list.
* @param shoppingList The shopping list content.
* @throws IOException If the file cannot be written.
*/
public void saveShoppingList(String filePath, String shoppingList) throws IOException {
try (FileOutputStream fos = new FileOutputStream(filePath);
OutputStreamWriter writer = new OutputStreamWriter(fos)) {
writer.write(shoppingList);
}
}
/**
* Retrieves the list of all recipes managed by the RecipeManager.
*
* @return A list of Recipe objects representing all the recipes
* loaded into the RecipeManager.
*/
public List<Recipe> getRecipes() {
return recipes;
}
}