版本:python3.7.4
"""
你需要为一家B2C(商业到消费者)零售商编写一个基础的电子商务引擎。你需要写一个针对顾客
的类User, 一个对应存货清单的类Item, 还有一个对应购物车的类叫Cart. 货物放到购物车里,
顾客可以有多个购物车。同时购物车里可以有多个货物,包括多个同样的货物。
"""
import pickle
import os
def prop(func):
return property(**func())
class Item(object):
def __init__(self):
self.nm='goods.db'
if not os.path.exists(self.nm):
self.fhandler=open(self.nm,'wb')
self.dict_item={}
else:
self.fhandler=open(self.nm,'rb')
self.dict_item=self.get_item()
print('items:',self.dict_item)
def __del__(self):
self.fhandler.close()
def goodsVerify(self,goods,num):
if goods in self.dict_item.keys() and num<=self.dict_item[goods]:
return True
def modify_item(self,item,num):
if 0>=num:
del self.dict_item[item]
elif item not in self.dict_item.keys():
self.dict_item[item]=num
with open(self.nm,'wb') as f:
pickle.dump(self.dict_item,f)
def get_item(self):
try:
self.dict_item=pickle.load(self.fhandler)
except Exception as e:
print(e)
return
if self.dict_item is None:
self.dict_item={}
return self.dict_item
def del_item(self):
if os.path.exists(self.nm):
os.remove(self.nm)
else:
print('Goods list is not existed!')
class Cart(object):
def __init__(self,account,cart):
self.nm='{}_{}.dbm'.format(account,cart)
if not os.path.exists(self.nm) or 0==os.path.getsize(self.nm):
self.fp=open(self.nm,'wb')
self.dict_cart={}
else:
self.fp=open(self.nm,'rb')
# print('size:',os.path.getsize(self.nm))
self.dict_cart = pickle.load(self.fp)
def toCart(self,goods,num):
if goods not in self.dict_cart.keys():
self.dict_cart[goods] = num
else:
self.dict_cart[goods]=self.dict_cart[goods]+num
self.saveAll()
print('%s:%s '%(self.nm,self.dict_cart))
def modifyCart(self,goods,num):
if 0>=num:
pass
else:
self.dict_cart[goods]=num
def delFrom(self,goods):
if goods not in self.dict_cart.keys():
pass
del self.dict_cart[goods]
def emptyCart(self):
del self.dict_cart
def saveAll(self):
with open(self.nm,'wb') as f:
pickle.dump(self.dict_cart,f)
import shelve
class User(object):
def __init__(self,account=None,pwd=None):
self._flag = False
self.account=account
self.dbm=shelve.open('user.dbm')
if account and pwd:
self.login(account,pwd)
def __del__(self):
self.dbm.close()
def register(self,account,pwd):
if account in list(self.dbm.keys()):
return
self.dbm[account]=pwd
print('register successfully!')
# self._flag=False
def login(self,account,pwd):
if not account in list(self.dbm.keys()):
self._flag=False
print('user [%s] is not exist.'%account)
return
if self.dbm[account] == pwd:
print('login successfully!')
self._flag=True
else:
self._flag=False
def loginout(self):
self._flag=False
def cart(self,cart):
if self._flag:
account=self.account
return Cart(account,cart)
else:
print('please login first!')
def to_Cart(self,item_obj,cart_obj,goods,num=1):
if self._flag:
if item_obj.goodsVerify(goods,num):
cart_obj.toCart(goods,num)
else:
print('please login first!')
def modify_Cart(self,item_obj,cart_obj,goods,num):
if self._flag:
if item_obj.goodsVerify(goods,num):
cart_obj.modifyCart(goods,num)
else:
print('please login first!')
if __name__ == '__main__':
inventory = Item()
inventory.modify_item('math book', 1)
inventory.modify_item('english book', 11)
inventory.modify_item('musci book', 21)
goods_list = inventory.dict_item
user_1=User('jojo','jojo')
# user_1.register('jojo','jojo')
print(user_1._flag)
# user_1.login('jojo','jojo')
cart1=user_1.cart('cart1')
user_1.to_Cart(inventory,cart1,'math book',2)
user_1.to_Cart(inventory, cart1, 'musci book', 2)
cart2 = user_1.cart('cart2')
user_1.to_Cart(inventory, cart2, 'english book', 1)
user_2=User('haohao','haohao')
# user_2.register('haohao','haohao')
ucart2=user_2.cart('ucart1')
ucart2.toCart('math book',1)
user_3=User()
print(user_3._flag)
cart3=user_3.cart('cart3')
user_3.to_Cart(inventory,cart3,'english book', 1)
运行结果:
items: {'math book': 1, 'english book': 11, 'musci book': 21}
login successfully!
True
jojo_cart1.dbm:{'math book': 6, 'musci book': 32}
jojo_cart2.dbm:{'english book': 18}
login successfully!
haohao_ucart1.dbm:{'math book': 7}
False
please login first!
please login first!
生成的文件:

Python3.7.4电子商务课后练习13-11解析
本文介绍了Python核心编程第二版中第十三章的一个课后练习,专注于13-11题目的解答,详细探讨了在Python3.7.4环境下处理电子商务问题的方法和技术。

被折叠的 条评论
为什么被折叠?



