-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecipe Manager.py
165 lines (138 loc) · 6.22 KB
/
Recipe Manager.py
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
import sqlite3
from datetime import datetime
class RecipeManager:
def __init__(self, db_name):
self.conn = sqlite3.connect(db_name)
self.cursor = self.conn.cursor()
self.create_tables()
def create_tables(self):
self.cursor.execute('''CREATE TABLE IF NOT EXISTS recipes
(id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
ingredients TEXT,
instructions TEXT,
rating INTEGER,
date_added TEXT)''')
self.cursor.execute('''CREATE TABLE IF NOT EXISTS categories
(id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT UNIQUE)''')
self.conn.commit()
def add_recipe(self, name, ingredients, instructions, rating=0):
date_added = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.cursor.execute('''INSERT INTO recipes (name, ingredients, instructions, rating, date_added)
VALUES (?, ?, ?, ?, ?)''', (name, ingredients, instructions, rating, date_added))
self.conn.commit()
def add_category(self, name):
try:
self.cursor.execute('''INSERT INTO categories (name) VALUES (?)''', (name,))
self.conn.commit()
return True
except sqlite3.IntegrityError:
return False # Category name already exists
def rate_recipe(self, recipe_id, rating):
self.cursor.execute('''UPDATE recipes SET rating = ? WHERE id = ?''', (rating, recipe_id))
self.conn.commit()
def get_all_categories(self):
self.cursor.execute('''SELECT * FROM categories''')
categories = self.cursor.fetchall()
return categories
def search_recipes(self, query):
self.cursor.execute('''SELECT * FROM recipes
WHERE name LIKE ? OR ingredients LIKE ? OR instructions LIKE ?''',
('%' + query + '%', '%' + query + '%', '%' + query + '%'))
recipes = self.cursor.fetchall()
return recipes
def view_recipe(self, recipe_id):
self.cursor.execute('''SELECT * FROM recipes WHERE id = ?''', (recipe_id,))
recipe = self.cursor.fetchone()
return recipe
class UserPreferences:
def __init__(self, db_name):
self.conn = sqlite3.connect(db_name)
self.cursor = self.conn.cursor()
self.create_table()
def create_table(self):
self.cursor.execute('''CREATE TABLE IF NOT EXISTS preferences
(id INTEGER PRIMARY KEY,
favorite_category TEXT,
max_rating INTEGER)''')
self.conn.commit()
def set_favorite_category(self, category):
self.cursor.execute('''INSERT OR REPLACE INTO preferences (id, favorite_category) VALUES (1, ?)''', (category,))
self.conn.commit()
def set_max_rating(self, max_rating):
self.cursor.execute('''INSERT OR REPLACE INTO preferences (id, max_rating) VALUES (1, ?)''', (max_rating,))
self.conn.commit()
def get_favorite_category(self):
self.cursor.execute('''SELECT favorite_category FROM preferences WHERE id = 1''')
favorite_category = self.cursor.fetchone()
return favorite_category[0] if favorite_category else None
def get_max_rating(self):
self.cursor.execute('''SELECT max_rating FROM preferences WHERE id = 1''')
max_rating = self.cursor.fetchone()
return max_rating[0] if max_rating else None
def main():
recipe_manager = RecipeManager('recipes.db')
user_preferences = UserPreferences('preferences.db')
while True:
print("\nRecipe Manager Menu:")
print("1. Add Recipe")
print("2. Rate Recipe")
print("3. Search Recipes")
print("4. View Recipe")
print("5. Set Favorite Category")
print("6. Set Max Rating")
print("7. Exit")
choice = input("Enter your choice: ")
if choice == "1":
name = input("Enter recipe name: ")
ingredients = input("Enter ingredients (comma-separated): ").split(',')
instructions = input("Enter instructions: ")
recipe_manager.add_recipe(name, ','.join(ingredients), instructions)
print("Recipe added successfully!")
elif choice == "2":
recipe_id = input("Enter recipe ID: ")
rating = int(input("Enter rating (1-5): "))
recipe_manager.rate_recipe(recipe_id, rating)
print("Recipe rated successfully!")
elif choice == "3":
query = input("Enter search query: ")
recipes = recipe_manager.search_recipes(query)
if recipes:
print("Search results:")
for recipe in recipes:
print(f"ID: {recipe[0]}, Name: {recipe[1]}")
else:
print("No recipes found.")
elif choice == "4":
recipe_id = input("Enter recipe ID: ")
recipe = recipe_manager.view_recipe(recipe_id)
if recipe:
print("Recipe details:")
print(f"Name: {recipe[1]}")
print(f"Ingredients: {recipe[2]}")
print(f"Instructions: {recipe[3]}")
print(f"Rating: {recipe[4]}")
else:
print("Recipe not found.")
elif choice == "5":
category = input("Enter favorite category: ")
success = recipe_manager.add_category(category)
if success:
user_preferences.set_favorite_category(category)
print("Favorite category set successfully!")
else:
print("Category already exists.")
elif choice == "6":
max_rating = int(input("Enter max rating (1-5): "))
user_preferences.set_max_rating(max_rating)
print("Max rating set successfully!")
elif choice == "7":
print("Exiting...")
break
else:
print("Invalid choice. Please try again.")
recipe_manager.conn.close()
user_preferences.conn.close()
if __name__ == "__main__":
main()