为银行编写一个账户管理系统,假设用户的银行账户叫 BankAccount
,它有以下几个属性:
- 用户姓名(username)
- 账号(card_no)
- 余额(balance)
- 历史操作记录(history_lst)
用户可以在银行执行以下几个操作:
- 存钱(deposit)
- 取钱(withdrawal)
- 转账(transfer)
- 查看操作记录(history)
给定以下测试用例:
- 小明在银行的账户为 '123',余额 100 元。
- 小红在银行的账户为 '456',余额 200 元。
- 小明存入银行 100 元。
- 小红从银行取出 50 元。
- 小红向小明转账 50 元。
- 输出小红的历史操作记录。
class BankAccount:
def __init__(self, username, card_no, balance=0):
self.username = username
self.card_no = card_no
self.balance = balance
self.history_lst = []
def deposit(self, amount):
self.balance += amount
message = f"存款:{amount}元"
self.history_lst.append(message)
print(message)
def withdrawal(self, amount):
if amount > self.balance:
print("余额不足,无法取款")
else:
self.balance -= amount
message = f"取款:{amount}元"
self.history_lst.append(message)
print(message)
def transfer(self, other, amount):
if amount > self.balance:
print("余额不足,无法转账")
else:
self.balance -= amount
other.balance += amount
self.history_lst.append(f"转账:{amount}元给{other.username}")
print(f"转账:{amount}元给{other.username}")
def history(self):
for item in self.history_lst:
print(f"{self.username} 历史操作记录:{item}")
# 测试用例
xiaoming = BankAccount("小明", "123", 100)
xiaohong = BankAccount("小红", "456", 200)
xiaoming.deposit(100)
xiaohong.withdrawal(50)
xiaohong.transfer(xiaoming, 50)
xiaohong.history()
设计一个学生管理系统,包括以下功能:
- 添加学生:输入学生的姓名、年龄和成绩,将学生信息添加到系统中。
- 查询学生:输入学生的姓名,查询该学生的年龄和成绩。
- 修改学生:输入学生的姓名和新的年龄、成绩,修改该学生的信息。
- 删除学生:输入学生的姓名,从系统中删除该学生的信息。
- 统计学生数量:统计系统中学生的数量。
- 要求使用面向对象的方式实现学生管理系统,并使用类和对象的概念来表示学生和系统。
class Student:
def __init__(self, name, age, score):
self.name = name
self.age = age
self.score = score
class StudentSystem:
def __init__(self):
self.students = []
def add_student(self, name, age, score):
student = Student(name, age, score)
self.students.append(student)
print("添加学生成功!")
def query_student(self, name):
for student in self.students:
if student.name == name:
print(f"学生姓名:{student.name}")
print(f"学生年龄:{student.age}")
print(f"学生成绩:{student.score}")
return
print("学生不存在!")
def modify_student(self, name, age, score):
for student in self.students:
if student.name == name:
student.age = age
student.score = score
print("修改学生信息成功!")
return
print("学生不存在!")
def delete_student(self, name):
for student in self.students:
if student.name == name:
self.students.remove(student)
print("删除学生成功!")
return
print("学生不存在!")
def count_students(self):
count = len(self.students)
print(f"学生数量:{count}")