baili-luoyun
  1 # -*- coding: utf-8 -*-
  2 # @Time    : 2018/12/23 13:46
  3 # @Author  : Endless-cloud
  4 # @Site    : 
  5 # @File    : 重写购物车项目.py
  6 # @Software: PyCharm
  7 \'\'\'
  8 0 首先声明在任何地方输入q 或者Q 退出系统
  9 1 首先读取文件夹中的账号密码  Y 继续, N 继续输入账号密码
 10 2 让用户判断查询消费记录 输入C 或者c ,  Y 2.1  ,N 3
 11     2.1  打印消费,余额
 12 3 打印余额,让用户判断是否输入 金额 ,Y 继续  , N 继续
 13     3.1 余额充值  ,提示充值成功 ,继续4
 14 4 打印系统商品列表
 15 5 让用户输入 想要选择的商品编号 (扩展.商品名称)
 16 6 打印用户购买的商品名字 ,以及账户余额
 17 7 询问客户是否继续购买商品  Y  , N
 18     7.1  走流程4  ,5 , 6
 19     7.2 走流程 0
 20 8 如果推出系统打印出客户已经购买的所有物品 , 以及余下金额
 21 #  所有余额,商品内容用特殊字标注
 22 \'\'\'
 23 """
 24 准备 :
 25         描述             函数名称
 26 1      存储基础属性       __init__    
 27 
 28         描述              名称           类型
 29 1   已经购买的商品列表   shop_listshop    list
 30 2   存储用户的账号密码   popmsg           dict                 
 31 3   存储用户的余额      ymoney            str->int 
 32 """
 33 
 34 
 35 class shop(object):
 36     def __init__(self):
 37         self.shop_listshop = []
 38         self.popmsg = {  # 待升级用读取txt
 39             \'wzh\': \'123\'
 40         }
 41         self.ymoney = \'\'
 42         self.goodgifts = [
 43             {"name": "电脑", "price": 1999},
 44             {"name": "鼠标", "price": 10},
 45             {"name": "游艇", "price": 20},
 46             {"name": "美女", "price": 998},
 47         ]
 48         self.msg =\'\'
 49 
 50     # 如果用户输入q 那么 退出系统
 51     def close(self, char):
 52         if char.upper() == \'Q\':
 53             self.save()
 54             quit()
 55 
 56     # 登录, 有3次判断机会,如果用完了退出系统
 57     def login(self):
 58         count = 3
 59         while 0 < count:
 60 
 61             username = input(\'请输入username\')
 62             password = input(\'请输入password\')
 63             with open(\'login.txt\', \'r\', encoding=\'utf-8\') as f:
 64                 l = f.readlines()  # readlines返回list
 65                 neirong = eval(l[0])
 66 
 67                 if neirong.get(username) == password:  # 利用字典的get()获取password判断
 68                     print(\'欢迎进入购物系统\')
 69                     break
 70 
 71                 elif neirong.get(username) != password:
 72                     count = count - 1
 73                     print(\'你还有%s次机会\' % (count))
 74         else:
 75             print(\'机会用完\')
 76             exit()
 77 
 78     # 打开money.txt ,读取第一行 ,利用readline() 获取内容(str). 在str 类型转换int类型,然后返回salay
 79     def money(self):
 80         with open(\'money.txt\', \'r\', encoding=\'utf-8\') as f:
 81             salay = int(f.readline())
 82         return salay
 83 
 84     # 根据用户的金额进行判断充值,
 85     def money_chioce(self, salay):
 86         print(\'你当前的余额为\' + str(salay) + \'\')
 87 
 88         chongzhi = input(\'是否充值1键 ,或直接消费其他除q,c键 ,,输入q退出,c进入已经购买列表\')
 89         if chongzhi == \'1\':
 90             jine = int(input(\'请输入充值金额\'))
 91             if jine <= 0:
 92                 endsalay = salay + int(jine)
 93                 salay = salay + int(jine)
 94                 print(\'最少充值1元\')
 95 
 96 
 97             elif jine > 0:
 98                 print(\'感谢你的充值\')
 99                 endsalay = salay + int(jine)
100                 with open(\'money.txt\', \'w\', encoding=\'utf-8\') as f:
101                     f.writelines(str(endsalay))
102             print(\'你当前的余额为\' + str(endsalay))
103             self.close(chongzhi)
104         elif chongzhi==\'c\':
105             self.look_pay_list()
106             print(\'当前余额为%s\'%(self.money()))
107         elif chongzhi==\'q\':
108             self.close(chongzhi)
109 
110     # 让用户输入c ,打印出来列表中的商品
111     def look_pay_list(self):
112         with open(\'shaop.txt\', \'r\', encoding=\'utf-8\') as f:
113             print(\'%s   %s  %s\' % (\'编号\', \'名字\', \'数量\'))
114             ll = f.readlines()
115             if len(ll)==0:
116                 print(\'当前没有任何内容\')
117             else:
118                 s = (eval([i for i in ll][0]) )
119             # print(type(s))
120             # print(type(eval([i for i in ll][0])[0]))
121 
122                 for i, j in enumerate(s):
123                     print(str(i) + \'   \' + j[\'name\'] + \'  \' + str(j[\'number\']))
124             # print(type(j))
125 
126 
127     #
128     def goods(self):
129         goods = self.goodgifts
130         titie1 = \'购物系统\'
131         print(titie1.center(50, \'*\'))
132         print(\'%s   %s  %s\' % (\'编号\', \'名字\', \'金额\'))
133         for i, j in enumerate(goods):
134             print(\'%s    %s    %s\' % (i + 1, j[\'name\'], j[\'price\']))
135     def txt_goods(self):
136         with open(\'shaop.txt\', \'r\', encoding=\'utf-8\') as f:
137             self.msg=f.readlines()
138             l =eval(self.msg[0])
139             self.shop_listshop =l
140             # print(type(self.shop_listshop[0]))
141             # print(self.msg)
142             # print(l)
143             # print(type(l[0]))
144 
145     def chiose_goods(self):
146 
147         while True:
148             chiose2 = input(\'请输入购买的商品编号\')
149             if chiose2 == \'q\':
150                 self.close(chiose2)
151             elif chiose2==\'c\':
152                 self.look_pay_list()
153             elif int(chiose2) in [i + 1 for i, j in enumerate(self.goodgifts)]:
154                 print(\'你购买的物品是%s ,花了%s\' % (self.goodgifts[int(chiose2) - 1][\'name\'], self.goodgifts[int(chiose2) - 1][\'price\']))
155                 goots_dic = [self.goodgifts[int(chiose2) - 1]][0]
156                 if self.goodgifts[int(chiose2) - 1][\'name\'] in [j[\'name\'] for i, j in enumerate(self.shop_listshop)]:
157                     self.shop_listshop[int(chiose2) - 1][\'number\'] = self.shop_listshop[int(chiose2) - 1][\'number\'] + 1
158                     print(\'yes\')
159                 else:
160 
161                     self.shop_listshop.append(goots_dic)
162                     self.shop_listshop[int(chiose2) - 1][\'number\'] = 1
163                     print(\'no\')
164             elif chiose2 not in [i + 1 for i, j in enumerate(self.goodgifts)]:
165                 print(\'没有这个商品编号\')
166             print(self.shop_listshop)
167 
168     def save(self):
169         with open(\'shaop.txt\', \'w\', encoding=\'utf-8\') as f:
170             f.writelines(str(self.shop_listshop))
171         self.look_pay_list()
172 
173 
174 shop = shop()
175 
176 shop.login()
177 
178 l = shop.money()
179 shop.money_chioce(l)
180 shop.goods()
181 shop.chiose_goods()

 



With great power comes great responsibility

分类:

技术点:

相关文章: