【python】练习:银行管理系统

银行管理系统是一个集开户、查询、取款、存款、转账、锁定、解锁、存盘和退出等一系列的功能的管理系统。

#coding=utf-8
import os
import pickle#可以让字典序列化

#定义账户类
class Account:
    def __init__(self, name, password, balance=0, status="normal"):#账号,密码,余额,状态
        self.name = name
        self.password = password
        self.balance = balance
        self.status = status

#定义显示函数,显示账户基本信息
    def __str__(self):
        return "--- Account Info ---\n" \
               "账户: {}\n" \
               "密码: {}\n" \
               "余额: {}\n" \
               "账户: {}".format(self.name, '*'*len(self.password), self.balance, self.status)

#定义银行类
class Bank:
    def __init__(self):
        self.accounts = {}  # 存放所有账户信息的字典
        self.filename = "bank.txt"  # 存档文件名

    def verify(self, name, password):#判断账户和密码是否正确,如果true进入功能列表
        if name in self.accounts and self.accounts[name].password == password:
            return True
        else:
            return False

    def create_account(self, name, password):#定义开户
        if name in self.accounts:
            return "账户已存在"
        else:
            self.accounts[name] = Account(name, password)
            return "账户创建成功"

    def query_account(self, name):#定义查询
        if name in self.accounts:
            return str(self.accounts[name])
        else:
            return "账户不存在"

    def withdraw(self, name, amount):#定义取款
        if name in self.accounts:
            if self.accounts[name].status == "normal":
                if self.accounts[name].balance >= amount:
                    self.accounts[name].balance -= amount
                    return "取款成功"
                else:
                    return "余额不足"
            else:
                return "账户已被锁定"
        else:
            return "账户不存在"

    def deposit(self, name, amount):#定义存款
        if name in self.accounts:
            if self.accounts[name].status == "normal":
                self.accounts[name].balance += amount
                return "存款成功"
            else:
                return "账户已被锁定"
        else:
            return "账户不存在"

    def transfer(self, name1, name2, amount):#定义转账
        if name1 in self.accounts and name2 in self.accounts:
            if self.accounts[name1].status == "normal" and self.accounts[name2].status == "normal":
                if self.accounts[name1].balance >= amount:
                    self.accounts[name1].balance -= amount
                    self.accounts[name2].balance += amount
                    return "转账成功"
                else:
                    return "您的余额不足"
            else:
                return "账户已被锁定"
        else:
            return "账户不存在"

    def lock_account(self, name):#定义锁定
        if name in self.accounts:
            self.accounts[name].status = "locked"
            return "账户已锁定"
        else:
            return "账户不存在"

    def unlock_account(self, name):#定义解锁
        if name in self.accounts:
            self.accounts[name].status = "normal"
            return "账户已解锁"
        else:
            return "账户不存在"

    def save(self):#定义保存
        with open(self.filename, 'wb') as file:
            pickle.dump(self.accounts, file)

    def load(self):#定义退出
        if os.path.exists(self.filename):#判断对应文件或目录是否存在
            with open(self.filename, 'rb') as file:
                self.accounts = pickle.load(file)

bank = Bank()

def main():
    bank.load()

    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("请输入您的账号: ")
            password = input("请输入您的密码: ")
            print(bank.create_account(name, password))
        elif choice == "2":
            name = input("请输入您的账号: ")
            print(bank.query_account(name))
        elif choice == "3":
            name = input("请输入您的账号: ")
            amount = float(input("请输入您要取款金额: "))
            print(bank.withdraw(name, amount))
        elif choice == "4":
            name = input("请输入您的账号: ")
            amount = float(input("请输入您要存款金额: "))
            print(bank.deposit(name, amount))
        elif choice == "5":
            name1 = input("请输入您的账号: ")
            name2 = input("请输入要转账的账号 ")
            amount = float(input("请输入您要转账金额: "))
            print(bank.transfer(name1, name2, amount))
        elif choice == "6":
            name = input("请输入您的账号: ")
            print(bank.lock_account(name))
        elif choice == "7":
            name = input("请输入您的账号: ")
            print(bank.unlock_account(name))
        elif choice == "8":
            bank.save()
            print("已保存")
        elif choice == "9":
            bank.save()
            print("再见")
            break
        else:
            print("您的输入有误!!!")

if __name__ == '__main__':
    main()

你可能感兴趣的:(python,python)