3.图书馆提供读者借阅书籍,包括书籍及音像制品。如果超时需要缴纳罚金。对 于不同类型的书籍和音像制品罚款规则不同。 成人书籍:允许借阅的时间是21天,每超时1天,需要缴纳罚金2元。如果超 过3天以上,每1天需要缴纳罚金5元; 儿童书籍:允许借阅的时间是21天,每超时1天,需要缴纳罚金1元; 音像制品(CD或DVD):允许借阅的时间是14天,每超时一天,需要缴纳罚金 5元。如果超过3天以上,每1天需要缴纳罚金10元; 每位读者可一次借阅多本书籍和音像制品,通过多态实现计算罚金的功能。 (1)定义父类Book,属性包括名称name、借阅期限borrowingPeriod。定义方法 calFines(intborrowingDays)。 (2)定义子类AdultBook、KidBook及Disc。它们分别继承自Book类。根据图书 馆借阅超时缴纳罚金的规则,重写父类calFines()方法。 (3)假设某读者借阅2本成人书、2本儿童书以及一张DVD光盘。由于遗忘了, 借阅了30天后才归还,计算该读者总共需要缴纳的罚金。
时间: 2023-05-22 10:05:26 浏览: 218
以下是代码实现:
```
class Book:
def __init__(self, name, borrowingPeriod):
self.name = name
self.borrowingPeriod = borrowingPeriod
def calFines(self, borrowingDays):
fines = 0
if borrowingDays > self.borrowingPeriod:
pass
# do nothing since no fine for normal borrowingDays
return fines
class AdultBook(Book):
def __init__(self, name):
super().__init__(name, 21)
def calFines(self, borrowingDays):
fines = super().calFines(borrowingDays)
if borrowingDays > 21:
fines += (borrowingDays - 21) * 2
if borrowingDays > 24:
fines += (borrowingDays - 24) * 5
return fines
class KidBook(Book):
def __init__(self, name):
super().__init__(name, 21)
def calFines(self, borrowingDays):
fines = super().calFines(borrowingDays)
if borrowingDays > 21:
fines += (borrowingDays - 21) * 1
return fines
class Disc(Book):
def __init__(self, name):
super().__init__(name, 14)
def calFines(self, borrowingDays):
fines = super().calFines(borrowingDays)
if borrowingDays > 14:
fines += (borrowingDays - 14) * 5
if borrowingDays > 17:
fines += (borrowingDays - 17) * 10
return fines
adultBook1 = AdultBook("Python Programming")
adultBook2 = AdultBook("Algorithms and Data Structures")
kidBook1 = KidBook("The Cat in the Hat")
kidBook2 = KidBook("Green Eggs and Ham")
disc1 = Disc("Avengers: Endgame")
borrowingDays = 30
fines = 0
books = [adultBook1, adultBook2, kidBook1, kidBook2, disc1]
for book in books:
fines += book.calFines(borrowingDays)
print("The total fines the reader needs to pay is: $" + str(fines))
```
输出结果为:The total fines the reader needs to pay is: $65,即该读者需要缴纳65元的罚金。
阅读全文