python银行管理系统代码
时间: 2023-09-29 09:08:07 浏览: 83
好的,下面是一个 Python 银行管理系统的示例代码,包括一个 `BankAccount` 类和一个 `Bank` 类:
```python
class BankAccount:
def __init__(self, account_number, name, balance=0.0):
self.account_number = account_number
self.name = name
self.balance = balance
def deposit(self, amount):
self.balance += amount
print("Deposit successful. New balance is:", self.balance)
def withdraw(self, amount):
if self.balance < amount:
print("Withdrawal unsuccessful. Insufficient balance.")
else:
self.balance -= amount
print("Withdrawal successful. New balance is:", self.balance)
def check_balance(self):
print("Your current balance is:", self.balance)
class Bank:
def __init__(self, name):
self.name = name
self.accounts = {}
def add_account(self, account_number, name, balance=0.0):
if account_number in self.accounts:
print("Account already exists.")
else:
self.accounts[account_number] = BankAccount(account_number, name, balance)
print("Account created successfully.")
def remove_account(self, account_number):
if account_number in self.accounts:
del self.accounts[account_number]
print("Account removed successfully.")
else:
print("Account not found.")
def deposit(self, account_number, amount):
if account_number in self.accounts:
self.accounts[account_number].deposit(amount)
else:
print("Account not found.")
def withdraw(self, account_number, amount):
if account_number in self.accounts:
self.accounts[account_number].withdraw(amount)
else:
print("Account not found.")
def check_balance(self, account_number):
if account_number in self.accounts:
self.accounts[account_number].check_balance()
else:
print("Account not found.")
def main():
bank = Bank("Python Bank")
while True:
print("1. Create account")
print("2. Remove account")
print("3. Deposit")
print("4. Withdraw")
print("5. Check balance")
print("6. Quit")
choice = int(input("Enter your choice: "))
if choice == 1:
account_number = input("Enter account number: ")
name = input("Enter name: ")
balance = float(input("Enter initial balance (optional): ") or 0)
bank.add_account(account_number, name, balance)
elif choice == 2:
account_number = input("Enter account number: ")
bank.remove_account(account_number)
elif choice == 3:
account_number = input("Enter account number: ")
amount = float(input("Enter amount to deposit: "))
bank.deposit(account_number, amount)
elif choice == 4:
account_number = input("Enter account number: ")
amount = float(input("Enter amount to withdraw: "))
bank.withdraw(account_number, amount)
elif choice == 5:
account_number = input("Enter account number: ")
bank.check_balance(account_number)
elif choice == 6:
print("Thank you for banking with us!")
break
else:
print("Invalid choice. Please try again.")
if __name__ == '__main__':
main()
```
此代码定义了一个 `Bank` 类和一个 `BankAccount` 类,用于管理银行账户。在 `main()` 函数中,用户可以执行以下操作:
1. 创建账户
2. 删除账户
3. 存款
4. 取款
5. 查询余额
6. 退出程序
用户选择要执行的操作后,程序将相应地调用 `Bank` 类中的方法。
以上是一个 Python 银行管理系统的示例代码,可以根据自己的需要进行修改和扩展。
阅读全文