使用python3编写简单信用卡管理程序

这篇文章主要介绍了使用python3 编写简单信用卡管理程序的代码,非常不错,具有参考借鉴价值,需要的朋友参考下吧

1、程序执行代码:

#author by andy
#_*_ coding:utf-8 _*_
import os,sys,time
base_dir=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(base_dir)
str=”欢迎使用银行信用卡自助服务系统!\n”
for i in str:
sys.stdout.write(i)
sys.stdout.flush()
time.sleep(0.3)
while true:
print(“1、管理人员入口。”)
time.sleep(0.3)
print(“2、用户登录入口。”)
print(“3、退出请按q!”)
choice=input(“:”)
from core import main
exit_flag=true
while exit_flag:
user_choice=main.menu(choice)
if user_choice == ‘1’:
main.get_user_credit()
elif user_choice == ‘2’:
main.repayment()
elif user_choice == ‘3’:
main.enchashment()
elif user_choice == ‘4’:
main.change_pwd()
elif user_choice == ‘5’:
main.transfer()
elif user_choice == ‘6’:
main.billing_query()
elif user_choice == ‘7’:
print(“该功能正在建设中,更多精彩,敬请期待!”)
elif user_choice == ‘a’:
main.change_user_credit()
elif user_choice == ‘b’:
main.add_user()
elif user_choice == ‘c’:
main.del_user()
elif user_choice == ‘d’:
main.change_pwd()
elif user_choice == ‘q’ or user_choice == ‘q’:
print(“欢迎再次使用,再见!”)
exit_flag = false

2、程序功能函数:

#author by andy#_*_ coding:utf-8 _*
import json,sys,os,time,shutil
base_dir=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(base_dir)
#定义认证装饰器
def auth(func):
def wrapper(*args,**kwargs):
# print(“请输入卡号和密码进行验证!”)
f = open(base_dir+’\data\\user_db.txt’, ‘r’)
log_file = open(base_dir+’\logs\log.txt’, ‘a+’, encoding=’utf-8′)
bill_log_file = open(base_dir + ‘\logs\\bill_log.txt’, ‘a+’, encoding=’utf-8′)
func_name = func.__name__
time_formate = ‘%y-%m-%d %x’
start_time = time.strftime(time_formate, time.localtime())
user_data = json.load(f)
count=0
while count < 3: global user_id global user_pwd user_id = input('请输入您的卡号:') user_pwd = input('请输入您的密码:') if user_id in user_data: if user_pwd == user_data[user_id]['password']: log_file.write(start_time + ' 卡号 %s 认证成功!\n' % user_id) log_file.flush() time.sleep(1) log_file.close keywords = func(*args, **kwargs) if func_name == 'repayment' or func_name == 'transfer' or func_name == 'enchashment': bill_log_file.write(start_time + ' 卡号 '+ user_id + ' 发起 ' + func_name + ' 业务,金额为: %s \n' % keywords) bill_log_file.flush() time.sleep(1) bill_log_file.close return keywords else: return keywords else: print('卡号或密码错误!请重新输入!') log_file.write(start_time + ' 卡号 %s 认证失败!\n' % user_id) log_file.flush() time.sleep(1) log_file.close count +=1 else: print("卡号不存在,请确认!") if count == 3: print("对不起,您已输错3三次,卡号已锁定!") log_file.write(start_time + ' 卡号 %s 因连续三次验证失败而被锁定!\n' % user_id) time.sleep(1) log_file.close return wrapper #定义菜单函数,根据不同用户显示不通菜单。 def menu(choice): if choice == '2': print( "请选择服务类别:\n" "1、查询信用额度。\n" "2、信用卡还款。\n" "3、信用卡提现。\n" "4、修改口令。\n" "5、信用卡转账。\n" "6、信用卡账单查询。\n" "7、轻松购物。\n" "8、退出请按q!\n") service_items = input('-->‘)
elif choice == ‘1’:
print(“请选择服务类别:\n”
“a、修改用户信用额度。\n”
“b、新增信用卡用户。\n”
“c、删除信用卡用户。\n”
“d、修改用户口令。\n”
“e、退出请按q!\n”)
service_items = input(‘–>’)
else:
print(“感谢使用,祝生活愉快!”)
exit()
return service_items
# 定义备份用户数据文件函数
def back_up_file():
time_formate = ‘%y-%m-%d’
sys_time = time.strftime(time_formate, time.localtime())
shutil.copy(base_dir + “\data\\user_db.txt”, base_dir + “\data\\user_db–” + sys_time + “.bak.txt”)
#定义获取用户数据信息函数
def get_user_data():
with open(base_dir + “\data\\user_db.txt”, ‘r+’,encoding=’utf-8′) as f:
user_data = json.load(f)
return user_data
#定义用户数据变量
user_data = get_user_data()
#定义查询信用额度函数
@auth
def get_user_credit():
user_credit=user_data[user_id][‘credit’]
print(“您目前的信用额度为:%s元\n”
%(user_credit))
time.sleep(2)
return user_credit
#定义信用卡还款函数
@auth
def repayment():
user_data = get_user_data()
user_credit=int(user_data[user_id][‘credit’])
user_balance=int(user_data[user_id][‘balance’])
user_bill = user_credit – user_balance
print(“您目前需要还款金额为:%s元.\n” %user_bill)
exit_flag=true
while exit_flag:
repayment_value=input(“请输入还款金额:”)
if repayment_value.isdigit():
repayment_value=int(repayment_value)
user_data[user_id][‘balance’] = user_data[user_id][‘balance’] + repayment_value
f = open(base_dir + “\data\\user_db.txt”, ‘r+’, encoding=’utf-8′)
json.dump(user_data, f)
f.close()
print(“恭喜,还款成功!”)
print(“您目前需要还款金额为:%s元.\n” % (user_data[user_id][‘credit’] – user_data[user_id][‘balance’]))
time.sleep(1)
exit_flag = false
return repayment_value
else:
print(“请输入正确的金额!”)
#定义信用卡提现函数
@auth
def enchashment():
user_credit=user_data[user_id][‘credit’]
print(“你可用的取现额度为:%s” %user_credit)
exit_flag=true
while exit_flag:
enchashment_value=input(“请输入您要取现的金额:”)
if enchashment_value.isdigit():
enchashment_value=int(enchashment_value)
if enchashment_value % 100 == 0:
if enchashment_value

Posted in 未分类

发表评论