Skip to content

Commit 48f6b61

Browse files
committed
Fixed grocery list logic
1 parent 620062f commit 48f6b61

File tree

2 files changed

+41
-37
lines changed

2 files changed

+41
-37
lines changed
Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,24 @@
11
import pickle
2-
import grocery_list
2+
from pathlib import Path
33

4-
file_name = "grocery-list.data"
4+
file = Path("grocery-list.data")
55

66

77
def persist(items):
8-
with open(file_name, "wb") as f:
8+
with open(file, "wb") as f:
99
pickle.dump(items, f)
1010

1111

1212
def read():
1313
items = []
14-
with open(file_name, "rb") as f:
15-
items = pickle.load(f)
14+
if file.is_file():
15+
with open(file, "rb") as f:
16+
items = pickle.load(f)
1617
return items
1718

1819

19-
def update(item, new_item):
20-
items = read()
21-
items[items.index(item)] = new_item
22-
with open(file_name, "r+b") as f:
23-
pickle.dump(items, f)
24-
25-
2620
def delete(item):
2721
items = read()
2822
items.remove(item)
29-
with open(file_name, "r+b") as f:
30-
items = grocery_list.get_list()
23+
with open(file, "r+b") as f:
3124
pickle.dump(items, f)

grocery-shopping/main.py

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,47 @@
1-
import grocery_list
21
import grocery_list_repository
32

3+
items = grocery_list_repository.read()
4+
5+
6+
def options(option):
7+
if option == 1:
8+
item = input("Enter the item: ")
9+
global items
10+
items.append(item)
11+
grocery_list_repository.persist(items)
12+
print(f"Item {item} saved in the list")
13+
return 0
14+
elif option == 2:
15+
item = input("Enter the item you want to remove: ")
16+
items.remove(item)
17+
grocery_list_repository.persist(items)
18+
print(f"Item {item} removed from the list")
19+
return 0
20+
elif option == 3:
21+
print("Items - ")
22+
if grocery_list_repository.read():
23+
items = grocery_list_repository.read()
24+
for item in items:
25+
print(item, end=" ")
26+
print()
27+
else:
28+
print("Empty list")
29+
return 0
30+
elif option == 0:
31+
return 1
32+
433

534
def main():
635
option = 0
7-
items = grocery_list_repository.read()
836
while True:
937
print("1 - Add item to the list")
10-
print("2 - Update item from the list")
11-
print("3 - Delete item from the list")
12-
print("4 - Get all items from the list")
13-
print("5 - Save list")
38+
print("2 - Delete item from the list")
39+
print("3 - Get all items from the list")
1440
print("0 - Exit")
15-
option = int(input("> "))
16-
41+
option = options(int(input("> ")))
1742
if option == 1:
18-
item = input("Enter the item: ")
19-
grocery_list.add(item.title())
20-
elif option == 2:
21-
item = input("Enter the item you want to replace: ")
22-
new_item = input("Enter the new item to store on the list: ")
23-
grocery_list_repository.update(item, new_item)
24-
elif option == 3:
25-
item = input("Enter the item you want to remove: ")
26-
grocery_list_repository.delete(item)
27-
elif option == 4:
28-
print("Items - ")
29-
print(grocery_list_repository.read())
30-
elif option == 5:
31-
grocery_list_repository.persist(grocery_list.get_list())
32-
elif option == 0:
3343
break
3444

45+
3546
if __name__ == '__main__':
3647
main()

0 commit comments

Comments
 (0)