Python图书管理系统大作业代码
时间: 2023-11-05 07:03:11 浏览: 59
由于没有提供具体要求,以下是一个简单的Python图书管理系统代码:
```python
class Book:
def __init__(self, title, author, year):
self.title = title
self.author = author
self.year = year
def __str__(self):
return f"{self.title} ({self.author}, {self.year})"
class Library:
def __init__(self):
self.books = []
def add_book(self, book):
self.books.append(book)
def remove_book(self, book):
if book in self.books:
self.books.remove(book)
print(f"{book} removed from library")
else:
print(f"{book} not found in library")
def search_book(self, title):
for book in self.books:
if book.title == title:
print(f"{book} found in library")
return book
print(f"{title} not found in library")
return None
def display_books(self):
if self.books:
print("Library Books:")
for book in self.books:
print(f"- {book}")
else:
print("No books in library")
library = Library()
while True:
print("\nMenu:")
print("1. Add book")
print("2. Remove book")
print("3. Search book")
print("4. Display books")
print("0. Exit")
choice = input("Enter your choice: ")
if choice == "0":
break
elif choice == "1":
title = input("Enter book title: ")
author = input("Enter book author: ")
year = input("Enter book year: ")
book = Book(title, author, year)
library.add_book(book)
print(f"{book} added to library")
elif choice == "2":
title = input("Enter book title: ")
book = library.search_book(title)
if book:
library.remove_book(book)
elif choice == "3":
title = input("Enter book title: ")
library.search_book(title)
elif choice == "4":
library.display_books()
else:
print("Invalid choice")
```
这个代码实现了一个简单的图书管理系统,其中包括添加书籍、删除书籍、查找书籍和显示所有书籍等功能。用户可以通过命令行菜单选择需要执行的操作。
阅读全文