通过完成信用卡系统+购物商城 使自己在利用 字典和列表方面有了较大的提升,感悟很深,

下面将我对此次作业所展示的重点列表如下:

#!/usr/bin/env python3.5
# -*-coding:utf8-*-
import pickle,os,datetime,sys BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
# 加载用户信息
if os.path.exists(r'%s/config/user_acc.txt'% BASE_DIR):
f = open("%s/config/user_acc.txt" % BASE_DIR, "rb")
data = pickle.loads(f.read())
f.close()
else:
data = []
# 加载ATM操作日志信息
if os.path.exists(r'%s/config/user_log.txt'% BASE_DIR):
fl = open("%s/config/user_log.txt" % BASE_DIR, "rb")
data_log = pickle.loads(fl.read())
fl.close()
else:
data_log = []
# ATM 操作日志增加函数
def User_Action(cz_usercard,cz_flage,usercard,user_action,money=0):
global data_log
data_log_add = {}
data_log_add["cz_usercard"] = cz_usercard
data_log_add["cz_flage"] = cz_flage
data_log_add["cz_action"] = user_action
data_log_add["cz_adduser"] = usercard
data_log_add["cz_time"] = datetime.datetime.now()
data_log_add["cz_money"] = money
data_log.append(data_log_add)
fl = open("%s/config/user_log.txt" % BASE_DIR, "wb")
pickle.dump(data_log, fl)
fl.close()
# 增加用户
def User_Add(cz_usercard,cz_flage,usercard,password,total_money,Repayment_Date,flage):
global data
data_list = []
for user_data in data:
data_list.append(user_data["usercard"])
if usercard not in data_list:
# 增加用户
user_dic ={}
user_dic["usercard"] = usercard
user_dic["password"] = password
user_dic["new_money"] = total_money
user_dic["total_money"] = total_money
user_dic["Repayment_Date"] = Repayment_Date
user_dic["flage"] = flage
user_dic["state"] = "on"
data.append(user_dic)
f = open("%s/config/user_acc.txt" % BASE_DIR, "wb")
pickle.dump(data, f)
f.close()
User_Action(cz_usercard,cz_flage,usercard,"增加用户")
print("增加卡号[%s],成功!"%(usercard))
else:
print("增加信用卡卡号失败,原因:[“已存在”]")
# 解冻信用卡
def User_thaw(usercard,cz_usercard,cz_flage):
global data
# 判断是否为管理员账号
if cz_flage =="":
for x_name in data:
if x_name["usercard"] == usercard:
x_name["state"] = "on"
f = open("%s/config/user_acc.txt" % BASE_DIR, "wb")
pickle.dump(data, f)
f.close()
# 写日志
User_Action(cz_usercard,cz_flage,usercard,"解冻用户")
print("解冻用户[%s],成功!"%(usercard)) # 用户冻结
def User_Frozen(usercard,cz_usercard,cz_flage):
global data
# 判断是否为管理员账号
if cz_flage =="":
for x_name in data:
if x_name["usercard"] == usercard:
x_name["state"] = "off"
f = open("%s/config/user_acc.txt" % BASE_DIR, "wb")
pickle.dump(data, f)
f.close()
# 写日志
User_Action(cz_usercard,cz_flage,usercard,"冻结用户")
print("冻结用户[%s],成功!"%(usercard))
# 用户删除
def User_del(usercard,cz_usercard,cz_flage):
global data
# 判断是否为管理员账号
if cz_flage =="":
for index,x_name in enumerate(data):
if x_name["usercard"] == usercard:
del data[index]
f = open("%s/config/user_acc.txt" % BASE_DIR, "wb")
pickle.dump(data, f)
f.close()
# 写日志
User_Action(cz_usercard,cz_flage,usercard,"删除用户")
print("删除用户[%s],成功!"%(usercard)) #用户查询
def Select_User():
for x_name in data:
if x_name["flage"] =="":
user_type ="管理员"
else:
user_type= "信用卡用户"
print("-"*100)
print("用户类型:[%s],卡号:[%s],信用授予额度:[%s]元,信用卡当前余额:[%s]元,账单日:[%s]号"
%(user_type,x_name["usercard"],x_name["total_money"],x_name["new_money"],x_name["Repayment_Date"]))
# 信用卡额度调整
def card_limit(usercard,money,cz_usercard,cz_flage):
global data
for x_name in data:
if x_name["usercard"] == usercard:
if x_name["total_money"] + money >= 0:
x_name["total_money"] += money
x_name["new_money"] += money
User_Action(cz_usercard,cz_flage,usercard,"调整信用卡额度",money)
print("调整信用卡[%s],额度成功!"%(usercard))
else:
print("信用卡可用额度不能为负数!")
f = open("%s/config/user_acc.txt" % BASE_DIR, "wb")
pickle.dump(data, f)
f.close()
#!/usr/bin/env python3.5
# -*-coding:utf8-*-
import os,pickle,sys,datetime BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
from contrl.management import User_Action
#加载ATM用户数据库
if os.path.exists(r'%s/config/user_acc.txt'% BASE_DIR):
f = open("%s/config/user_acc.txt" % BASE_DIR, "rb")
data_user = pickle.loads(f.read())
f.close()
# 加载ATM操作日志信息
if os.path.exists(r'%s/config/user_log.txt'% BASE_DIR):
fl = open("%s/config/user_log.txt" % BASE_DIR, "rb")
data_log = pickle.loads(fl.read())
fl.close() # 消费明细
if os.path.exists(r'%s/config/user_list.txt'% BASE_DIR):
f_list = open("%s/config/user_list.txt" % BASE_DIR, "rb")
data_list = pickle.loads(f_list.read())
f_list.close()
else:
data_list = [] # 消费明细增加
def sale_add(usercard,usermoney_input,action,state,bills,remarks,Counter=0,interest=0):
global data_user
today =datetime.date.today()
for x_data in data_user:
if x_data["usercard"] == usercard:
date = x_data["Repayment_Date"]
# 计算账单日
Statement_Date = today.replace(day=date)
if today > Statement_Date:
Statement_Date = Statement_Date + datetime.timedelta(days=+30) # 如果今天消费的日期 大于系统计算出来的账单日,则此笔消费的出账单为下个月
data_list_dic = {}
data_list_dic["usercard"] = usercard
data_list_dic["date"] = datetime.datetime.now()
data_list_dic["money"] = usermoney_input
data_list_dic["action"] = action # 1、信用卡取现 2、转账 3、消费 4、还款
data_list_dic["state"] = state # 1、已欠款 2、已还清
data_list_dic["bills"] = bills # 1、已出账单 2、未出账单
data_list_dic["Counter"] = Counter # 手续费
data_list_dic["interest"] = interest # 利息
data_list_dic["Statement_Date"] = Statement_Date # 出账单日
data_list_dic["remarks"] = remarks
data_list.append(data_list_dic)
f_list = open("%s/config/user_list.txt" % BASE_DIR, "wb")
pickle.dump(data_list,f_list)
f_list.close() # 查询余额函数
def select_balance(usercard,flage):
# 定义全局变量
global data_user
if flage =="":
for x_name in data_user:
# 匹配用户信息
if x_name["usercard"] == usercard:
if x_name["new_money"] - x_name["total_money"] >=0:
print("""
您当前可用余额为:%s元,当前信用卡授权额度为%s元,出账单日为每月%s号,当前没有欠款,谢谢!
"""%(x_name["new_money"],x_name["total_money"],x_name["Repayment_Date"]))
else:
print("""
您当前可用余额为:%s元,当前信用卡授权额度为%s元,出账单日为每月%s号,当前总欠款%s元,请按时还款!
"""%(x_name["new_money"],x_name["total_money"],x_name["Repayment_Date"],(x_name["total_money"]-x_name["new_money"]) )) else:
print("请求参数出错,请检查后在试!")
# 查询ATM操作日志
def select_log():
print("ATM操作日志:")
for index,x_log in enumerate(data_log):
x_date = x_log["cz_time"]
cz_user = x_log["cz_usercard"]
x_action = x_log["cz_action"]
x_money = x_log["cz_money"]
x_user = x_log["cz_adduser"]
print("-" *200)
print("序号:[%s]|操作日期:[%s]|操作用户:[%s]|动作:[%s]|涉及金额:%s元|对象用户:[%s]" %(index+1,x_date,cz_user,x_action,x_money,x_user)) # ATM 取现:
def Take_Money(usercard,usermoney_input,flage):
global data_user,data_list
for x_name in data_user:
if x_name["usercard"] == usercard:
# 判断可用额度是否满足取现的金额
if x_name["new_money"] - usermoney_input >= 0:
Counter = usermoney_input*0.05 # 取现手续费
x_name["new_money"] -= (usermoney_input + Counter)
# 写日志
User_Action(usercard,flage,usercard,"信用卡取款",usermoney_input)
# 写消费明细
if x_name["new_money"] >= x_name["total_money"]:
sale_add(usercard,usermoney_input,"","","","取现",Counter)
else:
sale_add(usercard,usermoney_input,"","","","取现",Counter)
print("取现[%s],成功!"%(usermoney_input))
else:
print("您当前取现额度超过你信用卡的可用额度,不能取现!")
f = open("%s/config/user_acc.txt" % BASE_DIR, "wb")
pickle.dump(data_user, f)
f.close() # 信用卡还款
def Card_Payment(usercard,money,flage=""):
global data_user,data_list
for x_name in data_user:
if x_name["usercard"] == usercard:
x_name["new_money"] += money
if flage != "":
print("还款入账成功")
sale_add(usercard,money,"","","","还款入账")
# 提前全额还款处理
if x_name["new_money"] >= x_name["total_money"]:
for list_name in data_list:
if list_name ["usercard"] == usercard:
if list_name ["state"] != "":
list_name ["state"] = ""
f_list = open("%s/config/user_list.txt" % BASE_DIR, "wb")
pickle.dump(data_list,f_list)
f_list.close()
# 提前部分还款处理
else:
for list_name in data_list:
if list_name ["usercard"] == usercard:
if list_name ["state"] != "":
if list_name["money"] < money:
money -= list_name["money"]
list_name["state"] = ""
f_list = open("%s/config/user_list.txt" % BASE_DIR, "wb")
pickle.dump(data_list,f_list)
f_list.close()
# 写还款日志
User_Action(usercard,flage,usercard,"信用卡还款",money)
f = open("%s/config/user_acc.txt" % BASE_DIR, "wb")
pickle.dump(data_user, f)
f.close()
# 账户转账
def Transfer(usercard,flage,usercard_input,usermoney_input):
global data_user
for input_name in data_user:
if input_name["usercard"] == usercard:
input_name["new_money"] -= usermoney_input
# 写消费明细
if input_name["new_money"] >= input_name["total_money"]:
sale_add(usercard,usermoney_input,"","","","转账")
else:
sale_add(usercard,usermoney_input,"","","","转账")
# 写日志
User_Action(usercard,flage,usercard_input,"信用卡转账",usermoney_input)
# 调用转账还款入账函数
Card_Payment(usercard_input,usermoney_input)
print("转账成功!")
f = open("%s/config/user_acc.txt" % BASE_DIR, "wb")
pickle.dump(data_user, f)
f.close()
# 账单明细查询
def select_bill(usercard,bill_date):
global data_list
# 已出账单明细
have_bill = []
# 未出账单明细
No_bills = []
# 未出账单还款入账
repayment = []
# 未出账单已还清
Already = []
for index,x_name in enumerate(data_list):
if x_name["usercard"] == usercard:
# 欠款未出账单消费明细
if x_name["state"] == "" and x_name["bills"] == "":
No_bills.append(data_list[index])
# 欠款已出账单消费明细
elif x_name["state"] == "" and x_name["bills"] == "":
have_bill.append(data_list[index])
# 转账还款入账明细
elif x_name["bills"] == "" and x_name["action"] == "":
repayment.append(data_list[index])
# 已还清未出账单明细
elif x_name["bills"] == "" and x_name["state"] == "":
Already.append(data_list[index]) if len(have_bill) != 0:
print("以下消费为已出账单未还清明细:")
for index,have_bill_list in enumerate(have_bill):
x_date = have_bill_list["date"]
x_action = have_bill_list["remarks"]
x_money = have_bill_list["money"]
x_Counter = have_bill_list["Counter"]
x_Statement_Date = have_bill_list["Statement_Date"]
x_state = "未还清"
x_interest = have_bill_list["interest"]
print("-" *150)
print("序号:[%s]|日期:[%s]|类型:[%s]|金额:[%s]元|手续费:%s元|状态:[%s]|此笔交易出账单日[%s]|累计利息为:%s元" %(index+1,x_date,x_action,x_money,x_Counter,x_state,x_Statement_Date,x_interest))
if len(No_bills) != 0:
print("以下消费为未出账单未还清明细:")
for index,No_bills_list in enumerate(No_bills):
x_date = No_bills_list["date"]
x_action = No_bills_list["remarks"]
x_money = No_bills_list["money"]
x_Counter =No_bills_list["Counter"]
x_Statement_Date = No_bills_list["Statement_Date"]
x_state = "未还清"
print("-" *100)
print("序号:[%s]|日期:[%s]|类型:[%s]|金额:[%s]元|手续费:%s元|状态:[%s]|此笔交易出账单日[%s]" %(index+1,x_date,x_action,x_money,x_Counter,x_state,x_Statement_Date))
if len(repayment) != 0:
print("以下为未出账单还款明细:")
for index,repayment_list in enumerate(repayment):
x_date = repayment_list["date"]
x_action = repayment_list["remarks"]
x_money = repayment_list["money"]
x_Counter = repayment_list["Counter"]
x_state = "还款入账"
print("-" *100)
print("序号:[%s]|日期:[%s]|类型:[%s]|金额:[%s]元|手续费:%s元|状态:[%s]" %(index+1,x_date,x_action,x_money,x_Counter,x_state))
if len(Already) != 0:
print("以下为未出账单已还款或无需还款消费明细:")
for index,Already_repayment_list in enumerate(Already):
x_date = Already_repayment_list["date"]
x_action = Already_repayment_list["remarks"]
x_money = Already_repayment_list["money"]
x_Counter = Already_repayment_list["Counter"]
x_state = "正常"
print("-" *100)
print("序号:[%s]|日期:[%s]|类型:[%s]|金额:[%s]元|手续费:%s元|状态:[%s]" %(index+1,x_date,x_action,x_money,x_Counter,x_state))

在处理利息计算方面,这是我想到的方法:

#!/usr/bin/env python3.5
# -*-coding:utf8-*-
import datetime,pickle,os,sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
# 加载利息计算日志文件
if os.path.exists(r'%s/config/calculation_log.txt'% BASE_DIR):
f_calculation = open("%s/config/calculation_log.txt" % BASE_DIR, "rb")
data_calculation = pickle.loads(f_calculation.read())
# 利息初始化文件
# print(data_calculation)
f_calculation.close()
else:
data_calculation = []
# 加载用户信息
if os.path.exists(r'%s/config/user_acc.txt'% BASE_DIR):
f = open("%s/config/user_acc.txt" % BASE_DIR, "rb")
data = pickle.loads(f.read())
f.close()
else:
data = []
# 加载消费明细
if os.path.exists(r'%s/config/user_list.txt'% BASE_DIR):
f_list = open("%s/config/user_list.txt" % BASE_DIR, "rb")
data_list = pickle.loads(f_list.read())
f_list.close()
else:
data_list = []
# 初始化利息计算文件列表
def calculation_computer():
global data, data_calculation, data_list
# 获取今天时间
today = datetime.date.today()
# 取昨天的时间
yesterday = datetime.date.today()+datetime.timedelta(days=-1)
for user_data in data:
if user_data["flage"] == "":
# 获取 账单日
x_data_date = user_data["Repayment_Date"]
# 生成账单日期
x_data_time = today.replace(day=x_data_date)
# 如果文件内容不为空
if len(data_calculation) != 0:
for y_data in data_calculation:
# 如果信用卡卡号在文件字典里存在关键字
if user_data["usercard"] in y_data.keys():
for x in y_data[user_data["usercard"]]:
# 如果该字典里存在今天新加的记录则跳出
if x["date"] == today:
break
# 如果字典里的日期与昨天有误差则进行增加
elif yesterday > y_data[user_data["usercard"]][-1]["date"]:
date = y_data[user_data["usercard"]][-1]["date"] + datetime.timedelta(days=1)
y_data[user_data["usercard"]].append({"date":date,"money":0,"computer":""})
continue
else:
y_data[user_data["usercard"]].append({"date":today,"money":0,"computer":""})
break
# 如果全部循环后找不到key 则新增
else:
# 重复数据判断
list_data = []
for y_data in data_calculation:
for k in y_data:
list_data.append(k)
# 如果要添加的数据在利息初始化文件中不存大
if user_data["usercard"] not in list_data:
if today > x_data_time:
new_dic = {user_data["usercard"]:[{"date":x_data_time,"money":0,"computer":""}]}
data_calculation.append(new_dic)
elif today < x_data_time:
new_dic = {user_data["usercard"]:[{"date":today,"money":0,"computer":""}]}
data_calculation.append(new_dic)
# 将所有利息数据写入文件中
fl = open("%s/config/calculation_log.txt" % BASE_DIR, "wb")
pickle.dump(data_calculation, fl)
fl.close()
# 如果文件内容为空
else:
# 时间按账单日开始记录
if today > x_data_time:
new_dic = {user_data["usercard"]:[{"date":x_data_time,"money":0,"computer":""}]}
data_calculation.append(new_dic)
elif today < x_data_time:
new_dic = {user_data["usercard"]:[{"date":today,"money":0,"computer":""}]}
data_calculation.append(new_dic)
fl = open("%s/config/calculation_log.txt" % BASE_DIR, "wb")
pickle.dump(data_calculation, fl)
fl.close()
calculation_computer()
# 出账单及利息计算
def computer():
global data, data_calculation, data_list
# 调用利息初始化文件
calculation_computer()
# 获取今天时间
today = datetime.date.today()
for x_data in data:
if x_data["flage"] == "":
x_usercard = x_data["usercard"] # 获取计算信用卡卡号
# 获取账单还款到期日,每月10号
due_date = today.replace(day=10)
# 如果到期还款日小于账单日,当前利息计算日 = 当月计算利息日-30天
xx_time = due_date + datetime.timedelta(days=-30)
# 加载消费明细账单
for z_data in data_list:
# 定位当前用户所有没有出账单的明细,如果今天大于或等于该笔消费出账单日
if z_data["usercard"] == x_usercard and z_data["bills"] == "" and today >= z_data["Statement_Date"]:
# 则更改当前该笔消费为已出账单
z_data["bills"] = ""
f_list_computer = open("%s/config/user_list.txt" % BASE_DIR, "wb")
pickle.dump(data_list, f_list_computer)
f_list_computer.close()
# 调用利息计算初始化文件
for xy_data in data_calculation:
# 定位到当前用户下所有记账日
if x_usercard in xy_data.keys():
for date in xy_data[x_usercard]:
if date["computer"] == "":
# 定位到当前用户下所有出账单的未还清的消费记录
# 初始化记账日所有利息
counter = 0
for z1_data in data_list:
# 如果记账日没有清算
if z1_data["usercard"] == x_usercard and z1_data["bills"] == "":
# 如果记账日大于最后还款日大于账单日
if date["date"] > due_date > z1_data["Statement_Date"] and z1_data["state"] == "":
z1_data["interest"] += (z1_data["money"]+z1_data["Counter"])*0.0005
counter += (z1_data["money"]+z1_data["Counter"])*0.0005
# 如果记账日小于出账单日或小于最后还款日
elif date["date"] > xx_time > z1_data["Statement_Date"] and z1_data["state"] == "":
z1_data["interest"] += (z1_data["money"]+z1_data["Counter"])*0.0005
counter += (z1_data["money"]+z1_data["Counter"])*0.0005
x_data["new_money"] -= counter # 更新当前用户的余额
f_list_computer = open("%s/config/user_list.txt" % BASE_DIR, "wb")
pickle.dump(data_list,f_list_computer)
f_list_computer.close()
date["computer"] = ""
continue
break
fl = open("%s/config/calculation_log.txt" % BASE_DIR, "wb")
pickle.dump(data_calculation, fl)
fl.close()
fx = open("%s/config/user_acc.txt" % BASE_DIR, "wb")
pickle.dump(data, fx)
fx.close()
05-11 13:36