1. 程式人生 > >作業: 小型購物系統 小結

作業: 小型購物系統 小結

系統 ase RR 入口 OS lec 用戶 exit PE

作業:
用戶入口:
1. 商品信息存在文件裏
2. 已夠商品,余額記錄

商家入口
2. 可以添加商品,修改商品價格

數據文件:商品庫:product_store.db

{‘book‘: {‘price‘: 45, ‘stocks‘: 80}, ‘watch‘: {‘price‘: 210, ‘stocks‘: 27}, ‘phone‘: {‘price‘: 1999, ‘stocks‘: 29}, ‘apple‘: {‘price‘: 6, ‘stocks‘: 21}, ‘orange‘: {‘price‘: 4, ‘stocks‘: 23}, ‘computer‘: {‘price‘: 4800, ‘stocks‘: 5}, ‘cap‘: {‘price‘: 56, ‘stocks‘: 13}}

數據文件:購物車:cart_list.db

[]

編碼:

# Author:Brace Li
import sys,time # list entrance for user
print("*" * 60)
print("*%s*" % " ".center(58))
print("*%s*" % "Shopping System".center(58))
print("*%s*" % "v1.00".center(58))
print("*%s*" % "1.For Host 2.For Customer".center(58))
print("*%s*" % " ".center(58))
print("*" * 60) # for user selected
while True:
user_selected = input("Pls Input:").strip()
if not (user_selected and user_selected.isdigit()):
print("\033[1;31;40mError: input error!\033[0m ")
continue
if not int(user_selected) in (1, 2):
print("\033[1;31;40mError: input error!\033[0m ")
continue
else:
break # for shop host
if int(user_selected) == 1:
# get product date from database
with open("product_store.db", "r") as fs:
product_data = eval(fs.read())
print("-" * 60)
print("|{name}|{price}|{balance}|".format(name="Name".center(25, " "), price="Price".center(15, " "), balance="Balance".center(16, " ")))
print("-" * 60)
for n in product_data:
# print(n, product_data[n]["price"], product_data[n]["stocks"])
print("|{name}|{price}|{balance}|".format(name=n.center(25, " "), price=str(product_data[n]["price"]).center(15, " "), balance=str(product_data[n]["stocks"]).center(16, " ")))
print("-" * 60)
# info for action of host
flag = True
while True:
if flag:
print("*" * 60)
print("*%s*" % "1.Edit Price 2.Add Products 3. Exit System".center(58, " "))
print("*" * 60)
host_selected = input("Pls input:").strip()
if not (host_selected and host_selected.isdigit()):
print("\033[1;31;40mError: input error!\033[0m ")
flag = False
continue
if not int(host_selected) in (1, 2, 3):
print("\033[1;31;40mError: input error!\033[0m ")
flag = False
continue
flag = True
if int(host_selected) == 3:
with open("product_store.db", "w") as fs:
fs.write(str(product_data))
sys.exit("Bye, the system exiting......")
elif int(host_selected) == 1:
edit_pro_flag = {"status": False, "sel_pro": ""}
while True:
product_selected = input("Input product:").strip()
if not product_selected:
print("\033[1;31;40mError: input error!\033[0m ")
continue
elif product_selected in product_data:
edit_pro_flag["status"] = True
edit_pro_flag["sel_pro"] = product_selected
break
else:
print("\033[1;31;40mError: Product not exist...!\033[0m ")
continue
if edit_pro_flag["status"]:
while True:
new_pro_price = input("Input New Price:").strip()
if not (new_pro_price and new_pro_price.isdigit()):
print("\033[1;31;40mError: input error!\033[0m ")
continue
else:
product_data[edit_pro_flag["sel_pro"]]["price"] = int(new_pro_price)
break
print("-" * 60)
print("|{name}|{price}|{balance}|".format(name="Name".center(25, " "), price="Price".center(15, " "),balance="Balance".center(16, " ")))
print("-" * 60)
for n in product_data:
print("|{name}|{price}|{balance}|".format(name=n.center(25, " "), price=str(product_data[n]["price"]).center(15, " "), balance=str(product_data[n]["stocks"]).center(16, " ")))
print("-" * 60)
edit_pro_flag = {"status": False, "sel_pro": ""}
else:
while True:
pro_name = input("Input Product Name:").strip()
if not pro_name:
print("\033[1;31;40mError: input error!\033[0m ")
continue
else:
break
if pro_name in product_data:
while True:
add_count = input("the %s exit, add stocks:" % pro_name).strip()
if not (add_count and add_count.isdigit()):
print("\033[1;31;40mError: input error!\033[0m ")
continue
else:
product_data[pro_name]["stocks"] += int(add_count)
break
else:
while True:
add_price = input("Input %s‘s prince:"% pro_name).strip()
if not (add_price and add_price.isdigit()):
print("\033[1;31;40mError: input error!\033[0m ")
continue
else:
break
while True:
add_count = input("Input %s‘s stocks:" % pro_name)
if not (add_count and add_count.isdigit()):
print("\033[1;31;40mError: input error!\033[0m ")
continue
else:
break
product_data[pro_name] = {‘price‘: int(add_price), ‘stocks‘: int(add_count)} print("-" * 60)
print("|{name}|{price}|{balance}|".format(name="Name".center(25, " "), price="Price".center(15, " "),balance="Balance".center(16, " ")))
print("-" * 60)
for n in product_data:
print("|{name}|{price}|{balance}|".format(name=n.center(25, " "), price=str(product_data[n]["price"]).center(15, " "), balance=str(product_data[n]["stocks"]).center(16, " ")))
print("-" * 60) # for customer
if int(user_selected) == 2:
# load cart data
with open("cart_list.db", "r") as fs:
cart_list = eval(fs.read())
# load list product
with open("product_store.db", "r") as fs:
product_data = eval(fs.read()) while True:
print("-" * 60)
print("|{name}|{price}|{balance}|".format(name="Name".center(25, " "), price="Price".center(15, " "), balance="Balance".center(16, " ")))
print("-" * 60)
for n in product_data:
print("|{name}|{price}|{balance}|".format(name=n.center(25, " "), price=str(product_data[n]["price"]).center(15, " "), balance=str(product_data[n]["stocks"]).center(16, " ")))
print("-" * 60) while True:
select_product = input("Input your products:").strip()
if not select_product:
print("\033[1;31;40mError: input error!\033[0m ")
continue
if select_product in product_data:
cart_list.append(select_product)
print("\033[1;33;40mMsg: Product been in cart successfully!\033[0m ")
break
else:
print("\033[1;31;40mError: Product not exit!\033[0m ")
continue while True:
print("*" * 60)
print("*%s*" % "1.Continue 2.Charge 3. Exit System".center(58, " "))
print("*" * 60)
customer_action = input("Input select: ").strip()
if not (customer_action and customer_action.isdigit()):
print("\033[1;31;40mError: input error!\033[0m ")
continue
if not int(customer_action) in (1, 2, 3):
print("\033[1;31;40mError: input error!\033[0m ")
continue
if int(customer_action) == 1:
break
elif int(customer_action) == 3:
with open("cart_list.db", "w") as fs:
fs.write(str(cart_list))
sys.exit("System exiting .....!")
else:
# list product info in cart
cart_info = list(set(cart_list))
print("")
print("")
print("*" * 60)
print("|%s|" %"Products List In Cart".center(58, " "))
print("-" * 60)
print("|{id}|{name}|{price}|{quantity}|{total}|".format(id="ID".center(5, " "), name="Name".center(19, " "), price="Price".center(10, " "), quantity="Quantity".center(10, " "), total="Total".center(10, " ")))
print("-" * 60)
total_price = 0
for c_id, c_name in enumerate(cart_info):
print("|{id}|{name}|{price}|{quantity}|{total}|".format(id=str(c_id).center(5, " "), name=c_name.center(19, " "), price=str(product_data[c_name]["price"]).center(10, " "), quantity=str(cart_list.count(c_name)).center(10, " "),total=str(product_data[c_name]["price"] * cart_list.count(c_name)).center(10, " ")))
print("-" * 60)
total_price += product_data[c_name]["price"] * cart_list.count(c_name) print("|{data_name}|{date_value}|{total_name}|{total_value}|".format(data_name="Date".center(10," "), date_value=str(time.strftime(‘%Y-%m-%d‘,time.localtime())).center(15, " "), total_name="Total".center(10, " "), total_value=str(total_price).center(20, " ")))
print("-" * 60)
print()
print("*" * 60)
print("*%s*" % "1.Back to Menu 2.Pay Account".center(58, " "))
print("*" * 60)
while True:
c_selected = input("Pls Input:").strip()
if not (c_selected and c_selected.isdigit()):
print("\033[1;31;40mError: input error!\033[0m ")
continue
if not int(c_selected) in (1, 2):
print("\033[1;31;40mError: input error!\033[0m ")
continue
else:
break
if int(c_selected) == 2:
cart_list.clear()
with open("cart_list.db", "w") as fs:
fs.write(str(cart_list))
sys.exit("System exiting .....!")
else:
break

作業: 小型購物系統 小結