在本篇博文中,我们将使用Python编写一个简单的银行管理系统,该系统具有开户、查询、取款、存款、转账、锁定、解锁、存盘和退出等功能。我们将逐步分析每个功能,并提供相应的代码实现。
7.5 实例1: 银行管理系统
从早期的钱庄到现如今的银行,金融行业在不断地变革;随着科技的发展、计算机的普及、计算机技术在金融行业得到了广泛的应用“银行管理系统是一个集开户、查询、取款、存款、转账、锁定、解锁、退出等一系列功能的管理系统。该系统中各功能的介绍如下:
(1)开户功能:用户在ATM 机上根据提示“请输人姓名:”、“请输人身份证号:”、“请输入机号:”依次输人姓名、身份证号、手机号、预存金额、密码等信息,如果开户成功,系统随机生成一个不重复的6位数字卡号。
(2)查询功能:根据用户输人的卡号、密码查询卡中余额,如果连续 3 次输入错误密码,该卡号会被锁定。
(3)取款功能:首先根据用户输入的卡号、密码显示卡中余额,如果连续 3 次输人错误密码该卡号会被锁定;然后接收用户输人的取款金额,如果取款金额大于卡中余额或取款金额小于0系统进行提示并返回功能页面。
(4)存款功能:首先根据用户输人的卡号、密码显示卡中余额,如果连续 3 次输入错误密码该卡号会被锁定,然后接收用户输入的存款金额:如果存款金额小于 0,系统进行提示并返回功能页面。
(5)转账功能: 用户需要分别输人转出卡号与转人卡号,如果连续3次输入错误密码,卡号会被锁定。当输人转账金额后,需要用户再次确认是否执行转账功能;如果确定执行转账功能后转出卡与转入卡做相应金额计算;如果取消转账功能,则回退之前操作。
(6)锁定功能:根据输人的卡号、密码执行锁定功能,锁定之后该卡不能执行查询、取款存款、转账等操作。
(7)解锁功能:根据输入的卡号、密码执行解锁功能,解锁后的卡,能够执行查询、取款存款、转账等操作。
(8)存盘功能:执行存盘功能后,程序执行的数据会写入本地文件中
(9)退出功能:执行退出功能时,需要输人管理员的账户密码。如果输入的账号密码错误则返回功能页面;如果输人的账号密码正确,则执行存盘并退出系统。
本实例要求使用python编写程序,实现一个具有上述功能的银行管理系统
开户功能允许用户在ATM机上输入个人信息,并生成一个不重复的6位数字卡号。以下是实现开户功能的代码:
# 开户功能
def open_account():
name = input("请输入姓名:")
id_number = input("请输入身份证号:")
phone = input("请输入手机号:")
balance = float(input("请输入预存金额:"))
password = input("请输入密码:")
card_number = generate_card_number()
users[card_number] = {
"name": name,
"id_number": id_number,
"phone": phone,
"balance": balance,
"password": password
}
print("开户成功!您的卡号是:", card_number)
查询功能允许用户通过输入卡号和密码来查询卡中的余额。如果连续3次输入错误密码,该卡号将被锁定。以下是实现查询功能的代码:
# 查询功能
def inquire():
card_number = input("请输入卡号:")
password = input("请输入密码:")
if card_number in locked_cards:
print("该卡号已被锁定,请联系银行解锁。")
return
user = users.get(card_number)
if user and user["password"] == password:
print("您的余额为:", user["balance"])
else:
print("卡号或密码错误!")
if card_number in users:
if card_number not in locked_cards:
users[card_number].setdefault("wrong_password_count", 0)
users[card_number]["wrong_password_count"] += 1
if users[card_number]["wrong_password_count"] >= 3:
locked_cards.append(card_number)
print("连续输错密码已达3次,该卡已被锁定。")
取款功能允许用户在输入卡号和密码后显示卡中的余额,并输入取款金额。如果连续3次输入错误密码,卡号将被锁定;如果取款金额大于卡中余额或小于0,系统将提示错误并返回功能页面。以下是实现取款功能的代码:
# 取款功能
def withdraw():
card_number = input("请输入卡号:")
password = input("请输入密码:")
if card_number in locked_cards:
print("该卡号已被锁定,请联系银行解锁。")
return
user = users.get(card_number)
if user and user["password"] == password:
print("您的余额为:", user["balance"])
amount = float(input("请输入取款金额:"))
if amount > user["balance"] or amount < 0:
print("取款金额无效!")
else:
user["balance"] -= amount
print("取款成功!")
print("当前余额为:", user["balance"])
else:
print("卡号或密码错误!")
if card_number in users:
if card_number not in locked_cards:
users[card_number].setdefault("wrong_password_count", 0)
users[card_number]["wrong_password_count"] += 1
if users[card_number]["wrong_password_count"] >= 3:
locked_cards.append(card_number)
print("连续输错密码已达3次,该卡已被锁定。")
存款功能允许用户在输入卡号和密码后显示卡中的余额,并输入存款金额。如果连续3次输入错误密码,卡号将被锁定;如果存款金额小于0,系统将提示错误并返回功能页面。以下是实现存款功能的代码:
# 存款功能
def deposit():
card_number = input("请输入卡号:")
password = input("请输入密码:")
if card_number in locked_cards:
print("该卡号已被锁定,请联系银行解锁。")
return
user = users.get(card_number)
if user and user["password"] == password:
print("您的余额为:", user["balance"])
amount = float(input("请输入存款金额:"))
if amount < 0:
print("存款金额无效!")
else:
user["balance"] += amount
print("存款成功!")
print("当前余额为:", user["balance"])
else:
print("卡号或密码错误!")
if card_number in users:
if card_number not in locked_cards:
users[card_number].setdefault("wrong_password_count", 0)
users[card_number]["wrong_password_count"] += 1
if users[card_number]["wrong_password_count"] >= 3:
locked_cards.append(card_number)
print("连续输错密码已达3次,该卡已被锁定。")
转账功能允许用户分别输入转出卡号和转入卡号。如果连续3次输入错误密码,卡号将被锁定。在输入转账金额后,用户需要再次确认是否执行转账功能。如果确定执行转账功能,将进行相应的金额计算;如果取消转账功能,则回退之前的操作。以下是实现转账功能的代码:
# 转账功能
def transfer():
from_card_number = input("请输入转出卡号:")
from_password = input("请输入密码:")
if from_card_number in locked_cards:
print("该卡号已被锁定,请联系银行解锁。")
return
from_user = users.get(from_card_number)
if from_user and from_user["password"] == from_password:
print("您的余额为:", from_user["balance"])
to_card_number = input("请输入转入卡号:")
to_user = users.get(to_card_number)
if to_card_number in locked_cards:
print("对方卡号已被锁定,无法转账。")
return
if not to_user:
print("转入卡号不存在,无法转账。")
return
amount = float(input("请输入转账金额:"))
if amount > from_user["balance"] or amount < 0:
print("转账金额无效!")
return
confirm = input("确认执行转账功能?(Y/N)")
if confirm.lower() == "y":
from_user["balance"] -= amount
to_user["balance"] += amount
print("转账成功!")
print("当前余额为:", from_user["balance"])
else:
print("已取消转账功能。")
else:
print("卡号或密码错误!")
if from_card_number in users:
if from_card_number not in locked_cards:
users[from_card_number].setdefault("wrong_password_count", 0)
users[from_card_number]["wrong_password_count"] += 1
if users[from_card_number]["wrong_password_count"] >= 3:
locked_cards.append(from_card_number)
print("连续输错密码已达3次,该卡已被锁定。")
锁定功能允许用户根据卡号和密码执行锁定功能。锁定后,该卡不能进行查询、取款、存款、转账等操作。以下是实现锁定功能的代码:
# 锁定功能
def lock_card():
card_number = input("请输入卡号:")
password = input("请输入密码:")
if card_number in locked_cards:
print("卡号已被锁定,请联系客服解锁。")
return
user = users.get(card_number)
if user and user["password"] == password:
locked_cards.append(card_number)
print("卡号已被锁定。")
else:
print("卡号或密码错误!")
解锁功能允许用户根据卡号和密码执行解锁功能。解锁后,该卡可以进行查询、取款、存款、转账等操作。以下是实现解锁功能的代码:
# 解锁功能
def unlock_card():
card_number = input("请输入卡号:")
password = input("请输入密码:")
user = users.get(card_number)
if user and user["password"] == password:
if card_number in locked_cards:
locked_cards.remove(card_number)
print("卡号已解锁。")
else:
print("该卡号未被锁定。")
else:
print("卡号或密码错误!")
存盘功能允许将程序执行的数据写入本地文件中。以下是实现存盘功能的代码:
# 存盘功能
def save_data():
with open("bank_data.txt", "w") as file:
for card_number, user in users.items():
file.write(
f"{card_number},{user['name']},{user['id_number']},{user['phone']},{user['balance']},{user['password']}\n")
with open("locked_cards.txt", "w") as file:
file.write("\n".join(locked_cards))
print("数据已保存到本地文件。")
加载存盘的数据功能可以在程序启动时将之前保存的用户数据读取到内存中。以下是实现加载存盘数据功能的代码:
# 加载存盘的数据
def load_data():
global users, locked_cards
with open("bank_data.txt", "r") as file:
for line in file:
card_number, name, id_number, phone, balance, password = line.strip().split(",")
users[card_number] = {
"name": name,
"id_number": id_number,
"phone": phone,
"balance": float(balance),
"password": password
}
with open("locked_cards.txt", "r") as file:
for line in file:
locked_cards.append(line.strip())
退出功能要求输入管理员账户密码。如果输入的账号密码错误,则返回功能页面;如果输入的账号密码正确,则执行存盘并退出系统。以下是实现退出功能的代码:
# 登录或退出功能
def login_or_out(option):
admin_account = "admin"
admin_password = "admin123"
account = input("请输入管理员账号:")
password = input("请输入管理员密码:")
if account == admin_account and password == admin_password:
if option == 1:
load_data()
print("登录成功!")
# continue with normal operations
elif option == 2:
save_data()
print("已退出系统。")
exit()
else:
print("无效的选项!请传入1或2。")
else:
print("管理员账号或密码错误!")
主程序用于提供用户界面和选项选择,根据用户的选择调用相应的功能函数。以下是主程序的代码:
login_or_out(1)
while True:
print("=============== 欢迎使用银行管理系统 ===============")
print("1. 开户 2. 查询 3. 取款")
print("4. 存款 5. 转账 6. 锁定 ")
print("7. 解锁 8. 存盘 9. 退出")
print("================================================")
choice = input("请输入您的选择(数字):")
if choice == "1":
open_account()
elif choice == "2":
inquire()
elif choice == "3":
withdraw()
elif choice == "4":
deposit()
elif choice == "5":
transfer()
elif choice == "6":
lock_card()
elif choice == "7":
unlock_card()
elif choice == "8":
save_data()
elif choice == "9":
login_or_out(2)
else:
print("无效的选择!")
以上是一个简单的银行管理系统的Python实现,通过以上代码可以实现开户、查询、取款、存款、转账、锁定、解锁、存盘和退出等功能。根据实际需求,你可以在此基础上进一步完善和优化代码。
import random
# 存储用户信息的字典
users = {}
# 锁定的卡号列表
locked_cards = []
# 当前登录的用户
current_user = None
# 开户功能
def open_account():
name = input("请输入姓名:")
id_number = input("请输入身份证号:")
phone = input("请输入手机号:")
balance = float(input("请输入预存金额:"))
password = input("请输入密码:")
card_number = generate_card_number()
users[card_number] = {
"name": name,
"id_number": id_number,
"phone": phone,
"balance": balance,
"password": password
}
print("开户成功!您的卡号是:", card_number)
# 查询功能
def inquire():
card_number = input("请输入卡号:")
password = input("请输入密码:")
if card_number in locked_cards:
print("该卡号已被锁定,请联系银行解锁。")
return
user = users.get(card_number)
if user and user["password"] == password:
print("您的余额为:", user["balance"])
else:
print("卡号或密码错误!")
if card_number in users:
if card_number not in locked_cards:
users[card_number].setdefault("wrong_password_count", 0)
users[card_number]["wrong_password_count"] += 1
if users[card_number]["wrong_password_count"] >= 3:
locked_cards.append(card_number)
print("连续输错密码已达3次,该卡已被锁定。")
# 取款功能
def withdraw():
card_number = input("请输入卡号:")
password = input("请输入密码:")
if card_number in locked_cards:
print("该卡号已被锁定,请联系银行解锁。")
return
user = users.get(card_number)
if user and user["password"] == password:
print("您的余额为:", user["balance"])
amount = float(input("请输入取款金额:"))
if amount > user["balance"] or amount < 0:
print("取款金额无效!")
else:
user["balance"] -= amount
print("取款成功!")
print("当前余额为:", user["balance"])
else:
print("卡号或密码错误!")
if card_number in users:
if card_number not in locked_cards:
users[card_number].setdefault("wrong_password_count", 0)
users[card_number]["wrong_password_count"] += 1
if users[card_number]["wrong_password_count"] >= 3:
locked_cards.append(card_number)
print("连续输错密码已达3次,该卡已被锁定。")
# 存款功能
def deposit():
card_number = input("请输入卡号:")
password = input("请输入密码:")
if card_number in locked_cards:
print("该卡号已被锁定,请联系银行解锁。")
return
user = users.get(card_number)
if user and user["password"] == password:
print("您的余额为:", user["balance"])
amount = float(input("请输入存款金额:"))
if amount < 0:
print("存款金额无效!")
else:
user["balance"] += amount
print("存款成功!")
print("当前余额为:", user["balance"])
else:
print("卡号或密码错误!")
if card_number in users:
if card_number not in locked_cards:
users[card_number].setdefault("wrong_password_count", 0)
users[card_number]["wrong_password_count"] += 1
if users[card_number]["wrong_password_count"] >= 3:
locked_cards.append(card_number)
print("连续输错密码已达3次,该卡已被锁定。")
# 转账功能
def transfer():
from_card_number = input("请输入转出卡号:")
from_password = input("请输入密码:")
if from_card_number in locked_cards:
print("该卡号已被锁定,请联系银行解锁。")
return
from_user = users.get(from_card_number)
if from_user and from_user["password"] == from_password:
print("您的余额为:", from_user["balance"])
to_card_number = input("请输入转入卡号:")
to_user = users.get(to_card_number)
if to_card_number in locked_cards:
print("对方卡号已被锁定,无法转账。")
return
if not to_user:
print("转入卡号不存在,无法转账。")
return
amount = float(input("请输入转账金额:"))
if amount > from_user["balance"] or amount < 0:
print("转账金额无效!")
return
confirm = input("确认执行转账功能?(Y/N)")
if confirm.lower() == "y":
from_user["balance"] -= amount
to_user["balance"] += amount
print("转账成功!")
print("当前余额为:", from_user["balance"])
else:
print("已取消转账功能。")
else:
print("卡号或密码错误!")
if from_card_number in users:
if from_card_number not in locked_cards:
users[from_card_number].setdefault("wrong_password_count", 0)
users[from_card_number]["wrong_password_count"] += 1
if users[from_card_number]["wrong_password_count"] >= 3:
locked_cards.append(from_card_number)
print("连续输错密码已达3次,该卡已被锁定。")
# 锁定功能
def lock_card():
card_number = input("请输入卡号:")
password = input("请输入密码:")
if card_number in locked_cards:
print("卡号已被锁定,请联系客服解锁。")
return
user = users.get(card_number)
if user and user["password"] == password:
locked_cards.append(card_number)
print("卡号已被锁定。")
else:
print("卡号或密码错误!")
# 解锁功能
def unlock_card():
card_number = input("请输入卡号:")
password = input("请输入密码:")
user = users.get(card_number)
if user and user["password"] == password:
if card_number in locked_cards:
locked_cards.remove(card_number)
print("卡号已解锁。")
else:
print("该卡号未被锁定。")
else:
print("卡号或密码错误!")
# 生成不重复的6位数字卡号
def generate_card_number():
card_number = str(random.randint(100000, 999999))
while card_number in users:
card_number = str(random.randint(100000, 999999))
return card_number
# 存盘功能
def save_data():
with open("bank_data.txt", "w") as file:
for card_number, user in users.items():
file.write(
f"{card_number},{user['name']},{user['id_number']},{user['phone']},{user['balance']},{user['password']}\n")
with open("locked_cards.txt", "w") as file:
file.write("\n".join(locked_cards))
print("数据已保存到本地文件。")
#加载存盘的数据
def load_data():
global users, locked_cards
with open("bank_data.txt", "r") as file:
for line in file:
card_number, name, id_number, phone, balance, password = line.strip().split(",")
users[card_number] = {
"name": name,
"id_number": id_number,
"phone": phone,
"balance": float(balance),
"password": password
}
with open("locked_cards.txt", "r") as file:
for line in file:
locked_cards.append(line.strip())
# 退出功能
def login_or_out(option):
admin_account = "admin"
admin_password = "admin123"
account = input("请输入管理员账号:")
password = input("请输入管理员密码:")
if account == admin_account and password == admin_password:
if option == 1:
load_data()
print("登录成功!")
# continue with normal operations
elif option == 2:
save_data()
print("已退出系统。")
exit()
else:
print("无效的选项!请传入1或2。")
else:
print("管理员账号或密码错误!")
# 主程序
login_or_out(1)
while True:
print("=============== 欢迎使用银行管理系统 ===============")
print("1. 开户 2. 查询 3. 取款")
print("4. 存款 5. 转账 6. 锁定 ")
print("7. 解锁 8. 存盘 9. 退出")
print("================================================")
choice = input("请输入您的选择(数字):")
if choice == "1":
open_account()
elif choice == "2":
inquire()
elif choice == "3":
withdraw()
elif choice == "4":
deposit()
elif choice == "5":
transfer()
elif choice == "6":
lock_card()
elif choice == "7":
unlock_card()
elif choice == "8":
save_data()
elif choice == "9":
login_or_out(2)
else:
print("无效的选择!")