#!/usr/bin/env pythonAuthor: Created by soulboyFunction: Shopping with a credit card
import sys
import os
import pickle
salary = int(raw_input("Inpuet your salary:").strip()) #初始化每次购物的现金
def dump(): #把pickle.dump方法定义为函数方便调用
output = file('data.pkl','wb')
pickle.dump(maxoverdraft,output) #信用卡最大透支额度
pickle.dump(available,output) #可用额度
pickle.dump(overdraft,output) #亏欠额度
pickle.dump(bill,output) #用bill字典来保存以日期为Key保存信用卡的明细
output.close()
f = str(os.path.isfile('data.pkl')) #定义pickle的文件data,用来保存序列化数据对象
if 'False' in f: #如果程序是首次运行,会初始化如下对象并保存至当前目录data.pkl文件中
maxoverdraft = 15000
available = 15000
overdraft = 0
bill = {}
dump()
def load(): #从data.pkl文件中按顺序读取序列化数据对象并重新赋值
global maxoverdraft #声明全局变量方便在局部变量在函数外调用
global available
global overdraft
global bill
pkl_file = file('data.pkl','rb')
maxoverdraft = pickle.load(pkl_file)
available = pickle.load(pkl_file)
overdraft = pickle.load(pkl_file)
bill = pickle.load(pkl_file)
pkl_file.close()
def information(): #定义信息方便各功能模块重复调用
print '''
maxoverdraft = %s $ #最大透支额度
available = %s $ #可用额度
overdraft = %s $''' %(maxoverdraft,available,overdraft) #亏欠额度
def welcome(): #主菜单函数
print '''[1]:Go shopping !
'''
def authentication(): #密码认证函数,本程序中信用卡号为:soulboy,密码为:soulboy
while True:
count = 0
account = raw_input("Please input your username:")
if account == 'soulboy':
password = raw_input("Please input your password:")
if password == 'soulboy':break
while password != 'soulboy': #实时显示剩余密码重试次数
count += 1
chance = 3 - count
password = raw_input("Error password and try again,you have %s chance:"%(chance))
if count == 3:break
if count == 3:continue
else:break
else:
print "Sorry, the %s is not exist and please check yourself:" % account
while True:
welcome()
print "Your Salary: %s $" % salary
option = raw_input("Based on the digital option:").strip()
if option == '1':
shoplist = []
while True:
products = ['house','car','phone','computer','clothes'] #商品列表中的元素依次与价格列表中的元素一一对应
pirce = [600000,200000,4000,8000,200]
print "########################## The list of goods ##########################"
for p in products:
print "%s \t %s" %(p,pirce[products.index(p)]) #通过商品列表的坐标取出商品对应的价格
choice = raw_input("please choice by it's name:")
result = products.count(choice)
if choice == 'quit':
print '''
Your shopping list: %s
Remaining salary: %s
welcome to next shopping #################################################################''' % (shoplist,salary) #显示购物列表、剩余现金
break
elif result == 0: #商品名称不存在提示重输
print " %s not in goods list, input right name:" % choice
continue
pgoods = pirce[products.index(choice)]
if pgoods > salary: #当商品价格大于现金
selection = raw_input("not engouh money for %s, you will use credit card ?[yes] or [no]:" %(choice)) #是否刷卡
if selection == 'yes':
authentication() #安全认证
load() #读取data.pkl文件中
total = salary + available
if total >= pgoods: #当信用卡和现金大于商品价格时
date = raw_input("Input your date:") #输入交易日期
event = choice #事件
interest = 0 #利息
data = pgoods - salary #数据
bill[date] = [event,data,interest] #以日期key其他元素为value保存至字典中
overdraft = overdraft + data
available = available - pgoods + salary
dump() #保存至data.pkl文件中
shoplist.append(choice) #添加商品至购物列表
salary = 0 #现金清0
print '''%s add your shopping list %s
salary left:%s $
credit car left: %s $''' %(choice,shoplist,salary,available)
else: #钱不够
print "Sorry not engouh money ! "
else:
salary = salary - pgoods #现金大于产品价格
shoplist.append(choice)
print "%s add your shopping list %s ,and your money left:%s $" %(choice,shoplist,salary)
elif option == '2': #提现金
authentication()
date = raw_input("Input the date:") #日期
while True:
load()
information()
print '''Your Salary: %s $
withdrawal:[yes]
quit:[no]''' % salary
select = raw_input("your choice:[yes] or [no]") #是否继续
if select == 'no':break
withdrawal = int(raw_input("Input the withdrawal amount and the fee is 10%:"))
if withdrawal > available: #提取金额不能大于可用金额
print "Beyond the maximun amount:"
continue
else:
salary += withdrawal #现金增加
data = withdrawal + withdrawal / 10
interest = withdrawal / 10 #手续费为提取金额的10%
available = available - data
event = "remove"
overdraft += data
bill[date] = [event,data,interest]
dump() #和之前一样
print "The sucess of the transaction and you salary: %s $" % salary
continue
elif option == '3': #还钱接口
date = raw_input("Input the date:") #日期
authentication()
while True:
load()
information()
print '''Your Salary: %s $
repayment:[yes]
quit:[no]''' % salary
select = raw_input("your choice:[yes] or [no]")
if select == 'no':break
repayment = int(raw_input("Input the repayment amount:"))
if repayment > salary: #还钱金额不能大于你现金
print "You don't have so much!"
elif repayment > overdraft: #还钱金额最多等于亏欠额度
excess = repayment - overdraft
salary = salary - repayment + excess
event = "back"
data = -overdraft
interest = 0
available += overdraft
overdraft -= overdraft
bill[date] = [event,data,interest]
dump()
else: #正常情况下
salary = salary - repayment
available += repayment
overdraft -= repayment
event = "back"
data = -repayment
interest = 0
bill[date] = [event,data,interest]
dump()
elif option == '4': #查看信用卡明细
load()
print '''#######################################################################
DATE EVENT DATA INTEREST'''
for k,v in bill.items():
print "%s %s %s %s" %(k,v[0],v[1],v[2])
information()
print "#######################################################################"
dump()
elif option == '5': #退出