gpt4 book ai didi

Python实现信用卡系统(支持购物、转账、存取钱)

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 28 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Python实现信用卡系统(支持购物、转账、存取钱)由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

最近一直在做一个有关信用卡系统的项目,所有很少出来给大家打招呼了,今天也该告一段了,本项目是基于python编程语言做的,此信用卡支持购物,转账和存取钱,下面小编把需求及实现思路大概分享一下,仅供参考,如有bug欢迎各位大侠提出,共同学习进步,谢谢! 。

1、要求 。

Python实现信用卡系统(支持购物、转账、存取钱)

2、思路 。

1.购物类buy 。

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

返回消费金额 。

2.信用卡(ATM)类 。

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

其中: 1.每种交易类型不单独处理金钱,也不单独记录流水账,每种交易类型调用处理金钱的函数(传入交易类型,交易金额) 。

    2.处理金钱的函数,调用配置文件中关于每种交易类型的加减钱和利率 。

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

3.客户端 。

银行管理员注册登陆 。

普通用户注册登陆 。

发送需求:注册、登陆、交易类型、交易金额 。

4.服务器端 。

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

5.定时任务 。

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

3、代码 。

3.1配置文件 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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购物类 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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类 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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服务器端:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/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 用户端 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/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定时任务 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/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()

以上所述是小编给大家介绍的Python实现信用卡系统(支持购物、转账、存取钱)的全部内容,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我网站的支持! 。

最后此篇关于Python实现信用卡系统(支持购物、转账、存取钱)的文章就讲到这里了,如果你想了解更多关于Python实现信用卡系统(支持购物、转账、存取钱)的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com