#(全局变量)用户余额信息
shopping = {'orange': 30, 'milk': 50, 'bike': 200, 'lipstick': 350, 'bag': 3000, 'car': 100000, 'house': 1200000}#主程序
if __name__ == '__main__':print '\n\t\t\t\t'+change_color.HEADER+'Welcome to Internet Bank!'+change_color.ENDC #欢迎界面
login = 1 #为0表示已经登录进去了,为1表示尚未登录成功
while login: #登录主程序
user_admin = raw_input('\n\t\t\t'+change_color.OKBLUE+'Please input your admin name:'+change_color.ENDC) #输入帐号
if admin_is_locked(user_admin): #判断帐号是否被锁定
if admin_is_exist(user_admin): #判断帐号是否存在
times = 3
while times:#可以输入密码三次
pass_word = raw_input('\n\t\t\t'+change_color.OKBLUE+'Please input your password:'+change_color.ENDC) #输入密码
if password_is_match(user_admin, pass_word): #密码正确,打印账户资料
print '\n\t\t\t\t'+change_color.HEADER+'Welcome!'+change_color.ENDC
login=0break
else:
times-= 1
print '\n\t\t\t'+change_color.WARNING+"Password do not match! You still have"+str(times)+"times to try!" +change_color.ENDCif times == 0: #输入密码三次不正确,锁定用户
lock_user(user_admin)print '\n\t\t\t'+change_color.WARNING+"%s is locked" %user_admin +change_color.ENDC
sys.exit()#网上银行界面
if login ==0:
create_list_in_accout(user_admin)
create_dict_in_balance()
user_interface= 1
whileuser_interface:print '\n\t\t\t'+change_color.OKBLUE+'''#################################################
user name: %s
user balance: %s
Operation:
1.withdraw cash
2.shopping
3.month account
4.exit
#################################################''' % (user_admin, read_balance(user_admin)) +change_color.ENDC#用户选择要进行的操作
operation = input('\n\t\t\t'+change_color.OKBLUE+'Please input the number of operation(1/2/3/4):'+change_color.ENDC)if operation == 1: #提现操作
cash_interface = 1
whilecash_interface:
cash= input('\n\t\t\t'+change_color.OKBLUE+'Please input cash you want:'+change_color.ENDC)
user_balance=read_balance_dict()if user_balance[user_admin] >= caculate_cash_with_fee(cash): #提现的钱不超过余额
if cash <= 15000: #小于额度
balance = balance_caculate(cash, user_balance[user_admin]) #计算新的余额
print '\n\t\t\t'+change_color.OKGREEN+'You can take your cash! Your card still have %s:' % balance +change_color.ENDC#将余额写入文档
save_balance(user_admin, balance)#将流水写入文档(做成函数)
save_current_accout(user_admin, 'withdraw cash', cash, balance)else:print '\n\t\t\t'+change_color.WARNING+'You can not take more than 15000!'+change_color.ENDCelse:print '\n\t\t\t'+change_color.WARNING+'You balance is not enough!'+change_color.ENDC#选择是否继续
choice = raw_input('\n\t\t\t'+change_color.OKBLUE+'Do you want to continue?(y/n):'+change_color.ENDC)if choice == 'y': #继续提现
continue
else: #返回主界面
cash_interface =0if operation == 2: #购物车系统
buy_count = {} #存放已经买的东西的种类
buy_counts = [] #所有东西都直接加到购物车中
buy_balance =read_balance(user_admin)
can_buy= 1
whilecan_buy:print '\n\t\t\t\t'+change_color.OKBLUE+'Welcome for shopping!'+change_color.ENDCprint
print '\n\t\t\t\t'+change_color.OKBLUE+'****************************************'+change_color.ENDCfor i in enumerate(sorted(shopping.items(), key = lambda item:item[1]), 1): #将字典中的元素转换成元组排序,编号并输出
print '\t\t\t\t'+change_color.OKBLUE+'%d %s %d'% (i[0], i[1][0], i[1][1]) +change_color.ENDCprint '\t\t\t\t'+change_color.OKBLUE+'****************************************'+change_color.ENDC#读取用户余额
print '\n\t\t\t'+change_color.OKBLUE+'You balance: %d' % buy_balance +change_color.ENDC#用户选择要进行的操作
buy = input('\n\t\t\t'+change_color.OKBLUE+'Please input the number of goods you want to buy(1/2/3/4/...):'+change_color.ENDC)
shopping_list= sorted(shopping.items(), key=lambda item: item[1])if buy_balance > shopping_list[buy - 1][1]: #工资卡里的钱可以购买这样东西
#把东西放到购物车里
buy_counts.append(shopping_list[buy - 1][0])print '\n\t\t\t'+change_color.OKGREEN+'%s is added into you shopping car' % shopping_list[buy - 1][0] +change_color.ENDC#计算新的余额
buy_balance -= shopping_list[buy - 1][1]else: #钱不够, 使用信用额度
credit = raw_input( '\n\t\t\t'+change_color.WARNING+'Balance is not enough! Using credit(y/n)?'+change_color.ENDC)if credit =='y':#把东西放到购物车里
buy_counts.append(shopping_list[buy - 1][0])print '\n\t\t\t'+change_color.OKGREEN+'%s is added into you shopping car' % shopping_list[buy - 1][0] +change_color.ENDC#计算新的余额
buy_balance -= shopping_list[buy - 1][1]
choice= raw_input('\n\t\t\t'+change_color.OKBLUE+'Continue?(y/n)'+change_color.ENDC) #是否继续
if choice == 'y':continue
if choice == 'n':
finish= 1
whilefinish:print '\n\t\t\t'+change_color.OKBLUE+'''#################################################
Operation:
1.watch shopping car
2.add
3.reduce
4.accout
5.exit to user interface
6.exit system
#################################################''' +change_color.ENDC#用户选择要进行的操作
buy_operation = input('\n\t\t\t'+change_color.OKBLUE+'Please input the number of operation(1/2/3/4/5):'+change_color.ENDC)if buy_operation == 1: #查看购物车
#购物车列表转换为集合去重
buy_count =set(buy_counts)
buy_list=list(buy_count)print '\n\t\t\t\t'+change_color.OKBLUE+'number name price amount' + change_color.ENDC #打印选购的商品价格和数量
for i in enumerate(buy_list, 1):print '\t\t\t\t'+change_color.OKBLUE+'%s %s %d %d' % (i[0], i[1], shopping[i[1]], buy_counts.count(i)) +change_color.ENDCprint '\n\t\t\t'+change_color.OKGREEN+'All above is: %d' % (read_balance(user_admin) - buy_balance) +change_color.ENDC #打印总价
print '\n\t\t\t'+change_color.WARNING+ '10s later will come back to shopping operate interface' +change_color.ENDC
time.sleep(10)if buy_operation == 2: #增加购物车里的东西
finish =0if buy_operation == 3: #减去购物车里的东西
undo_finish = 1
whileundo_finish:#读取用户余额
print '\n\t\t\t'+change_color.OKBLUE+'You balance: %d' % buy_balance +change_color.ENDC#打印购物车
buy_count =set(buy_counts)
buy_list=list(buy_count)print '\n\t\t\t\t'+change_color.OKBLUE+'number name price amount' + change_color.ENDC #打印选购的商品价格和数量
for i in enumerate(buy_list, 1):print '\t\t\t\t'+change_color.OKBLUE+'%s %s %d %d' % (i[0], i[1], shopping[i[1]], buy_counts.count(i)) +change_color.ENDCprint '\n\t\t\t'+change_color.OKGREEN+'All above is: %d' % (read_balance(user_admin) - buy_balance) +change_color.ENDC #打印总价
#用户选择要进行的操作
undo = input('\n\t\t\t'+change_color.OKBLUE+'Please input the number of goods you want to reduce(1/2/...):'+change_color.ENDC)if buy_list[undo - 1] in buy_counts: #如果商品在购物车中,删除
buy_counts.remove(buy_list[undo - 1])print '\n\t\t\t'+change_color.OKGREEN+'%s is delete from you shopping car' % shopping_list[undo - 1][0] +change_color.ENDC#计算新的余额
buy_balance += shopping[buy_list[undo - 1]]else: #购物车里没有该物品
print '\n\t\t\t'+change_color.WARNING+'%s is not in you shopping car' % buy_list[undo - 1]+change_color.ENDC
undo_choice= raw_input('\n\t\t\t'+change_color.OKBLUE+'Continue?(y/n)'+change_color.ENDC) #是否继续
if undo_choice == 'y':continue
if undo_choice == 'n':
undo_finish=0if buy_operation == 4: #结算
buy_count =set(buy_counts)
buy_list=list(buy_count)print '\n\t\t\t\t'+change_color.OKBLUE+'name price amount' + change_color.ENDC #打印选购的商品价格和数量
for i inbuy_list:print '\t\t\t\t'+change_color.OKBLUE+'%s %d %d'% (i, shopping[i], buy_counts.count(i)) +change_color.ENDC
total= read_balance(user_admin) -buy_balanceprint '\n\t\t\t'+change_color.OKGREEN+'All above is: %d' % total +change_color.ENDC #打印总价
pay= raw_input('\n\t\t\t'+change_color.OKGREEN+'Do you want to pay(y/n)?'+change_color.ENDC)if pay == 'y': #确认付款,将流水和余额写入文件
#余额
save_balance(user_admin, buy_balance)print '\n\t\t\t'+change_color.OKGREEN+'Successful payment!'+change_color.ENDCprint '\n\t\t\t'+change_color.OKBLUE+'You balance: %d' % buy_balance +change_color.ENDC#流水写入文件
save_current_accout(user_admin, 'shopping', total, buy_balance)
finish= 0 #返回主界面
can_buy =0
time.sleep(3)if pay == 'n': #如果不付款,返回商品操作界面
can_buy =0if buy_operation == 5: #退回主界面
print '\n\t\t\t\t'+change_color.HEADER+'Thanks for Internet shopping!'+change_color.ENDC
finish=0
can_buy=0if buy_operation == 6: #退出
print '\n\t\t\t\t'+change_color.HEADER+'Thanks for Internet shopping!'+change_color.ENDC
sys.exit()if not (buy_operation == 1 or buy_operation == 2 or buy_operation == 3 or buy_operation == 4 or buy_operation == 5 or buy_operation == 6):print '\n\t\t\t'+change_color.WARNING+'You have to input number as(1/2/3/...)'+change_color.ENDC
time.sleep(3)if operation == 3: #查看流水账操作 #'dict' object has no attribute 'readline
print_user_accout(user_admin)print '\n\t\t\t'+change_color.WARNING+'10s later will come back to user interface'+change_color.ENDC
time.sleep(10)if operation == 4: #退出系统
print '\n\t\t\t\t'+change_color.HEADER+'Thanks for using Internet Bank!'+change_color.ENDC
sys.exit()if not (operation == 1 or operation == 2 or operation == 3 or operation == 4):print '\n\t\t\t'+change_color.WARNING+'You have to input number as(1/2/3/...)'+change_color.ENDC
time.sleep(3)