Python银行账户管理系统 - 使用Excel存储账户信息
import xlwt
import xlrd
import random
class Account:
def __init__(self, name, balance=0):
self.name = name
self.balance = balance
self.status = 'normal'
self.account_number = self.generate_account_number()
self.password = self.create_password()
def generate_account_number(self):
# 生成16位账号
account_number = ''
for i in range(16):
account_number += str(random.randint(0, 9))
return account_number
def create_password(self):
# 创建6位密码
password = input('请输入6位数字密码:')
while True:
confirm_password = input('请再次输入6位数字密码:')
if confirm_password == password:
return password
else:
print('两次输入密码不一致,请重新输入')
def verify_password(self):
# 验证密码是否正确
password = input('请输入密码:')
if password == self.password:
return True
else:
print('密码错误')
return False
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
if self.balance < amount:
print('余额不足')
else:
self.balance -= amount
def query_balance(self):
print('你的余额是', self.balance)
def lose_card(self):
self.status = 'lost'
def find_card(self):
self.status = 'normal'
def close_account(self):
self.status = 'closed'
def change_password(self):
# 修改密码
if self.verify_password():
new_password = self.create_password()
self.password = new_password
print('密码修改成功')
@staticmethod
def login(accounts):
# 登录
account_number = input('请输入账号:')
password = input('请输入密码:')
for name, account in accounts.items():
if account.account_number == account_number and account.password == password:
print('登录成功')
return account
print('账号或密码错误')
return None
def write_to_excel(accounts):
# 将账户信息写入到xls表格中
workbook = xlwt.Workbook(encoding='utf-8')
worksheet = workbook.add_sheet('account')
worksheet.write(0, 0, '姓名')
worksheet.write(0, 1, '账号')
worksheet.write(0, 2, '余额')
worksheet.write(0, 3, '状态')
worksheet.write(0, 4, '密码')
row = 1
for name, account in accounts.items():
worksheet.write(row, 0, name)
worksheet.write(row, 1, account.account_number)
worksheet.write(row, 2, account.balance)
worksheet.write(row, 3, account.status)
worksheet.write(row, 4, account.password)
row += 1
workbook.save('account.xls')
def read_from_excel():
# 从xls表格中读取账户信息
accounts = {}
workbook = xlrd.open_workbook('account.xls')
worksheet = workbook.sheet_by_name('account')
for row in range(1, worksheet.nrows):
name = worksheet.cell_value(row, 0)
account_number = worksheet.cell_value(row, 1)
balance = worksheet.cell_value(row, 2)
status = worksheet.cell_value(row, 3)
password = worksheet.cell_value(row, 4)
account = Account(name, balance)
account.status = status
account.account_number = account_number
account.password = password
accounts[name] = account
return accounts
def main():
try:
accounts = read_from_excel()
except:
# 如果xls表格不存在,则创建一个空表格
accounts = {}
write_to_excel(accounts)
while True:
print('欢迎来到银行')
print('1. 开立账户')
print('2. 存款')
print('3. 取款')
print('4. 查询余额')
print('5. 挂失')
print('6. 取消挂失')
print('7. 销户')
print('8. 修改密码')
print('9. 退出')
choice = input('请输入您的选择: ')
if choice == '1':
name = input('请输入您的姓名: ')
balance = 0 # 默认开户时余额为0
account = Account(name, balance)
accounts[name] = account # 保存账户信息
write_to_excel(accounts) # 将修改后的账户信息写入到xls表格中
print('您的账户已开通')
print('您的账号是', account.account_number)
elif choice == '2':
account = Account.login(accounts)
if account:
amount = int(input('输入要存入的金额: '))
account.deposit(amount)
write_to_excel(accounts) # 将修改后的账户信息写入到xls表格中
print('您的存款已成功存入')
elif choice == '3':
account = Account.login(accounts)
if account:
amount = int(input('请输入提取的金额: '))
account.withdraw(amount)
write_to_excel(accounts) # 将修改后的账户信息写入到xls表格中
print('您的取款成功了')
elif choice == '4':
account = Account.login(accounts)
if account:
account.query_balance()
elif choice == '5':
account = Account.login(accounts)
if account:
account.lose_card()
write_to_excel(accounts) # 将修改后的账户信息写入到xls表格中
print('您的卡片已丢失')
elif choice == '6':
account = Account.login(accounts)
if account:
account.find_card()
write_to_excel(accounts) # 将修改后的账户信息写入到xls表格中
print('您的卡片已找到')
elif choice == '7':
account = Account.login(accounts)
if account:
account.close_account()
del accounts[account.name]
write_to_excel(accounts) # 将修改后的账户信息写入到xls表格中
print('您的帐户已关闭')
elif choice == '8':
account = Account.login(accounts)
if account:
account.change_password()
write_to_excel(accounts) # 将修改后的账户信息写入到xls表格中
elif choice == '9':
break # 退出程序
else:
print('无效的选择')
if __name__ == '__main__':
main()
原文地址: https://www.cveoy.top/t/topic/oSWb 著作权归作者所有。请勿转载和采集!