本文实例讲述了python实现的银行系统模拟程序。分享给大家供大家参考,具体如下:
银行系统模拟程序
1、概述
使用面向对象思想模拟一个简单的银行系统,具备的功能:管理员登录/注销、用户开户、登录、找回密码、挂失、改密、查询、存取款、转账等功能。
编程语言:python。
2、目的
通过这个编程练习,可以熟悉运用面向对象的思想来解决实际问题,其中用到的知识点有类的封装、正则表达式、模块等。
3、体会
在编写这个程序时,实际上的业务逻辑还是要考虑的,比如修改密码时需要输入手机号、身份证号等。在进行类的封装时,实际上还是用面向过程的思想把一些基本的业务逻辑编写成函数,对一些重复使用的代码也可以封装成函数(就是自己造适合这个业务的*,实际开发中很多底层的函数是不用自己再次去实现的,可以直接调用),这些都是一些底层的封装,然后在实现主要业务时上就可以调用类中的方法实现,这时只需关注业务逻辑就好了。
使用面向对象的思想进行编程,考虑的点是:实现一个功能,有哪些方法可以让我进行调用(指挥者)。
使用面向过程的思想进行编程,考虑的点是:实现一个功能,我需要实现哪些方法(执行者)。
编写这个程序还用到一个很重要的概念,就是对程序进行模块化。模块化的好处是可以更好的对程序进行维护,条理也更清晰。
4、代码
源码github地址:https://github.com/liangdongchang/pybanksystem.git
1、banksystem.py文件
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
|
from view import view
from atm import atm
from person import person
def func(view,atm,per):
view.funcinterface()
choice = input ( "请选择您要办理的业务:" )
if choice = = '1' :
return per.checkmoney(atm)
elif choice = = '2' :
return per.savemoney(atm)
elif choice = = '3' :
return per.getmoney(atm)
elif choice = = '4' :
return per.transfermoney(atm)
elif choice = = '5' :
return per.changepassword(atm)
elif choice = = '6' :
return per.unlockaccount(atm)
elif choice = = '7' :
return per.closeaccount(atm)
elif choice = = 't' :
if per.exit(atm):
return true
else :
print ( "输入有误!" )
def main():
# 管理员登录名为'admin',密码为'123'
view = view( "admin" , '123' )
view.initface()
atm = atm()
view.login()
per = person()
while true:
view.funcinit()
choice = input ( "请选择您要办理的业务:" )
if choice = = '1' :
per.newaccount(atm)
elif choice = = '2' :
if per.login(atm):
while true:
if func(view,atm,per) = = none:
continue
else :
break
elif choice = = '3' :
per.findbackpassword(atm)
elif choice = = '4' :
per.lockaccount(atm)
elif choice = = 't' :
if per.exit(atm):
# 管理员注销系统
if view.logout():
return true
else :
print ( "输入有误!" )
if __name__ = = '__main__' :
main()
|
2、card.py文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
'''
卡:
类名:card
属性:卡号【6位随机】 密码 余额 绑定的身份证号 手机号
'''
class card( object ):
def __init__( self , cardid, password, money,identityid,phonenum,cardlock = 'false' ):
self .cardid = cardid
self .password = password
self .money = money
self .identityid = identityid
self .phonenum = phonenum
self .cardlock = cardlock
|
3、readappendcard.py文件:
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
|
'''
功能:读取文件cardinfo.txt的信息
方法:读、写、删
'''
from card import card
import json
# 读
class readcard(card):
def __init__( self , cardid = ' ', password=' ', money=0, identityid=' ', phonenum=' ', cardlock=' '):
card.__init__( self , cardid, password, money, identityid, phonenum, cardlock)
def dict2card( self , d):
return self .__class__(d[ "cardid" ], d[ "password" ], d[ "money" ],d[ "identityid" ],d[ "phonenum" ], d[ "cardlock" ])
def read( self ):
# card对象转为字典
with open ( "cardinfo.txt" , "r" ,encoding = "utf-8" ) as fr:
cards = []
for re in fr.readlines():
cards.append( self .dict2card( eval (re)))
return cards
# 写
class appendcard(card):
def __init__( self ):
card.__init__( self , cardid = ' ', password = ' ', money = 0, identityid = ' ', phonenum = ' ', cardlock=' ')
def card2dict( self ,card):
return { "cardid" : card.cardid, "password" : card.password,
"money" : card.money, "identityid" : card.identityid,
"phonenum" : card.phonenum, "cardlock" : card.cardlock
}
def append( self ,card,w = 'a' ):
# 默认是追加,如果w='w'就清空文件
if w = = 'w' :
with open ( "cardinfo.txt" , "w" , encoding = "utf-8" ) as fa:
fa.write('')
else :
with open ( "cardinfo.txt" , "a" , encoding = "utf-8" ) as fa:
json.dump(card, fa, default = self .card2dict)
fa.write( '\n' )
# 删
class del ( object ):
def del_( self ,cardid):
readcard = readcard()
cards = readcard.read()
for card in cards:
# 删除输入的卡号
if cardid = = card.cardid:
cards.remove(card)
break
else :
print ( "卡号不存在!" )
return false
# 重新写入文件
appendcard = appendcard()
appendcard.append(' ',w=' w')
for card in cards:
appendcard.append(card)
return true
|
4、person.py
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
|
'''
人
类名:person
行为:开户、查询、取款、存储、转账、改密、销户、退出
'''
class person( object ):
def __init__( self ,name = ' ',identity=' ',phonenum=' ',card = none):
self .name = name
self .identity = identity
self .phonenum = phonenum
self .card = card
# 登录
def login( self ,atm):
card = atm.login()
if card:
self .card = card
return true
else :
return false
# 开户
def newaccount( self ,atm):
return atm.newaccount()
#找回密码
def findbackpassword( self ,atm):
return atm.findbackpassword()
# 查询余额
def checkmoney( self , atm):
return atm.checkmoney( self .card)
# 存钱
def savemoney( self , atm):
return atm.savemoney( self .card)
# 取钱
def getmoney( self , atm):
return atm.getmoney( self .card)
# 转账
def transfermoney( self , atm):
return atm.transfermoney( self .card)
# 销户
def closeaccount( self , atm):
return atm.closeaccount( self .card)
# 挂失
def lockaccount( self , atm):
return atm.lockaccount()
# 解锁
def unlockaccount( self , atm):
return atm.unlockaccount( self .card)
# 改密
def changepassword( self , atm):
return atm.changepassword( self .card)
# 退出系统
def exit( self , atm):
return atm.exit()
|
5、view.py
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
|
'''
管理员界面
类名:view
属性:账号,密码
行为:管理员初始化界面 管理员登陆 系统功能界面 管理员注销
系统功能:开户 查询 取款 存储 转账 改密 销户 退出
'''
from check import check
import time
class view( object ):
def __init__( self ,admin,password):
self .admin = admin
self .password = password
# 管理员初始化界面
def initface( self ):
print ( "*------------------------------------*" )
print ( "| |" )
print ( "| 管理员界面正在启动,请稍候... |" )
print ( "| |" )
print ( "*------------------------------------*" )
time.sleep( 1 )
return
#管理员登录界面
def login( self ):
print ( "*------------------------------------*" )
print ( "| |" )
print ( "| 管理员登陆界面 |" )
print ( "| |" )
print ( "*------------------------------------*" )
check = check()
check.username( self .admin, self .password)
print ( "*-------------登陆成功---------------*" )
print ( " 正在跳转到系统功能界面,请稍候... " )
del check
time.sleep( 1 )
return
# 管理员注销界面
def logout( self ):
print ( "*------------------------------------*" )
print ( "| |" )
print ( "| 管理员注销界面 |" )
print ( "| |" )
print ( "*------------------------------------*" )
#确认是否注销
check = check()
if not check.issure( '注销' ):
return false
check.username( self .admin, self .password)
print ( "*-------------注销成功---------------*" )
print ( " 正在关闭系统,请稍候... " )
del check
time.sleep( 1 )
return true
#系统功能界面
'''
系统功能:开户,查询,取款,存储,转账,销户,挂失,解锁,改密,退出
'''
def funcinit( self ):
print ( "*-------welcome to future bank---------*" )
print ( "| |" )
print ( "| (1)开户 (2)登录 |" )
print ( "| (3)找回密码 (4)挂失 |" )
print ( "| (t)退出 |" )
print ( "| |" )
print ( "*--------------------------------------*" )
def funcinterface( self ):
print ( "*-------welcome to future bank---------*" )
print ( "| |" )
print ( "| (1)查询 (5)改密 |" )
print ( "| (2)存款 (6)解锁 |" )
print ( "| (3)取款 (7)销户 |" )
print ( "| (4)转账 (t)退出 |" )
print ( "| |" )
print ( "*--------------------------------------*" )
|
6、atm.py
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
'''
提款机:
类名:atm
属性:
行为(被动执行操作):开户,查询,取款,存储,转账,销户,挂失,解锁,改密,退出
'''
from check import check
from card import card
from readappendcard import readcard,appendcard
import random
import time
class atm( object ):
def __init__( self ):
# 实例化相关的类
self .check = check()
self .readcard = readcard()
self .appendcard = appendcard()
self .cards = self .readcard.read()
# 显示功能界面
def funcshow( self ,ope):
if ope ! = "找回密码" :
print ( "*-------welcome to future bank-------*" )
print ( "| %s功能界面 |" % ope)
print ( "*------------------------------------*" )
else :
# 显示找回密码界面
print ( "*-------welcome to future bank-------*" )
print ( "| 找回密码功能界面 |" )
print ( "*------------------------------------*" )
# 卡号输入
def cardinput( self ,ope = ''):
while true:
cardid = input ( "请输入卡号:" )
password = input ( "请输入密码:" )
card = self .check.iscardandpasswordsure( self .cards, cardid,password)
if not card:
print ( "卡号或密码输入有误!!!" )
if ope = = 'login' or ope = = 'lock' :
return false
else :
continue
else :
return card
# 登录
def login( self ):
self .funcshow( "登录" )
return self .cardinput( 'login' )
#找回密码
def findbackpassword( self ):
self .funcshow( "找回密码" )
cardid = input ( "请输入卡号:" )
card = self .check.iscardidexist( self .cards,cardid)
if card:
if not self .check.iscardinfosure(card, "找回密码" ):
return
newpassword = self .check.newpasswordinput()
index = self .cards.index(card)
self .cards[index].password = newpassword
self .writecard()
print ( "找回密码成功!请重新登录!!!" )
time.sleep( 1 )
return true
else :
print ( "卡号不存在!!!" )
return true
# 开户
def newaccount( self ):
self .funcshow( "开户" )
# 输入身份证号和手机号
pnum = self .check.phoneinput()
iden = self .check.identifyinput()
print ( "正在执行开户程序,请稍候..." )
while true:
# 随机生成6位卡号
cardid = str (random.randrange( 100000 , 1000000 ))
# 随机生成的卡号存在就继续
if self .check.iscardidexist( self .cards,cardid):
continue
else :
break
# 初始化卡号密码,卡里的钱,卡的锁定状态
card = card(cardid, '888888' , 0 , iden, pnum , 'false' )
self .appendcard.append(card)
print ( "开户成功,您的卡号为%s,密码为%s,卡余额为%d元!" % (cardid, '888888' , 0 ))
print ( "为了账户安全,请及时修改密码!!!" )
# 更新卡号列表
self .cards = self .readcard.read()
return true
# 查询
def checkmoney( self ,card):
self .funcshow( "查询" )
if self .check.iscardlock(card):
print ( "查询失败!" )
else :
print ( "卡上余额为%d元!" % card.money)
time.sleep( 1 )
# 存款
def savemoney( self ,card):
self .funcshow( "存款" )
if self .check.iscardlock(card):
print ( "存钱失败!" )
else :
mon = self .check.moneyinput( "存款" )
# 找到所有卡中对应的卡号,然后对此卡进行存款操作
index = self .cards.index(card)
self .cards[index].money + = mon
print ( "正在执行存款程序,请稍候..." )
time.sleep( 1 )
self .writecard()
print ( "存款成功!卡上余额为%d元!" % self .cards[index].money)
time.sleep( 1 )
# 取款
def getmoney( self ,card):
self .funcshow( "取款" )
if self .check.iscardlock(card):
print ( "取钱失败!" )
else :
print ( "卡上余额为%d元!" % card.money)
mon = self .check.moneyinput( "取款" )
if mon:
if mon > card.money:
print ( "余额不足,您当前余额为%d元!" % card.money)
time.sleep( 1 )
else :
print ( "正在执行取款程序,请稍候..." )
time.sleep( 1 )
# 找到所有卡中对应的卡号,然后对此卡进行存款操作
index = self .cards.index(card)
self .cards[index].money - = mon
self .writecard()
print ( "取款成功!卡上的余额为%d元!" % self .cards[index].money)
time.sleep( 1 )
# 转账
def transfermoney( self ,card):
self .funcshow( "转账" )
if self .check.iscardlock(card): #如果卡已锁定就不能进行转账操作
print ( "转账失败!" )
return
while true:
cardid = input ( "请输入对方的账号:" )
if cardid = = card.cardid:
print ( "不能给自己转账!!!" )
return
cardother = self .check.iscardidexist( self .cards,cardid) #判断对方卡号是否存在
if cardother = = false:
print ( "对方账号不存在!!!" )
return
else :
break
while true:
print ( "卡上余额为%d元" % card.money)
mon = self .check.moneyinput( "转账" )
if not mon: #输入的金额不对就返回
return
if mon > card.money: #输入的金额大于卡上余额就返回
print ( "余额不足,卡上余额为%d元!" % card.money)
return
else :
break
print ( "正在执行转账程序,请稍候..." )
time.sleep( 1 )
index = self .cards.index(card) # 找到所有卡中对应的卡号,然后对此卡进行转账操作
self .cards[index].money - = mon
indexother = self .cards.index(cardother) #找到对卡卡号所处位置
self .cards[indexother].money + = mon
self .writecard()
print ( "转账成功!卡上余额为%d元!" % self .cards[index].money)
time.sleep( 1 )
# 销户
def closeaccount( self ,card):
self .funcshow( "销户" )
if not self .check.iscardinfosure(card, "销户" ):
return
if card.money > 0 :
print ( "卡上还有余额,不能进行销户!!!" )
return
if self .check.issure( "销户" ):
self .cards.remove(card) #移除当前卡号
self .writecard()
print ( "销户成功!" )
time.sleep( 1 )
return true
# 挂失
def lockaccount( self ):
self .funcshow( "挂失" )
card = self .cardinput( 'lock' )
if not card:
return
if card.cardlock = = "true" :
print ( "卡已处于锁定状态!!!" )
return
if not self .check.iscardinfosure(card, "挂失" ):
return
if self .check.issure( "挂失" ):
index = self .cards.index(card) #找到所有卡中对应的卡号,然后对此卡进行挂失操作
self .cards[index].cardlock = "true"
self .writecard()
print ( "挂失成功!" )
time.sleep( 1 )
return true
# 解锁
def unlockaccount( self ,card):
self .funcshow( "解锁" )
if card.cardlock = = 'false' :
print ( "无需解锁,卡处于正常状态!!!" )
return
if not self .check.iscardinfosure(card, "解锁" ):
return
index = self .cards.index(card)
self .cards[index].cardlock = "false"
self .writecard()
print ( "解锁成功!" )
time.sleep( 1 )
return true
# 改密
def changepassword( self ,card):
self .funcshow( "改密" )
if self .check.iscardlock(card):
print ( "卡处于锁定状态,不能进行改密!!!" )
return
if not self .check.iscardinfosure(card, "改密" ):
return
# 输入旧密码
while true:
password = input ( "请输入旧密码:" )
if self .check.ispasswordsure(password,card.password):
break
else :
print ( "卡号原密码输入错误!" )
return
newpassword = self .check.newpasswordinput()
index = self .cards.index(card) #找到所有卡中对应的卡号,然后对此卡进行改密操作
self .cards[index].password = newpassword
self .writecard()
print ( "改密成功!请重新登录!!!" )
time.sleep( 1 )
return true
# 写入文件
def writecard( self ):
self .appendcard.append(' ', w=' w') #先清除原文件再重新写入
for card in self .cards:
self .appendcard.append(card)
# 退出
def exit( self ):
if self .check.issure( "退出" ):
return true
else :
return false
|
7、check.py
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
|
'''
验证类:
用户名、密码、卡号、身份证、手机号验证
使用正则表达式进行文本搜索
'''
import re
class check( object ):
def __init__( self ):
pass
#用户验证
def username( self ,admin,password):
self .admin = admin
self .password = password
while true:
admin = input ( "请输入用户名:" )
password = input ( "请输入密码:" )
if admin ! = self .admin or password ! = self .password:
print ( "用户名或密码输入有误,请重新输入!!!" )
continue
else :
return
#是否确认某操作
def issure( self ,operate):
while true:
res = input ( "是否确认%s?【yes/no】" % operate)
if res not in [ 'yes' , 'no' ]:
print ( "输入有误,请重新输入!!!" )
continue
elif res = = 'yes' :
return true
else :
return false
# 手机号验证
def phoneinput( self ):
# 简单的手机号验证:开头为1且全部为数字,长度为11位
while true:
pnum = input ( "请输入您的手机号:" )
res = re.match(r "^1\d{10}$" ,pnum)
if not res:
print ( "手机号输入有误,请重新输入!!!" )
continue
return pnum
# 身份证号验证
def identifyinput( self ):
# 简单的身份证号验证:6位,只有最后一可以为x,其余必须为数字
while true:
iden = input ( "请输入您的身份证号(6位数字):" )
res = re.match(r "\d{5}\d|x$" ,iden)
if not res:
print ( "身份证号输入有误,请重新输入!!!" )
continue
return iden
# 卡号是否存在
def iscardidexist( self ,cards,cardid):
for card in cards:
if cardid = = card.cardid:
return card
else :
return false
# 卡号和密码是否一致
def iscardandpasswordsure( self ,cards,cardid,password):
card = self .iscardidexist(cards,cardid)
if card:
if card.password = = password:
return card
return false
# 密码二次确认是否正确
def ispasswordsure( self , newassword,oldpassword):
if newassword = = oldpassword:
return true
else :
return false
# 卡号完整信息验证
def iscardinfosure( self ,card,ope):
phonenum = input ( "请输入手机号:" )
iden = input ( "请输入身份证号:" )
if card.phonenum = = phonenum and card.identityid = = iden:
return true
print ( "%s失败!!!\n密码、手机号或身份证号与卡中绑定的信息不一致!!!" % ope)
return false
# 卡号是否锁定
def iscardlock( self ,card):
if card.cardlock = = "true" :
print ( "此卡已挂失!" )
return true
return false
# 输入金额验证
def moneyinput( self ,ope):
mon = input ( "输入%s金额(100的倍数):" % ope)
# 输入的钱必须是100的倍数
if re.match(r "[123456789]\d*[0]{2}$" , mon):
return int (mon)
print ( "输入有误,%s金额必须是100的倍数!请重新输入!!!" % ope)
return false
def newpasswordinput( self ):
while true:
newpassword = input ( "请输入新密码:" )
if not re.match(r "\d{6}$" ,newpassword):
print ( "密码必须是6位的纯数字!!!" )
continue
newpasswordagain = input ( "请重复输入新密码:" )
if self .ispasswordsure(newpassword, newpasswordagain):
break
else :
print ( "两次输入不一致!" )
continue
return newpassword
|
希望本文所述对大家python程序设计有所帮助。
原文链接:https://blog.csdn.net/lm_is_dc/article/details/80199278