#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 开发团队: 奶豆的小铺
# 开发人员: bcz
# 开发时间: 2020/12/12 21:07
import pickle
import os
import sys
Base_Dir = os.path.dirname(os.path.dirname(__file__))
project_dir = os.path.dirname(__file__)
#商品类
class Goods():
def __init__(self,name,price,number):
self.name = name
self.price = price
self.number = number
def __str__(self):
return (f"商品名称{self.name} 商品数量 {self.number} 单品价值 {self.number * self.price}")
#购买商品
def buy_goods(self,goods,nums):
if self.name == goods.name:
if nums > self.number:
return False#购买不成功
else:
self.number -= nums
return True#购买成功
#退商品
def return_goods(self,name,nums):
if self.name == name:
self.number += nums
return True
return False
#商店管理员
class Admin():
opt_list = [("创建商品", ‘create_goods‘),
("查看所有商品", ‘show_all_goods‘),
(‘退出‘, ‘exit‘)
]
def __init__(self,name):
self.name = name
def create_goods(self):
‘‘‘创建商品‘‘‘
name = input(‘请输入商品名称: ‘)
price = float(input(‘请输入商品价格: ‘))
num = int(input(‘请输入商品数量: ‘))
goods_obj = Goods(name,price,num)
with open(‘goods_list_info‘,‘ab‘) as f:
pickle.dump(goods_obj,f)
print(f‘{goods_obj}商品创建成功‘)
def show_all_goods(self):
‘‘‘查看所有商品‘‘‘
with open(‘goods_list_info‘,‘rb‘) as f:
count = 0
while True:
try:
count +=1
goods_obj = pickle.load(f)
print(f‘商品序号:{count},商品名称:{goods_obj.name},商品价格:{goods_obj.price},商品数量:{goods_obj.number}‘)
except EOFError:
print(‘还没有商品,请先添加商品‘)
break
def exit(self):
exit(‘欢迎下次使用‘)
#顾客类
class Customer():
opt_list = [("购买商品", ‘select_goods‘),
("退商品", ‘return_goods‘),
(‘退出‘, ‘exit‘),
(‘打印列表‘,"show_goods")
]
#初始化一个对象就显示一下所有的商品
def __init__(self,salary_all_money):
self.salary_all_money = salary_all_money
self.shopping_car = []
self.shopping_money = 0
self.goods_list = []
self.show_goods()
def show_goods(self):
count = 0
#先把商品从二进制文件中读取出来放到self.goods_list
with open(‘goods_list_info‘,‘rb‘) as ff:
while True:
try:
count+=1
goods_obj = pickle.load(ff)
print(f‘商品序号:{count},商品名称:{goods_obj.name},商品价格:{goods_obj.price},商品数量:{goods_obj.number}‘)
self.goods_list.append(goods_obj)
except EOFError:
break
def add_shopping_car(self,buy_goods):
for item in self.shopping_car:
if item.name == buy_goods.name:#证明购物车中有此商品
item.number += buy_goods.number
return item.number
else:
self.shopping_car.append(buy_goods)
return buy_goods.number
# 购买商品
def select_goods(self):
# 现打印所有商品
self.show_goods()
number_goods = int(input(‘请输入你选择的商品的序号: ‘))
num_goods = int(input(‘请输入购买该商品的数量: ‘))
if number_goods > len(self.goods_list):
print("输入错误")
else:
googs_info = self.goods_list[number_goods-1] # 商品对象信息
money = googs_info.price * num_goods # 选择商品需花的钱
if self.salary_all_money - self.shopping_money < money:
print("余额不足")
elif googs_info.buy_goods(googs_info, num_goods):
buy_googs = Goods(googs_info.name, googs_info.price, num_goods)
#购买后购物车中该商品的数量
googs_all_num = self.add_shopping_car(buy_googs)
# self.shopping_car.get(buy_googs.name)
self.shopping_money = self.shopping_money + money
print(f‘您选择了{buy_googs.name}商品,购买了{googs_all_num}个‘)
else:
print("商品不足")
def return_goods(self):
‘‘‘退商品‘‘‘
# #循环自己的购物车,选择自己想退的商品然后从新pickle商品清单
goods_name_return = input(‘请输入你想退的商品名称: ‘)
for row in self.shopping_car:
if goods_name_return == row.name:
number_return = int(input(‘请输入你想退的数量: ‘))
if number_return > row.num:
print(‘你没有购买那么多该商品,退货不成功: ‘)
break
else:
row.num = row.num - number_return
for goose_info in self.goods_list:
if goose_info.return_goods(goods_name_return,number_return):
break
# self.show_goods()
self.shopping_money = self.shopping_money + number_return * row.price
print(‘退货成功‘)
break
else:
print(‘你没有购买该商品: %s ‘ %goods_name_return)
# for index,item in enumerate(self.shopping_car,1):
# if item[0] == goods_name_return:
# number_return = int(input(‘请输入你想退的数量: ‘))
# if number_return <= item[1]:
# self.shopping_car.remove(item)
# item[1] -= number_return
# self.shopping_car.append([item[0],item[1],item[2]])
# print(‘退货成功‘)
# break
# else:
# print(‘你没有购买那么多该商品,退货不成功: ‘)
# break
# else:
# print(‘你没有购买该商品: ‘)
# break
‘‘‘显示顾客的消费信息 包括该顾客购买了哪些商品 分别购买了多少 购买的单价‘‘‘
def show_shop_info(self):
with open(‘customer_shop_info‘,‘ab‘) as f:
pickle.dump(self.shopping_car,f)#把该顾客的购物车信息添加到文件对象中
with open(‘customer_shop_info‘, ‘rb‘) as f1:
count =0
while True:
try:
shop_info = pickle.load(f1) # 一个顾客的购物车
count +=1
print(f‘{count}号顾客购买商品如下:‘)
for item in shop_info:
print(f‘商品名称:{item.name},购买数量{item.number},商品价格:{item.price},共计消费{item.number * item.price}‘)
except EOFError:
break
#退出会显客所购商品信息
def exit(self):
print(‘购买商品如下》》》:‘)
#shopping_car = [{‘iphone‘:12},{‘ipad‘:9},{‘ipone‘:11}]
for googs in self.shopping_car:
print(googs)
# for index, item in enumerate(self.shopping_car, 1):
# print(f‘商品序号:{index},商品名称:{item[0]},购买数量:{item[1]}‘)
self.salary_all_money = self.salary_all_money - self.shopping_money
print(f‘共计消费:{self.shopping_money}‘)
print(self.shopping_car)
self.show_shop_info()
#
# self.save_googs()
exit(‘欢迎下次光临......‘)
def login():
name = input(‘请输入用户名: ‘)
pwd = input(‘请输入密码: ‘)
with open(‘userinfo‘,encoding = ‘utf-8‘)as f:
while True:
try:
for line in f:
user,pawd,identify = line.strip().split("|")
if user == name and pawd == pwd:
return {‘name‘:name,‘result‘:True,‘id‘:identify}
else:
return {‘result‘:False,‘name‘:name,‘id‘:identify}
except EOFError:
break
res = login()
if res[‘result‘]:
print(f"{res[‘name‘]}登录成功")
if hasattr(sys.modules[__name__],res[‘id‘]):
cls = getattr(sys.modules[__name__],res[‘id‘])
print(res[‘id‘])
if cls == Admin:
obj = Admin(res[‘name‘])
while True:#管理员对象可以循环选择进行操作
#循环列举中管理员具有的方法
for index,item in enumerate(obj.opt_list,1):
print(index,item[0])
func_str = obj.opt_list[int(input(‘请输入你选择的序号: ‘))-1][1]
if hasattr(obj,func_str):
getattr(obj,func_str)()
elif cls == Customer:
for item in os.listdir(project_dir):
if item == ‘goods_list_info‘:
salary_all_money = float(input(‘请输入工资总额: ‘))
obj = Customer(salary_all_money)
while True:
for index, item in enumerate(obj.opt_list, 1):
print(index, item[0])
func_str = obj.opt_list[int(input(‘请输入你选择的序号: ‘)) - 1][1]
print(func_str)
if hasattr(obj, func_str):
getattr(obj, func_str)()
else:
print(‘商品列表为空,请先以管理员身份登录创建商品。‘)
else:
print(f‘{res["name"]}登录失败‘)
原文:https://www.cnblogs.com/lihuafeng/p/14135845.html