import random
import datetime
class ATM:
def init(self):
self.users = {}
self.logged_in_user = None
self.bank_info = {'total_balance': 1000000}
self.transaction_log = []
def card_number(self):
return random.randint(1000, 9999)
def register(self):
name = input("请输入姓名:>>>>>>")
password = input("请输入密码:>>>>>>")
card_number = self.card_number()
user = {'name': name, 'password': password, 'card_number': card_number, 'balance': 0, 'activated': False}
self.users[card_number] = user
print(f'注册成功!你的卡号是:>>>>>{card_number}')
def login(self):
card_number = input('请输入卡号:>>>>>>>')
password = input('请输入密码:>>>>>>>')
user = self.users.get(int(card_number))
if user and user['password'] == password and user['activated']:
self.logged_in_user = user
print(f"欢迎{user['name']}!")
else:
print('登录失败')
def activate_card(self):
card_number = input("请输入卡号:>>>>>>>")
user = self.users.get(int(card_number))
if user:
user['activated'] = True
print("银行卡激活成功")
else:
print('卡号不存在,重新输入')
def draw_money(self):
amount = float(input("输入取款金额:>>>>>>"))
if amount > 0 and amount <= self.logged_in_user['balance']:
self.logged_in_user['balance'] -= amount
self.bank_info['total_balance'] -= amount
self.transaction_log.append(f'{datetime.datetime.now()}:取款{amount}元')
print('取款成功')
else:
print('取款失败,检查金额或余额是否足够')
def transfer_money(self):
other_card_number = input("请输入对方卡号:>>>>>>")
amount = float(input("请输入转账金额:>>>>>>>"))
other_card = self.users.get(int(other_card_number))
if other_card and amount > 0 and amount <= self.logged_in_user['balance']:
self.logged_in_user['balance'] -= amount
other_card['balance'] += amount
self.transaction_log.append(f"{datetime.datetime.now()}:转账{amount}元给卡号{other_card_number}")
print('转账成功')
else:
print('转账失败,检查对方卡号或你的余额')
def recharge_money(self):
amount = float(input('请输入充值金额:>>>>>>'))
if amount > 0:
self.logged_in_user['balance'] += amount
self.bank_info['total_balance'] += amount
self.transaction_log.append(f"{datetime.datetime.now()}: 充值 {amount} 元")
print("充值成功!")
else:
print('充值失败,输入正确的充值金额')
def search_history(self):
print('交易流水')
for log in self.transaction_log:
print(log)
def search_bank_info(self):
print("银行信息:")
print(f"总余额: {self.bank_info['total_balance']}")
print(f"你的卡号: {self.logged_in_user['card_number']}")
print(f"你的余额: {self.logged_in_user['balance']}")
self.search_history()
def menu(self):
while True:
print("\n===================用户功能菜单=====================")
print("1. 注册")
print("2. 登陆")
print("3. 激活银行卡")
print("4. 取款")
print("5. 转账")
print("6. 充值余额")
print("7. 查看流水")
print("8. 查看日志")
print("9. 查看银行信息")
print("======================欢迎使用=======================")
choice = input("请选择操作: ")
if choice == '1':
self.register()
elif choice == '2':
self.login()
elif choice == '3':
self.activate_card()
elif choice == '4':
self.draw_money()
elif choice == '5':
self.transfer_money()
elif choice == '6':
self.recharge_money()
elif choice == '7':
self.search_history()
elif choice == '8':
print("日志:")
for log in self.transaction_log:
print(log)
elif choice == '9':
if self.logged_in_user:
self.search_bank_info()
else:
print("请先登录!")
else:
print("无效的选择,请重新输入。")
if __name__ == "__main__":
atm = ATM()
atm.menu()
标签:self,ATM,number,choice,考试,print,card,user
From: https://www.cnblogs.com/dhyuan/p/17935641.html