购物车程序:
1、启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表
2、允许用户根据商品编号购买商品
3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
4、可随时退出,退出时,打印已购买商品和余额
5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示
6、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买
7、允许查询之前的消费记录
用户验证使用之前博文中的程序,进行一定改动。主要是传参和调用后续的购物车程序。此处不再描述。
购物车入口:
m_shop.py
#!/usr/bin/env python# -*- coding:utf-8 -*-# Author: Wang Yueimport mode_shop,sys,random,osdef show_me(shopcart,user_guest): # 打印购买结果: print("you choose:") # 加工输出买得东西列表:按照每种货物的,货物名称,购买数量,单价/小计 的方式输出 with open("shopping_cart_db.doo", mode="a+", encoding="utf-8") as shop_cart_db: for it in shopcart.get_items(): print( "item name:\033[1;35m {_it_name} \033[0m!,num:\033[1;35m {_num} \033[0m!,val:\033[1;35m {_val} \033[0m!/\033[1;35m {_vals} \033[0m!".format( _it_name=it[0].get_item_name(), _num=it[1], _val=it[0].get_item_value(), _vals=it[0].get_item_value() * it[1])) shop_cart_db.write("{shop_cart_id},{item_id},{_num}\n".format(shop_cart_id=shopcart.get_shopcart_id(), item_id=it[0].get_item_id(), _num=it[1])) # 输出购买总价格 # 输出兜兜里还剩下的钱数 with open("balance_db.doo", mode="a+", encoding="utf-8") as balance_db: print("your user_id is \033[1;35m {u_id} \033[0m!".format(u_id=user_guest.get_user_id())) print("all value is: \033[1;35m {_count} \033[0m!".format(_count=shopcart.get_count_val())) print("you surplus:\033[1;35m {_my} \033[0m!".format(_my=user_guest.get_user_val())) balance_db.write( "{u_id},{cart_id},{val}\n".format(u_id=user_guest.get_user_id(), cart_id=shopcart.get_shopcart_id(), val=user_guest.get_user_val()))if __name__ == "__main__": items=[] #货物实例数组,存放的各种货物实例,与类中定义的items意义不同 shopcart=mode_shop.shopping_cart() #初始化购物车实例 #初始化货物实例 with open("item_db.doo",mode="r",encoding="utf-8") as item_db: #从文件数据源中读出每个货物,初始化货物实例并放入items列表 item_db.flush() item_db.seek(0) for it_info in item_db.readlines(): it_info=it_info.strip() it_if=it_info.split(",") items.append(mode_shop.item(int(it_if[0]),it_if[1],int(it_if[2]))) user_guest=mode_shop.user_guest() #初始化一个用户 in_user_id=sys.argv[1] if in_user_id: #从装入之前的用户和购物车: if os.path.exists("balance_db.doo"): #装载用户之前的购物车情况,建立用户和购物车的关联 with open("balance_db.doo",mode="r",encoding="utf-8") as load_user_db: lu=load_user_db.readlines() for load_user in lu : load_user=load_user.strip() l_user=load_user.split(",") if in_user_id==l_user[0]: #仅仅读取一个用户 user_guest.set_user_id(l_user[0]) user_guest.set_shopcart_id(l_user[1]) user_guest.set_user_val(int(l_user[2])) break with open("balance_db.doo",mode="w",encoding="utf-8") as w_user_db: #装在后,需要将文件中,用户的涉及到的行删掉 for line in lu: if str(user_guest.get_user_id()) in line: continue w_user_db.write(line) if user_guest.get_user_id() != 0: #如果从用户余额文件中存在用户,则开始关联购物车,关联后,就删除文件中的信息 if os.path.exists("shopping_cart_db.doo"): with open("shopping_cart_db.doo",mode="r",encoding="utf-8") as cart_db: c_lines=cart_db.readlines() for cl in c_lines: c_info=cl.split(",") if user_guest.get_shopcart_id() == c_info[0]: #读取全部购物车信息 shopcart.set_shopcart_id(c_info[0]) for item in items: if int(c_info[1]) == item.get_item_id(): shopcart.set_oneappend_item(item,int(c_info[2])) with open("shopping_cart_db.doo",mode="w",encoding="utf-8") as w_cart_db: for line in c_lines: if shopcart.get_shopcart_id() in line: continue w_cart_db.write(line) else: pass if user_guest.get_shopcart_id() == 0: #生成随机id,用于描述关联, shopcart.set_shopcart_id("cart_{cart_id}".format(cart_id=random.randint(1000,9999))) user_guest.set_user_id(sys.argv[1]) #描述用户和购物车的关联,就是让用户推个购物车去采购。 user_guest.set_shopcart_id(shopcart.get_shopcart_id()) #开始业务,输入逛超市带的钱数 have_val = int(input("your val:")) user_guest.set_user_val(have_val) int_error = False # 初始化一个标记,是否进行业务 while not int_error: for it in items: #输出每个货物的信息,展示给用户 print("id:{_id},{_name},val: {_val}".format(_id=it.get_item_id(),_name=it.get_item_name(),_val=it.get_item_value())) it_id=input("which one is you choice(id) or view or quit:")#接收想要购买的货物id,不是数字就退出,不是id就重新输入,是id进行业务 if it_id.isdigit(): it_id=int(it_id) #尝试转为整形 elif it_id == "view": show_me(shopcart, user_guest) else: int_error=True if int_error: #如果输入的无法被转为数字,则认定是想结束程序,以下是结束过程 show_me(shopcart,user_guest) sys.exit(0) for it in items: #在货物堆中找出想要买得货物 if it.get_item_id()== it_id: it_num = input("how many {it_name} would you buy(int):".format(it_name=it.get_item_name())) if it_num.isdigit(): it_num = int(it_num) else: print("you need input a number(int)") break if user_guest.get_user_val() < it.get_item_value()*it_num: #判定兜兜里的钱是否够买 print("your money is not enough!!!!") break user_guest.set_user_val(user_guest.get_user_val()-it.get_item_value()*it_num) #钱够的话就买 shopcart.set_oneappend_item(it,it_num) #加入到购物车
定义类的文件mode_shop.py,货物类、购物车类、用户类:
#!/usr/bin/env python# -*- coding:utf-8 -*-# Author: Wang Yueclass shopping_cart(object): def __init__(self): self.items=[] #定义的二维数组,存放内容的单元素为[item,num] self.count_value=0 self.id=0 def set_oneappend_item(self,item,num): #加入一个货物,如果加过,就仅累加该货物的数量,否则加入新的元素。 for its in self.items: if its[0].get_item_id() == item.get_item_id(): its[1]=num+its[1] break else: self.items.append([item,num]) def get_items(self): #获取购物车上的货物,及数量,就是那个二维数组 return self.items def get_count_val(self):#获取购物车上当前的总价格,二维数组中的定义是货物和数量,所以算法如下。太粗暴了 for item in self.items: self.count_value=self.count_value+item[0].get_item_value()*item[1] return self.count_value def set_shopcart_id(self,sc_id): self.id=sc_id def get_shopcart_id(self): return self.idclass item(object): #货物类 def __init__(self,it_id,it_name,it_value): self.item_id=it_id #货物id self.item_name=it_name #货物名称 self.item_value=it_value #货物单价 def set_item_name(self,it_name): self.item_name=it_name def get_item_name(self): return self.item_name def set_item_value(self,it_val): self.item_value=it_val def get_item_value(self): return self.item_value def set_item_id(self,it_id): self.item_id=it_id def get_item_id(self): return self.item_idclass user_guest(object): def __init__(self): self.user_id=0 self.shopcart_id=0 self.val=0 def set_user_id(self,u_id): self.user_id=u_id def get_user_id(self): return self.user_id def set_shopcart_id(self,sc_id): self.shopcart_id=sc_id def get_shopcart_id(self): return self.shopcart_id def set_user_val(self,val): self.val=val def get_user_val(self): return self.val
附:用的文件作为数据源:
存放购物车货物存放信息shopping_cart_db.doo:
cart_4871,3,6cart_4871,4,6cart_4871,3,6cart_4871,4,6
存放货物信息item_db.doo:
1,iphone,100002,bag,3003,book,504,note,55,smoke,86,box,107,fire,18,table,30009,pc,7000
存放已经存放货物关联信息balance_db.doo:
user_2592,cart_4871,49550user_2592,cart_4871,49550
维护商品的脚本:supply_item.py
#!/usr/bin/env python# -*- coding: utf-8 -*-# author : Wang Yueimport syswhile True: print('''cmd: view : all items insert : id(int),name(str),price(int) insert one into DB update: id(int),name(str),price(int) update one from DB delete: id(int) delete one from DB quit: quit me ''') cmd=input("cmd>> ") command=cmd.split() if command[0] == "view": with open("item_db.doo",mode="r",encoding="utf-8") as view_db: lines=view_db.readlines() for line in lines: line=line.strip() print(line) elif command[0] == "insert": with open("item_db.doo",mode="a+",encoding="utf-8") as apd_db: item=input("item info:") it_info=item.split(",") if len(it_info) == 3: apd_db.write("{id},{name},{prc}\n".format(id=it_info[0],name=it_info[1],prc=it_info[2])) else: continue elif command[0] == "update": with open("item_db.doo",mode="r",encoding="utf-8") as r_it_db: lines=r_it_db.readlines() info=input("item info>> (id,name,prc):") info=info +"\n" info_id=info.split(",") is_break=0 for line in lines: line_old = line.split(",") if info_id[0] != line_old[0]: is_break=1 else: is_break=0 break if is_break==1: print("not found it from DB") continue with open("item_db.doo",mode="w",encoding="utf-8") as w_it_db: for line_n in lines: if info_id[0] == line_n[0]: line_n=info w_it_db.write(line_n) elif command[0] == "delete": with open("item_db.doo",mode="r",encoding="utf-8") as dr_it_db: lines=dr_it_db.readlines() d_info=input("input item's id:") with open("item_db.doo",mode="w",encoding="utf-8") as dw_it_db: for line_n in lines: line_n_l=line_n.split(",") if line_n_l[0]==d_info: continue dw_it_db.write(line_n) elif command[0] =="quit": sys.exit(0) break else: continue