分享python实现自动售货一个小作业
输入2进行购买商品,输入商品id和数量,可以选择不同商品,最后进行结账操作。
可以有两种查询方式,全部查询和按时间条件查询
具体操作如下:
全部查询:
时间条件查询:
就按4直接退出23333
其实就是参考路边的自动售货机贩卖饮料啥的,也有借鉴其他人的代码。改进的地方在于购买商品功能,能选择不同商品和数量,最后再结账。
定义了商品类、订单类
一开始初始化商品的数据
import time
print('欢迎光临')
class Products:
#商品类 定义名字、价格和存货数量
def __init__(self, name, price, surplus_count):
self.name = name
self.price = price
self.surplus_count = surplus_count
class Order:
#订单类 包括购买物品名称、价格和时间 购买数量
def __init__(self, name, price, buy_time,buy_count):
self.name = name
self.price = price
self.buy_time = buy_time
self.buy_count = buy_count
#字符串时间对比
def compare_time(time1,time2):
s_time = time.mktime(time.strptime(time1,'%Y-%m-%d %H:%M:%S'))
e_time = time.mktime(time.strptime(time2,'%Y-%m-%d %H:%M:%S'))
return int(s_time) - int(e_time)
#全局变量 商品列表
products_list = [
Products("乐事薯片", 5, 5),
Products("卫龙辣条", 3, 6),
Products("可乐", 3, 2),
Products("橙汁", 4, 5),
Products("波力海苔", 6, 0),
]
#订单list 存放订单数据
order_list = []
#打印商品列表
def show_products_list():
print("序号\t\t商品名称\t\t价格\t\t存货数量")
i = 1
for products in products_list:
print("%d\t\t%s\t\t%s\t\t%s" % (i, products.name, products.price,products.surplus_count))
i += 1
#购买商品
def buy_products():
buy_obj = {}
while(True):
op = input("1.购买商品 2.结账 3.退出 \n")
if op == "1":
products_id = int(input("请输入需要购买商品的序号:\n")) - 1
if products_id > len(products_list) or products_id < 0:
print("error!请输入正确的序号!")
else:
products = products_list[products_id]
if products.surplus_count > 0:
buy_count = int(input("请输入购买数量:"))
#限制库存提示
if buy_count> products.surplus_count:
print("库存不足,请提醒补货!")
else:
buy_obj[products_id] = buy_count;
else:
print("库存不足,请提醒补货!")
elif op == "2":
need_pay_money = 0;
for products_id in buy_obj.keys():
buy_count = buy_obj[products_id]
products = products_list[products_id]
need_pay_money += products.price * buy_count;
if need_pay_money<=0:
return ;
print ("您购买了:")
print("商品名\t\t商品价格\t\t购买数量")
for products_id in buy_obj.keys():
buy_count = buy_obj[products_id]
products = products_list[products_id]
print("%s\t\t%s\t\t%s\n" % (
products.name, products.price, buy_count))
print ("需要支付金额:\t",need_pay_money)
money = float(input("请输入支付金额:\n"))
if money >= need_pay_money:
#减库存
for products_id in buy_obj.keys():
buy_count = buy_obj[products_id]
products = products_list[products_id]
products.surplus_count = products.surplus_count - buy_count
products_list[products_id] = products
#生成订单
timeArray = time.localtime(int(time.time()))
order_list.append(Order(products.name, products.price * buy_count, time.strftime("%Y-%m-%d %H:%M:%S", timeArray),buy_count))
#清空
buy_obj.clear()
#大于0才提示找零
if money - need_pay_money >0:
print("购买成功:找回%.2f元!" % (money - need_pay_money))
else:
print("购买成功!")
break;
else:
print("支付金额不足!")
elif op == "3":
break;
#打印订单列表
def show_order_list():
is_query_all = int(input("请选择查询方式 1.查询全部销量 2.按时间查询销量\n"))
query_order_list = []
if is_query_all == 2:
time_str = time.localtime(int(time.time()))
now_time = time.strftime("%Y-%m-%d %H:%M:%S", time_str)
start_time = input("请输入开始时间 例如:"+now_time+"\n")
end_time = input("请输入结束时间 例如:"+now_time+"\n")
for order in order_list:
compare_num1 = compare_time(order.buy_time,start_time)
compare_num2 = compare_time(order.buy_time,end_time)
if compare_num1>=0 and compare_num2<=0:
query_order_list.append(order)
i = 1;
print("序号\t\t商品名\t\t支付金额\t\t购买数量\t\t购买时间")
for order in query_order_list:
print("%d\t\t%s\t\t%s\t\t%s\t\t%s" % (
i, order.name, order.price,order.buy_count,order.buy_time))
i += 1
elif is_query_all == 1:
i = 1
print("序号\t\t商品名\t\t支付金额\t\t购买数量\t\t购买时间")
for order in order_list:
print("%d\t\t%s\t\t%s\t\t%s\t\t%s" % (
i, order.name, order.price,order.buy_count,order.buy_time))
i += 1
if __name__ == '__main__':
while(True):
n = input("请输入相应数字:1.查询商品列表 2.购买商品 3.销售查询------打印销售明细 4.退出\n")
if n == "1":
#显示商品列表方法
show_products_list();
elif n == "2":
#购买商品
buy_products();
elif n == "3":
#显示订单列表
show_order_list();
elif n == "4":
print("已退出,欢迎下次光顾!")
break
else:
print("输入错误,请重新输入!")
治愈焦虑,一步一个脚印!