Skip to content
This repository was archived by the owner on May 12, 2018. It is now read-only.

Commit f7ccc5c

Browse files
committed
JS - home inventory app code
1 parent ceae20b commit f7ccc5c

File tree

1 file changed

+135
-0
lines changed

1 file changed

+135
-0
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#!python3
2+
3+
import sqlite3
4+
import sys
5+
from contextlib import contextmanager
6+
7+
DB = "Inventory.db"
8+
9+
#Script that creates the DB on first launch, otherwise is just a check.
10+
def first_launch():
11+
try:
12+
conn = sqlite3.connect(DB)
13+
except:
14+
sys.exit('Error code X')
15+
16+
17+
#generator to yield a db cursor and close the connection nicely.
18+
@contextmanager
19+
def access_db():
20+
try:
21+
conn = sqlite3.connect(DB)
22+
cursor = conn.cursor()
23+
yield cursor
24+
finally:
25+
conn.commit()
26+
conn.close()
27+
28+
29+
#A somewhat dirty menu. Improvement Point (make it a dict perhaps)
30+
def main_menu():
31+
menu = {}
32+
menu['1'] = "Add Room."
33+
menu['2'] = "Add Inventory."
34+
menu['3'] = "View Inventory List."
35+
menu['4'] = "Total Value."
36+
menu['5'] = "Exit."
37+
while True:
38+
print("\n")
39+
for item, desc in sorted(menu.items()):
40+
print(item, desc)
41+
42+
choice = input("Selection: ")
43+
if choice == '1':
44+
add_room()
45+
elif choice == '2':
46+
add_inventory(check_input())
47+
elif choice == '3':
48+
view_inventory(check_input())
49+
elif choice == '4':
50+
calc_total()
51+
elif choice == '5':
52+
sys.exit()
53+
else:
54+
print("Invalid option, try again.")
55+
56+
57+
#Adds a room. Scrubs the input first to only allow default chars.
58+
def add_room():
59+
name = input("\nWhat name would you like to give the room? ")
60+
name = scrub(name)
61+
with access_db() as cursor:
62+
cursor.execute("CREATE TABLE '" + name.lower() + "' """"
63+
(Item TEXT, Value REAL)
64+
""")
65+
print("\nA room with name %s has been added to the db.\n" % name)
66+
67+
68+
#Parses the DB for table names and returns a list of the names.
69+
def list_rooms():
70+
room_list = []
71+
with access_db() as cursor:
72+
cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
73+
for room in cursor:
74+
room_list.append(room[0])
75+
return room_list
76+
77+
78+
#The scrubbing function. Removes all chars that aren't letters and numbers.
79+
def scrub(table_name):
80+
return ''.join( chr for chr in table_name if chr.isalnum() )
81+
82+
83+
#Checks the users' input to see if it matches a room/table name.
84+
def check_input():
85+
while True:
86+
print('\n')
87+
for room in list_rooms():
88+
print(room)
89+
selection = input('Select a room: ').lower()
90+
if selection not in list_rooms():
91+
print("\n%s does not exist." % selection)
92+
else:
93+
return scrub(selection)
94+
95+
96+
#Allows users to add an item and value to the DB of a specific room.
97+
def add_inventory(selection):
98+
while True:
99+
name = input("\nName of item: ")
100+
cost = input("Monetary value: ")
101+
with access_db() as cursor:
102+
cursor.execute("INSERT INTO '" + selection + "' VALUES(?, ?)", [name, cost])
103+
104+
cont = input('\nHit Q to quit or any other key to continue: ')
105+
if cont.lower() == 'q':
106+
break
107+
108+
109+
#Returns a list of all items in a room and their total value.
110+
def view_inventory(selection):
111+
total = 0
112+
with access_db() as cursor:
113+
cursor.execute("SELECT * FROM '" + selection + "'")
114+
print("\n")
115+
for data in cursor:
116+
print("%s: $%d" % (data[0], data[1]))
117+
total += data[1]
118+
print("Total Value: $%d" % total)
119+
120+
121+
#Function to calculate the $ total of the entire database.
122+
def calc_total():
123+
total = 0
124+
room_list = list_rooms()
125+
with access_db() as cursor:
126+
for room in room_list:
127+
cursor.execute("SELECT value FROM '" + room + "'")
128+
for value in cursor:
129+
total += value[0]
130+
print("\nTotal Value of all rooms: $%d" % total)
131+
132+
133+
if __name__ == "__main__":
134+
first_launch()
135+
main_menu()

0 commit comments

Comments
 (0)