题目:模拟实现一个ATM + 购物商城程序额度 15000或自定义实现购物商城,买东西加入 购物车,调用信用卡接口结账可以提现,手续费5%
所有的文件如下:
下面详细列出文件内容:
atm.py
#!/usr/bin/env python
import pickle,sys,os
import tab
import logger
products = {}
shopList = []
num = []
dd = ['01','02','03','04','05','06','07','08','09','10','11','12']
####################################################################
def shop():
print "\n\033[;32m------Welcome to Shopping System--------\033[0m"
f = open('shop','r+')
fp_line = f.readlines()
for line in fp_line:
f_line = line.split()
list = f_line[0]
price = f_line[1]
print list,price
products[list] = price
f.close()
while True:
choices = raw_input("Please enter the purchase of products or back (b): ").strip()
if len(choices) == 0:continue
elif choices == 'b' or choices == 'back':menu()
if products.has_key(choices):
f = open('user_info.txt','r')
balance = pickle.load(f)
f.close()
if int(products[choices]) < int(balance[authuser][2]):
while True:
shopList.append(choices)
New_balance = int(balance[authuser][2]) - int(products[choices])
balance[authuser][2] = New_balance
f = open('user_info.txt','w+')
pickle.dump(balance,f)
f.close()
logger.record_log(authuser,choices,'buy',products[choices])
print "\033[;32m you spend it %s, the rate of money is %s\033[0m" % (products[choices],New_balance)
print "\033[;33mThe shopping list %s \033[0m" % shopList
break
else:
print "\033[;31mYou choose %r not in the list\033[0m" % choices
shop()
else:
print "\033[;31mYou choose %r not in the list\033[0m" % choices
shop()
#####################################################################
def queryMoney():
f = open('user_info.txt','r')
balance = pickle.load(f)
f.close()
limit = balance[authuser][1]
bal = balance[authuser][2]
print "You can overdraft is \033[;31m %s\033[0m, you have waste the \033[;31m %s\033[0m\n" % (limit,bal)
####################################################################
def saveMoney():
while True:
print "Welcome to reimbursement,you must get salary today @_@!!"
save_type = raw_input('Please input you want to do(eg:savemoney):')
if len(save_type) == 0:continue
try:
input = int(raw_input('Please enter the amount of money:'))
except ValueError:
print "\033[;31mYou entered must be number!!\033[0m"
saveMoney()
if input % 100 != 0:
print "\033[;31mDeposit amount must be In multiples of one hundred\033[0m"
continue
f = open('user_info.txt','r')
userinfo = pickle.load(f)
f.close()
new_balance = int(userinfo[authuser][2]) + input
userinfo[authuser][2] = new_balance
f = open('user_info.txt','w+')
pickle.dump(userinfo,f)
f.close()
print "\033[;32mYour credit is %r,Your balance is %r\033[0m" % (userinfo[authuser][1],userinfo[authuser][2])
logger.record_log(authuser,save_type,"+%d" % float(input))
explain = raw_input("continue ('c'),return ('b'),quit ('q')")
if explain == 'c':continue
elif explain == 'b':menu()
else:sys.exit()
#####################################################################
def drawMoney(expense_type):
global intrest
intrest = 0
f = open('user_info.txt','r+')
userinfo = pickle.load(f)
f.close
while True:
cost_amount = raw_input('Please input you want to withdarw the money:')
old_position = userinfo[authuser][2]
print old_position,cost_amount
if len(cost_amount)==0:continue
if int(cost_amount) % 100 != 0:
print "Please enter an integer money!!!"
continue
if int(cost_amount) < int(old_position):
if expense_type == 'withdraw':
intrest = int(cost_amount) * 0.05
new_position = int(old_position) - int(cost_amount) - int(intrest)
userinfo[authuser][2] = new_position
print "you have the $%d in you credit card" % new_position
logger.record_log(authuser,expense_type,"-%d" % float(cost_amount),intrest)
f = open('user_info.txt','w+')
pickle.dump(userinfo,f)
f.close()
explain = raw_input("continue ('c'),return ('b'),quit ('q')")
if explain == 'c':continue
elif explain == 'b':menu()
else:sys.exit()
else:
print "no enough money!!!"
sys.exit()
#####################################################################
def queryList():
while True:
month = raw_input('Please input you check the month(01-12) and quit(q):')
if month in dd:
num = os.system("grep -E \'^[0-9]{4}-%02s-[0-9]{2}|\'%s\'' account.log" % (month,authuser))
if int(num) == 256:
print "There is can't find the %s month's list" % month
continue
else:
print "Please input the right format(eg:01 02 03...)"
continue
if month == 'q'or month == 'quit':
break
menu()
#####################################################################
def menu():
print "\033[;32mWelcome the ATM System , you can use credit card, GO GO GO !!!\033[0m\n"
print "\t(1) shop"
print "\t(2) query money"
print "\t(3) save money"
print "\t(4) draw money"
print "\t(5) query List"
print "\t(6) quit"
while True:
choices = raw_input("Please choices the function:").strip()
if len(choices) == 0:continue
if choices == '1':shop()
elif choices == '2':queryMoney()
elif choices == '3':saveMoney()
elif choices == '4':drawMoney('withdraw')
elif choices == '5':queryList()
elif choices == '6':sys.exit()
else:continue
######################################################################
fp = open('user_info.txt','r+')
userinfo = pickle.load(fp)
fp.close()
while True:
authuser = raw_input("\033[;32mPlease input the credit card's Username:\033[0m").strip()
if len(authuser) == 0:continue
if userinfo.has_key(authuser):
for num in range(3,0,-1):
password = raw_input('Please input you password(you hava 3 times to try):').strip()
if len(password) == 0:continue
if password == userinfo[authuser][0]:menu()
else:
print "You input the Password is Error, you have %s times to Try it" % (num-1)
continue
else:
print 'You have try the 3 times , Please go out!!!'
sys.exit()
else:
print "\033[;31mWrong account %r,retype\033[0m" % authuser
这里主要说下pickle的操作
pickle语法dump
import pickle
account_info = {
'8906143632':['alex3714',15000,15000],
'8908223631':['rachel',9000,9000],
}
f = file('account.pkl','wb')
pickle.dump(account_info,f)
f.close()
pickle语法load
import pickle
#load data first
pkl_file = open('account.pkl','rb')
account_list = pickle.load(pkl_file)
pkl_file.close()
效果图: