使用python生成一个ATM机的系统
时间: 2023-07-15 14:12:51 浏览: 108
python3实现ATM机功能
5星 · 资源好评率100%
好的,下面是一个简单的ATM机系统代码示例:
```python
# 定义账户类
class Account:
def __init__(self, account_number, pin, balance):
self.account_number = account_number
self.pin = pin
self.balance = balance
def withdraw(self, amount):
if self.balance < amount:
print("余额不足")
else:
self.balance -= amount
print("取款成功,余额为", self.balance)
def deposit(self, amount):
self.balance += amount
print("存款成功,余额为", self.balance)
def check_balance(self):
print("当前余额为", self.balance)
# 定义ATM机类
class ATM:
def __init__(self, accounts):
self.accounts = accounts
def validate_pin(self, account_number, pin):
for account in self.accounts:
if account.account_number == account_number and account.pin == pin:
return True
return False
def display_menu(self):
print("请选择操作:")
print("1. 取款")
print("2. 存款")
print("3. 查询余额")
def run(self):
account_number = input("请输入账户号码:")
pin = input("请输入密码:")
if not self.validate_pin(account_number, pin):
print("账户号码或密码错误")
return
print("登录成功")
while True:
self.display_menu()
choice = input("请选择操作(输入数字1-3):")
if choice == "1":
amount = int(input("请输入取款金额:"))
for account in self.accounts:
if account.account_number == account_number:
account.withdraw(amount)
elif choice == "2":
amount = int(input("请输入存款金额:"))
for account in self.accounts:
if account.account_number == account_number:
account.deposit(amount)
elif choice == "3":
for account in self.accounts:
if account.account_number == account_number:
account.check_balance()
else:
print("无效的选项,请重新选择")
# 创建账户
accounts = [
Account("123456", "1234", 1000),
Account("654321", "4321", 2000),
]
# 运行ATM机
atm = ATM(accounts)
atm.run()
```
以上代码实现了一个简单的ATM机系统,包括账户类和ATM机类。用户可以输入账户号码和密码登录,然后选择取款、存款或查询余额等操作。请注意,此代码仅供参考,并且可能需要根据您的具体需求进行修改。
阅读全文