Skip to content

Commit c46593f

Browse files
committed
Add grocery shopping
1 parent 54189e6 commit c46593f

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
lines changed

grocery-shopping/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Grocery shopping
2+
3+
You enter with items to be store in a `.data` file
4+
5+
### Worked concepts
6+
* File reading and writing
7+
* Pickle

grocery-shopping/grocery-list.data

50 Bytes
Binary file not shown.

grocery-shopping/grocery_list.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
grocery_list = []
3+
4+
5+
def add(item):
6+
grocery_list.append(item)
7+
8+
9+
def add(*items):
10+
for i in items:
11+
grocery_list.append(i)
12+
13+
14+
def exclude(item):
15+
grocery_list.remove(item)
16+
17+
18+
def get_list():
19+
return grocery_list
20+
21+
22+
def alter(item, new_item):
23+
grocery_list[grocery_list.index(item)] = new_item
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import pickle
2+
3+
file_name = "grocery-list.data"
4+
5+
6+
def persist(items):
7+
with open(file_name, "wb") as f:
8+
pickle.dump(items, f)
9+
10+
11+
def read():
12+
items = []
13+
with open(file_name, "rb") as f:
14+
items = pickle.load(f)
15+
return items
16+
17+
18+
def update(items):
19+
with open(file_name, "r+b") as f:
20+
pickle.dump(items, f)

grocery-shopping/main.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import grocery_list
2+
import grocery_list_repository
3+
4+
5+
def main():
6+
option = 0
7+
while True:
8+
print("1 - Add item to the list")
9+
print("2 - Update item from the list")
10+
print("3 - Delete item from the list")
11+
print("4 - Get all items from the list")
12+
print("5 - Save list")
13+
print("0 - Exit")
14+
option = int(input("> "))
15+
16+
if option == 1:
17+
item = input("Enter the item: ")
18+
grocery_list.add(item.title())
19+
elif option == 2:
20+
item = input("Enter the item you want to replace: ")
21+
new_item = input("Enter the new item to store on the list: ")
22+
grocery_list.alter(item, new_item)
23+
elif option == 3:
24+
item = input("Enter the item you want to remove: ")
25+
grocery_list.exclude(item)
26+
elif option == 4:
27+
print("Items - ")
28+
print(grocery_list_repository.read())
29+
elif option == 5:
30+
grocery_list_repository.persist(grocery_list.get_list())
31+
elif option == 0:
32+
break
33+
34+
if __name__ == '__main__':
35+
main()

0 commit comments

Comments
 (0)