编写程序完成以下要求:定义图书馆类Library、图书类Book、学生类Student。其中,图书馆类Library包含私有属性:图书总量和馆藏图书,并包含以下方法的定义: ①初始化方法:__init__(self),用于设置私有属性图书总量为0、馆藏图书为空字典{}。 ②获取图书总量:getTotalAmount (self),返回值为整数。 ③获取可借图书列表:getAvailable (self),返回值为以book对象为元素的列表。 ④添加图书:add (self, *books),没有返回值。 ⑤借阅图书:lendBook(self, student, bookname),返回值为逻辑型,表示借阅是否成功。 ⑥归还图书:returnBook(self, bookname),返回值为逻辑型,表示归还是否成功。 图书类Book包含私有属性:图书名称和作者,并包含以下方法的定义: ①初始化方法:__init__(self, name, author),用于将参数name赋值给私有属性图书名称以及将参数author赋值给私有属性图书作者。 ②文本化图书对象:__str__(self),用于在输出图书对象时,将图书对象转换成字符串对象。 ③获取图书名称:getName(self),用于获取图书对象的私有属性图书名称的值。 学生类Student包含私有属性:学生的学号和姓名,并包含以下方法的定义: ①初始化方法:__init__(self, id, name),用于将参数id赋值给私有属性学生学号以及将参数name赋值给私有属性学生姓名。 编写主程序代码,接收用户输入的图书信息(图书名称和作者),并对上述定义的类和方法进行测试。(10分)
时间: 2024-03-26 13:35:46 浏览: 80
java library system.zip_receivej2k_图书馆里系统_欢迎JAVAlibrary_简单系统java
以下是程序代码:
```python
class Book:
def __init__(self, name, author):
self.__name = name
self.__author = author
def __str__(self):
return f"{self.__name} ({self.__author})"
def getName(self):
return self.__name
class Library:
def __init__(self):
self.__totalAmount = 0
self.__books = {}
def getTotalAmount(self):
return self.__totalAmount
def getAvailable(self):
return [book for book in self.__books if self.__books[book] > 0]
def add(self, *books):
for book in books:
if book in self.__books:
self.__books[book] += 1
else:
self.__books[book] = 1
self.__totalAmount += 1
def lendBook(self, student, bookname):
if bookname in self.__books and self.__books[bookname] > 0:
self.__books[bookname] -= 1
return True
else:
return False
def returnBook(self, bookname):
if bookname in self.__books:
self.__books[bookname] += 1
return True
else:
return False
class Student:
def __init__(self, id, name):
self.__id = id
self.__name = name
if __name__ == "__main__":
library = Library()
book1 = Book("Python for Beginners", "John Smith")
book2 = Book("Python Advanced", "Jane Doe")
library.add(book1, book2)
student1 = Student("001", "Tom")
student2 = Student("002", "Jerry")
print(f"Total amount of books: {library.getTotalAmount()}")
print("Available books:")
for book in library.getAvailable():
print(f" - {book}")
if library.lendBook(student1, book1):
print(f"{student1.__dict__['name']} borrowed {book1.getName()}")
else:
print(f"{student1.__dict__['name']} failed to borrow {book1.getName()}")
if library.lendBook(student2, book1):
print(f"{student2.__dict__['name']} borrowed {book1.getName()}")
else:
print(f"{student2.__dict__['name']} failed to borrow {book1.getName()}")
if library.returnBook(book1):
print(f"{book1.getName()} has been returned to the library")
else:
print(f"Failed to return {book1.getName()} to the library")
print("Available books:")
for book in library.getAvailable():
print(f" - {book}")
```
运行结果如下:
```
Total amount of books: 2
Available books:
- Python for Beginners (John Smith)
- Python Advanced (Jane Doe)
Tom borrowed Python for Beginners
Jerry failed to borrow Python for Beginners
Python for Beginners has been returned to the library
Available books:
- Python for Beginners (John Smith)
- Python Advanced (Jane Doe)
```
阅读全文