#--coding:utf-8--
#Author yanzijian
'''
#1. 打开文件的模式有(默认为文本模式):
r ,只读模式【默认模式,文件必须存在,不存在则抛出异常】
w,只写模式【不可读;不存在则创建;存在则清空内容】
a, 之追加写模式【不可读;不存在则创建;存在则只追加内容】
"+"表示可同时读写某个文件:
1. r+打开用于读和写文件。文件指针置于该文件的开头
2. w+打开文件为写入和读取模式。如果文件存在覆盖现有文件。如果该文件不存在,创建用于读写操作的新文件。
3. a+打开文件为追加和读取方式。文件指针是在文件是否存在该文件的末尾。该文件以追加模式打开。如果该文件不存在,它将创建用于读写操作的新文件
#2. 对于非文本文件,我们只能使用b模式,"b"表示以字节的方式操作(而所有文件也都是以字节的形式存储的,使用这种模式无需考虑文本文件的字符编码、图片文件的jgp格式、视频文件的avi格式)
rb
wb
ab
注:以b方式打开时,读取到的内容是字节类型,写入时也需要提供字节类型,不能指定编码
用户入口
购物车商品信息存放在文件中
已购商品,余额记录
管理员入口
可以添加商品,修改商品价格,
'''
notice = '''
请输入您的用户名和密码,您可以输入管理员或者普通账号。
'''
print(notice)
user = {}
administrator = {}
product = []
state = True
with open ("product",'r',encoding='UTF-8') as f:
for item in f.readlines():
product.append(item.strip())
# print (product)
#获取到本地用户和管理员密码并编码
make_encode = str.maketrans('12','am')
make_decode = str.maketrans('am','12')
with open("normal_users",'r',encoding='UTF-8') as f:
for item in f.readlines():
# print (item)
user.update({(item.strip().split()[0]):[item.strip().split()[1].translate(make_encode),item.split()[2]]})
with open('administrator','r',encoding='UTF-8') as f:
for item in f.readlines():
administrator.update({(item.strip().split()[0]):item.strip().split()[1].translate(make_encode)})
# print(user)
# print(administrator)
username = input("please input your username: ")
password = input("please input your password: ")
for i in user:
# print(i)
if username == i:
if password == user[i][0].translate(make_decode):
print ("normal user login success!")
money = float(user[i][1])
print(money)
while state:
for item in product:
print (product.index(item),item.split(',')[0])
length = len(product)
while state:
choice = input("enter serial number in (%s) for example %s>x>=0: "%(length,length))
if choice.isdigit() and int(choice) <length and int(choice)>=0:
if money > float(product[int(choice)].split(',')[1]):
print ("Purchase success!")
print("cost:%s¥"%float(product[int(choice)].split(',')[1]))
money_2 = money - float(product[int(choice)].split(',')[1])
print("balance:%s¥"%money_2)
file_data = ""
with open("normal_users", 'r', encoding='UTF-8') as f:
for line in f:
if str(int(money)) in line:
line = line.replace(str(int(money)), str(int(money_2)))
file_data = file_data + line
with open("normal_users", "w", encoding="utf-8") as f:
f.write(file_data)
elif choice == 'q' or choice == 'Q':
print("exit success!")
state = False
else:
pass
for i in administrator:
# print(i)
# print(administrator[i].translate(make_decode))
if username == i:
if password == administrator[i].translate(make_decode):
print ("administrator login success!")
#功能1 修改余额
menu = '''
1.修改余额
2.修改用户密码
3.增加用户
'''
while state:
print(menu)
choice = input('please choice (1 or 2 or 3): ')
user_name = []
if choice == '1' :
for i in user:
print ('请选择修改的用户:',i)
user_name.append(i)
choice = input("请输入用户名: ")
if choice in user_name:
ch_money = int(input("请输入金额: "))
money = str(user[choice][1])
# print(money)
file_data = ""
with open("normal_users", 'r', encoding='UTF-8') as f:
for line in f:
if choice in line:
line = line.replace(str(int(money)), str(int(ch_money)))
file_data = file_data + line
with open("normal_users", "w", encoding="utf-8") as f:
f.write(file_data) and print("修改成功!")
if choice == '2' :
for i in user:
print('请选择修改的用户:', i)
user_name.append(i)
choice = input("请输入用户名: ")
if choice in user_name:
ch_passwd = input("请输入新密码: ")
old_passwd = str(user[choice][0].translate(make_decode))
# print(money)
file_data = ""
with open("normal_users", 'r', encoding='UTF-8') as f:
for line in f:
if choice in line:
line = line.replace(str(old_passwd), str(str(ch_passwd)))
file_data = file_data + line
with open("normal_users", "w", encoding="utf-8") as f:
f.write(file_data) and print("修改成功!")
if choice == '3':
new_name = input("新用户名: ")
new_passwd = input("新密码: ")
new_money = input("新余额: ")
new_info = '\n' + new_name + ' ' + new_passwd + ' ' + new_money
with open('normal_users', 'a', encoding='UTF-8') as f:
f.writelines(new_info) and print("增加成功!")
else:
state = False