基于python网络编程实现支持购物、转账、存取钱、定时计算利息的信用卡系统

时间:2021-11-19 20:52:25

一、要求

基于python网络编程实现支持购物、转账、存取钱、定时计算利息的信用卡系统

二、思路

1.购物类buy

接收 信用卡类 的信用卡可用可用余额,

返回消费金额

2.信用卡(ATM)类

接收上次操作后,信用卡可用余额,总欠款,剩余欠款,存款

其中: 1.每种交易类型不单独处理金钱,也不单独记录流水账,每种交易类型调用处理金钱的函数(传入交易类型,交易金额)
    2.处理金钱的函数,调用配置文件中关于每种交易类型的加减钱和利率

返回本次操作后信用卡可用余额,总欠款,剩余欠款,存款

3.客户端

银行管理员注册登陆
普通用户注册登陆
发送需求:注册、登陆、交易类型、交易金额

4.服务器端

调用购物类,创建购物对象(购物接口)
调用信用卡(ATM)类,处理还款,转账等操作,对利息按月记录,写入文件

5.定时任务

定时执行程序,以计算利息。

三、代码

3.1配置文件

import os

BASE_DIR = os.path.dirname(os.path.dirname(__file__))  #配置文件的上层目录
DB_DIR=os.path.join(BASE_DIR,'db') #数据文件夹
ADMIN=os.path.join(DB_DIR,'admin')
ALL_USERS=os.path.join(DB_DIR,'allusrs')
A=os.path.join(BASE_DIR,'db','s')
LOG=os.path.join(BASE_DIR,'log') TRANSACTION={
'repay':{'action':'plus','interest':0}, #还款
'withdraw':{'action':'minus','interest':0.05},#取现
'transfer':{'action':'minus','interest':0.05},#转账
'consume':{'action':'minus','interest':0},#消费
'saving':{'action':'plus','interest':0} #存款
}

3.2公共类

3.2.1购物类

class buy:
goods=[
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998},
] def __init__(self,money,consumption,shopping_cart,):
self.money=money
self.consumption=consumption
self.shopping_cart=shopping_cart def gouwu(self): #购物模块
print('您的当前余额为:%d' %self.money)
num=int(input('请输入商品序号:'))
num-=1
if self.goods[num]["name"] in self.shopping_cart.keys(): #goods[num]["name"]取商品名
self.shopping_cart[self.goods[num]["name"]]['n']+=1 #商品数量+1
else:
self.shopping_cart[self.goods[num]["name"]]={"price":self.goods[num]["price"],'n':1,} # 创建购物车字典 {keys{"price":价格,数量:1}}
self.money-=self.shopping_cart[self.goods[num]["name"]]["price"]*self.shopping_cart[self.goods[num]["name"]]['n'] #单价*数量
self.consumption+=self.shopping_cart[self.goods[num]["name"]]["price"]*self.shopping_cart[self.goods[num]["name"]]['n'] def yichu(self): #移除购物车模块
c=int(input(' 请输入0/1选择是否移除购物车商品, 移除请输入1:'))
if c==1:
e=int(input(' 请输入要移除的商品序号:'))
d=self.goods[e-1]
if d in self.shopping_cart.keys(): #判断要移除的商品是否在购物车内
self.shopping_cart.remove(d) #移除商品
self.money=self.money+self.goods[self.goods.index(d)]["price"] #余额增加
self.consumption=self.consumption-self.goods[self.goods.index(d)]["price"] #消费总额减少
else:
print('商品不存在')
def chongzhi(self): #充值模块
pay=int(input('请输入充值金额'))
self.money=self.money+pay
print('您的当前余额为:%d' % self.money) #显示当前余额 def main(self):
print('商品清单:')
for m,n in enumerate(self.goods,1):
print(m)
for v in n.values():
print(v)
print('=============')
#消费总额清零
self.consumption=0
buy=True #定义默认一直购物
while buy:
price=0 #定义初始价格
b=1 #定义默认不退出购物或充值状态
if self.money>=price: #消费模块;金钱大于货物价格时,才能开始购物
while self.money>=price:
#计价模块,有钱就可以一直购物
self.gouwu()
#移除购物车商品模块
self.yichu()
if self.money>=0:
print('您的当前余额为:%d' %self.money) #显示当前余额
b=int(input(' 请输入0/1选择是否继续购物, 购物请输入1:'))
if b==0: #
break #退出计价模块
if b==0: #如果不购物
break #不购物退出整个购物程序
#充值模块
else:
while self.money<price: #金钱不足,可多次充钱,直到能买得起货物
a=int(input(' 您的余额不足,请输入0/1选择是否充值,充值请输入1:'))
if a==1:
self.chongzhi()
else:
break #退出充值模块
if a==0:
break #不充值退出程序
#打印购物车商品名、商品价格、消费总额、余额
print('您的消费清单为:')
for m,n in self.shopping_cart.items():
print(m,n['price'],n['n'])
#打印消费清单
print('=============')
print('您的当前余额为:%d,您的消费总额为:%d' % (self.money,self.consumption) ) #打印消费总额
return self.consumption

3.2.2 信用卡ATM类

class Atm:
credit=15000 #信用卡额度
def __init__(self,balance,debt,remaining_debt,interest,saving,id):
self.id=id #信用卡id
self.balance=balance #信用卡可用金额
self.debt=debt #总欠款
self.remaining_debt=remaining_debt #剩余欠款
self.interest=interest #手续费
self.saving=saving #存款
self.now_time=time.strftime("%Y-%m-%d %H:%M:%S")
self.now_data=time.strftime("%Y-%m")
self.struct_time=time.gmtime(time.time())
if self.struct_time.tm_mday>22:
self.now_data=self.struct_time.tm_year+'-'+str(int(self.struct_time.tm_mon)+1) def account_info(self):#打印账户信息
return '账户id%s 信用卡额度%s;信用卡可用金额%s;剩余欠款%s;'%(self.id,self.credit,self.balance,self.remaining_debt,)
def ret_account_info(self):
return [self.id,self.credit,self.balance,self.debt,self.remaining_debt,self.interest]
def repay(self,amount):#还款
self.handel_money('repay',amount)
def withdraw(self,amount): #取现
self.handel_money('withdraw',amount)
def transfer(self,amount): #转账
self.handel_money('transfer',amount)
def consume(self,amount): #消费
self.handel_money('consume',amount)
def saves(self,amount):
self.handel_money('saving',amount)
def transaction(self,a,amount):
dic={
'1':self.repay,
'2':self.withdraw,
'3':self.transfer,
'4':self.consume,
'5':self.saves
}
print("debug: a:",type(a),"amount:",type(amount)) print(a)
print(dic[a])
print(dic["5"])
dic[a](amount)
print("end debug") def handel_money(self,transaction,amount): #交易类型,
amount=int(amount)
interest=amount*settings.TRANSACTION[transaction]['interest'] #手续费计算
if settings.TRANSACTION[transaction]['action']=='plus': if amount<=self.remaining_debt:
self.remaining_debt-=amount
self.balance+=amount
else:
self.balance+=self.remaining_debt
self.remaining_debt=0
self.saving+=amount-self.remaining_debt
else: if self.saving<amount:
self.saving=0
a=amount-self.saving
self.balance-=a+interest-self.saving
# self.debt+=amount+interest
self.remaining_debt+=a+interest
a='time:%s id:%s transaction: %s amount:%s interest %s \n'%(self.now_time,self.id,transaction,amount,interest)
print(a)
mulu=os.path.join(settings.ALL_USERS,self.id)
path_name_liushui=os.path.join(mulu,str(self.id)+'name_liushui',str(self.now_data)) with open(path_name_liushui,'a')as f: #记录流水信息
f.write(a) s=[self.balance,self.debt,self.remaining_debt,self.interest,self.saving,] #更新基本信息
path_name_base=os.path.join(mulu,str(self.id)+'name_base')
pickle.dump(s,open(path_name_base,'wb'))

 3.3服务器端:

#!/usr/bin/env python
# -*- coding: utf-8 -*- import sys,os
import hashlib
import pickle
import time
import socketserver
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from config import settings
from lib import modules
from lib.modules import * class Myserver(socketserver.BaseRequestHandler): def md5(self,pwd):
'''
对密码进行加密
:param pwd: 密码
:return:
'''
hash=hashlib.md5(bytes('xx7',encoding='utf-8'))
hash.update(bytes(pwd,encoding='utf-8'))
return hash.hexdigest() def login(self,usrname,pwd,x):
'''
登陆
:param usrname: 用户名
:param pwd: 密码
:return:是否登陆成功
'''
conn=self.request
if x=='1':
path_name_pwd=os.path.join(settings.ADMIN,usrname)
else:
mulu=os.path.join(settings.ALL_USERS,usrname)
path_name_pwd=os.path.join(mulu,usrname+'name_pwd')
s=pickle.load(open(path_name_pwd,'rb'))
if usrname in s:
if s[usrname]==self.md5(pwd): #和加密后的密码进行比较
return True
else:
return False
else:
return False def regist(self,usrname,pwd,x):
'''
注册
:param usrname: 用户名
:param pwd: 密码
:return:是否注册成功
''' conn=self.request
if x=='1':
mulu=os.path.join(settings.ADMIN,usrname)
else:
mulu=os.path.join(settings.ALL_USERS,usrname) if os.path.exists(mulu):
return False
else:
os.mkdir(mulu)
s={}
s[usrname]=self.md5(pwd)
path_name_pwd=os.path.join(mulu,usrname+'name_pwd')
pickle.dump(s,open(path_name_pwd,'wb'))
path_name_base=os.path.join(mulu,usrname+'name_base')
pickle.dump([15000,{},0,0,0],open(path_name_base,'wb'))
path_name_liushui=os.path.join(mulu,usrname+'name_liushui')
os.mkdir(path_name_liushui)
return True def user_identity_authentication(self,usrname,pwd,ret,x):
'''
判断注册和登陆,并展示用户的详细目录信息,支持cd和ls命令
:return:
'''
conn=self.request
if ret=='1':
r=self.login(usrname,pwd,x)
if r:
conn.sendall(bytes('y',encoding='utf-8'))
else:
conn.sendall(bytes('n',encoding='utf-8'))
elif ret=='2':
# print(usrname,pwd)
if x=='1':
r=self.regist(usrname,pwd,x)
else: #用户注册
s=[0,1]
pickle.dump(s,open(settings.A,'wb'))
while True:
ret=pickle.load(open(settings.A,'rb'))
if ret[0]==0:
time.sleep(30)
continue
elif ret[0]==1 or ret[0]==2:
break #默认值已更改,银行管理员已操作 if ret[0]==1: #如果管理员同意
r=self.regist(usrname,pwd,x)
else:
r=0
s=[0,0]
pickle.dump(s,open(settings.A,'wb'))
if r:
conn.sendall(bytes('y',encoding='utf-8'))
else:
conn.sendall(bytes('n',encoding='utf-8'))
def interactive(self,usrname): #进行交互
conn=self.request
while True:
c=conn.recv(1024) #接收用户交互选项
r=str(c,encoding='utf-8')
mulu=os.path.join(settings.ALL_USERS,usrname)
path_name_base=os.path.join(mulu,usrname+'name_base')
s=pickle.load(open(path_name_base,'rb')) #打印账户信息
obj=modules.Atm(s[0],s[1],s[2],s[3],s[4],usrname) #Atm对象
a=obj.account_info() #接收账户信息
conn.sendall(bytes(a,encoding='utf-8')) b=obj.ret_account_info() if r== '4':
buy_obj=modules.buy(b[2],0,{})
amount=buy_obj.main()
elif r=='q':
break
else:
s=conn.recv(1024)
amount=str(s,encoding='utf-8') obj.transaction(r,amount) pass def handle(self):
conn=self.request
x=conn.recv(1024)
x=str(x,encoding='utf-8')
conn.sendall(bytes('收到用户类别',encoding='utf-8'))
while True:
if x=='1' or x=='2':
b=conn.recv(1024)
ret=str(b,encoding='utf-8')
conn.sendall(bytes('b ok',encoding='utf-8'))
c=conn.recv(1024)
r=str(c,encoding='utf-8')
usrname,pwd=r.split(',')
print(usrname,pwd)
self.user_identity_authentication(usrname,pwd,ret,x) #登陆或注册验证
if x=='2':#普通用户身份验证成功后
self.interactive(usrname) pass break
elif x=='q':
break if __name__=='__main__':
sever=socketserver.ThreadingTCPServer(('127.0.0.1',9999),Myserver)
sever.serve_forever()

3.4 用户端

#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
本程序作为用户或银行管理员的入口,其中c=1代表银行管理员,c=2代表普通用户
'''
import pickle
import sys
import time
import os
import socket
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from config import settings
from lib import *
from lib.modules import * def login(usrname,pwd):
'''
登陆
:param usrname:用户名
:param pwd:密码
:return:是否登陆成功
'''
obj.sendall(bytes(usrname+','+pwd,encoding='utf-8'))
ret=obj.recv(1024)
r=str(ret,encoding='utf-8')
if r=='y':
return 1
else:
return 0 def regist(usrname,pwd,x):
'''
注册
:param usrname:用户名
:param pwd:密码
:return:是否注册成功
'''
obj.sendall(bytes(usrname+','+pwd,encoding='utf-8')) ret=obj.recv(1024)
r=str(ret,encoding='utf-8')
if r=='y':
return 1
else:
return 0
def user_identity_authentication(usrname,pwd,x):
'''
选择登陆或注册,展示用户的详细目录信息,支持cd和ls命令
:return:
'''
a=input('请选择1.登陆 2.注册')
obj.sendall(bytes(a,encoding='utf-8'))
obj.recv(1024)
if a=='1':
ret=login(usrname,pwd)
if ret:
print('登陆成功')
return 1
else:
print('用户名或密码错误')
return 0
elif a=='2':
ret=regist(usrname,pwd,x)
if ret:
print('注册成功')
return 1
else:
print('用户名已存在或银行管理员拒绝')
return 0
def main(x):
usrname=input('请输入用户名')
pwd=input('请输入密码')
if user_identity_authentication(usrname,pwd,x): #如果验证身份成功
if x=='1': #处理用户注册信息 while True:
s=pickle.load(open(settings.A,'rb'))
if s[1]==0:
time.sleep(30)
continue
elif s[1]==1:
while True:
a=input('用户请求注册,输入1同意,2拒绝')
if a=='1':
s=[1,0]
pickle.dump(s,open(settings.A,'wb'))
break
elif a=='2':
s=[2,0]
pickle.dump(s,open(settings.A,'wb'))
break
else:
print('输入有误')
break
else: #普通用户登陆后
interactive() #进行交互 def interactive():
while True:
a=input('请选择 1.还款 2.取现 3.转账 4.消费 5.存钱 q退出')
obj.sendall(bytes(a,encoding='utf-8'))
r=obj.recv(1024) #接收账户信息
ret=str(r,encoding='utf-8')
print(ret)
if a !='4'and a !='q':
b=input('请输入金额')
obj.sendall(bytes(b,encoding='utf-8'))
elif a=='q':
break obj=socket.socket() #创建客户端socket对象
obj.connect(('127.0.0.1',9999))
while True:
x=input('请选择1.银行管理员 2.用户 q、退出')
obj.sendall(bytes(x,encoding='utf-8'))
obj.recv(1024) #确认收到用户类别
if x=='1' or x=='2':
main(x)
break
elif x=='q':
break
else:
print('输入有误请重新输入') obj.close()

 3.5定时任务

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os,sys
import json,pickle
import time
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from config import settings def main():
card_list = os.listdir(settings.ALL_USERS)
for card in card_list:
basic_info = pickle.load(open(os.path.join(settings.ALL_USERS, card, card+'name_base')))
struct_time = time.localtime() # 循环账单列表,为每月的欠款计息。并写入到当月账单中
for item in basic_info['debt']:
interest = item['total_debt'] * 0.0005
if basic_info[4] >= interest:
basic_info[4] -= interest
else:
temp = interest - basic_info[4]
basic_info[4]=0
basic_info[0] -= temp
pickle.dump(
basic_info,
open(os.path.join(settings.ALL_USERS, card, card+'name_base'),'w')
) # 如果当前等于10号(9号之前)
# 当前余额为负值,则将值添加到账单列表中,开始计息,同时,本月可用额度恢复。
date = time.strftime("%Y-%m-%d")
if struct_time.tm_mday == 11 and basic_info[2]>0:
dic = {'date': date,
"total_debt": basic_info[2],
"balance_debt": basic_info[2],
}
basic_info[1].append(dic)
# 恢复可用额度
basic_info[0] = 15000
pickle.dump(
basic_info,
open(os.path.join(settings.ALL_USERS, card, card+'name_base'),'w')
) def run():
main()