很久没写博文了,看了下距离before篇的时间确实有点长,真是感受时间愈来愈不够用了…… 好了不扯了,进入主题:python
先交代一下,此程序纯粹是为了练习Python各类元素的用法而假想出来的,距离如今已有段时间,此时笔者只是简单的掌握了列表、字典、模块(sys、os、pickle)文件读写等基本语法。
git
功能模型思路:安全
一、钱、没钱怎么买东西…… 因此每次购物须要带钱(现金+信用卡)
app
二、既然是购物程序那么要有商品列表和价格的对照表以便用户购买,用户不能买超出本身支付能力的商品,购买商品成功应当加入用户的购物列表,而且实时的显示用户的余额,购物刷卡没有手续费。
ide
三、当用户现金不够时能够用信用卡支付,信用卡付款金额 = 商品价格 - 现金(身上的钱)
函数
四、信用卡能够提现金,可是有手续费。
ui
五、应当提供还款接口。
spa
六、能够查看信用卡的明细,好比说什么时间买过什么东西,花了多少钱等……
rest
七、退出
orm
逻辑模型思路:
一、购物
print 购物列表,任意用户能够光临,money为用户的全部钱,quit为退换主菜单,若是现钱不够能够刷卡
二、提现
10%的利息
三、还钱
四、查看帐单(函数和Pickle)
最大额度 = 15000
可用额的 = 11000 (购买商品不能大于此额度)
亏欠金额 = 4000 (此金额不能大于15000)
######################################
DATE EVENT DATA INTEREST
1 car 20000 0
2 还款 -20000 0
3 提现 1000 100
五、退出
代码以下:
#!/usr/bin/env python #Author: Created by soulboy #Function: 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 ! [2]:Withdraw money ! [3]:Repayment ! [4]:Credit card list ! [5]:Exit ! ''' 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': #退出 sys.exit()
补充说明:日期这块是手动输入的,当时还没看到date模块,使用的时候每第二天期尽可能别重复,安全认证函数在查看信用卡明细的时候没有调用主要是为了方便查看,日期忘记排序了,运行效果应该以下: