Python实现ATM
时间: 2024-05-16 16:09:59 浏览: 12
Python可以通过面向对象编程来实现ATM,以下是一个简单的实现方式:
1. 创建一个账户类(Account),用于存储账户信息、余额和交易记录等。
2. 创建一个ATM机类(ATM),用于处理用户的交易请求,包括取款、存款、查询余额等操作。
3. 在ATM类中,需要实现用户认证、账户锁定、密码重置等功能。
4. 在主函数中,实例化ATM类对象,并提供一个简单的控制台界面,让用户选择不同的操作。
以下是一个简单的示例代码:
```
class Account:
def __init__(self, name, account_number, password, balance=0):
self.name = name
self.account_number = account_number
self.password = password
self.balance = balance
self.transaction_history = []
def deposit(self, amount):
self.balance += amount
self.transaction_history.append(f"Deposit: {amount}")
def withdraw(self, amount):
if amount > self.balance:
return "Insufficient funds"
else:
self.balance -= amount
self.transaction_history.append(f"Withdrawal: {amount}")
def get_balance(self):
return self.balance
def get_transaction_history(self):
return self.transaction_history
class ATM:
def __init__(self):
self.accounts = []
def create_account(self, name, account_number, password):
new_account = Account(name, account_number, password)
self.accounts.append(new_account)
def authenticate(self, account_number, password):
for account in self.accounts:
if account.account_number == account_number and account.password == password:
return account
return None
def reset_password(self, account_number, new_password):
account = self.authenticate(account_number, "")
if account:
account.password = new_password
return True
else:
return False
def lock_account(self, account_number):
account = self.authenticate(account_number, "")
if account:
account.password = ""
return True
else:
return False
def unlock_account(self, account_number, password):
account = self.authenticate(account_number, password)
if account:
return True
else:
return False
def deposit(self, account_number, amount):
account = self.authenticate(account_number, "")
if account:
account.deposit(amount)
return True
else:
return False
def withdraw(self, account_number, amount):
account = self.authenticate(account_number, "")
if account:
result = account.withdraw(amount)
if result == "Insufficient funds":
return False
else:
return True
else:
return False
def main():
atm = ATM()
while True:
print("1. Create Account")
print("2. Login")
print("3. Reset Password")
print("4. Lock Account")
print("5. Unlock Account")
print("6. Deposit")
print("7. Withdraw")
print("8. Check Balance")
print("9. Transaction History")
print("0. Exit")
choice = input("Enter choice: ")
if choice == "1":
name = input("Enter name: ")
account_number = input("Enter account number: ")
password = input("Enter password: ")
atm.create_account(name, account_number, password)
print("Account created successfully")
elif choice == "2":
account_number = input("Enter account number: ")
password = input("Enter password: ")
account = atm.authenticate(account_number, password)
if account:
while True:
print("1. Reset Password")
print("2. Lock Account")
print("3. Unlock Account")
print("4. Deposit")
print("5. Withdraw")
print("6. Check Balance")
print("7. Transaction History")
print("0. Exit")
choice = input("Enter choice: ")
if choice == "1":
new_password = input("Enter new password: ")
result = atm.reset_password(account_number, new_password)
if result:
print("Password reset successful")
else:
print("Account not found")
elif choice == "2":
result = atm.lock_account(account_number)
if result:
print("Account locked successfully")
else:
print("Account not found")
elif choice == "3":
password = input("Enter password: ")
result = atm.unlock_account(account_number, password)
if result:
print("Account unlocked successfully")
else:
print("Invalid password")
elif choice == "4":
amount = int(input("Enter amount: "))
result = atm.deposit(account_number, amount)
if result:
print("Deposit successful")
else:
print("Account not found")
elif choice == "5":
amount = int(input("Enter amount: "))
result = atm.withdraw(account_number, amount)
if result:
print("Withdrawal successful")
else:
print("Insufficient funds or account not found")
elif choice == "6":
balance = account.get_balance()
print(f"Balance: {balance}")
elif choice == "7":
transaction_history = account.get_transaction_history()
for transaction in transaction_history:
print(transaction)
elif choice == "0":
break
else:
print("Invalid choice")
else:
print("Invalid credentials")
elif choice == "0":
break
else:
print("Invalid choice")
if __name__ == "__main__":
main()
```