Implementiere eine Python-Klasse, um ein Bibliotheksverwaltungssystem zu simulieren
Wir werden eine einfache Python-Klasse erstellen, um ein Bibliotheksverwaltungssystem zu simulieren. Dieses System ermöglicht es Benutzern, Bücher hinzuzufügen, Bücher zu löschen, Bücher zu finden und alle Bücher aufzulisten. Jedes Buch hat zwei Attribute: Titel und Autor.
Beispiel
class Book:
def __init__(self, title, author):
self.title = title
self.author = author
def __str__(self):
return f"{self.title} von {self.author}"
class Library:
def __init__(self):
self.books = []
def add_book(self, title, author):
new_book = Book(title, author)
self.books.append(new_book)
print(f"Hinzugefügt: {new_book}")
def remove_book(self, title):
for book in self.books:
if book.title == title:
self.books.remove(book)
print(f"Entfernt: {book}")
return
print(f"Buch mit dem Titel '{title}' nicht gefunden.")
def find_book(self, title):
for book in self.books:
if book.title == title:
print(f"Gefunden: {book}")
return book
print(f"Buch mit dem Titel '{title}' nicht gefunden.")
return None
def list_books(self):
if not self.books:
print("Keine Bücher in der Bibliothek.")
else:
print("Bücher in der Bibliothek:")
for book in self.books:
print(book)
# Beispielverwendung
library = Library()
library.add_book("1984", "George Orwell")
library.add_book("To Kill a Mockingbird", "Harper Lee")
library.list_books()
library.find_book("1984")
library.remove_book("1984")
library.list_books()
def __init__(self, title, author):
self.title = title
self.author = author
def __str__(self):
return f"{self.title} von {self.author}"
class Library:
def __init__(self):
self.books = []
def add_book(self, title, author):
new_book = Book(title, author)
self.books.append(new_book)
print(f"Hinzugefügt: {new_book}")
def remove_book(self, title):
for book in self.books:
if book.title == title:
self.books.remove(book)
print(f"Entfernt: {book}")
return
print(f"Buch mit dem Titel '{title}' nicht gefunden.")
def find_book(self, title):
for book in self.books:
if book.title == title:
print(f"Gefunden: {book}")
return book
print(f"Buch mit dem Titel '{title}' nicht gefunden.")
return None
def list_books(self):
if not self.books:
print("Keine Bücher in der Bibliothek.")
else:
print("Bücher in der Bibliothek:")
for book in self.books:
print(book)
# Beispielverwendung
library = Library()
library.add_book("1984", "George Orwell")
library.add_book("To Kill a Mockingbird", "Harper Lee")
library.list_books()
library.find_book("1984")
library.remove_book("1984")
library.list_books()
Code-Erklärung:
BookKlasse: Stellt ein Buch dar, das den Titel enthält (title) und Autor (author) zwei Attribute.__str__Methode zur Rückgabe der Stringdarstellung eines Buches.LibraryKlasse: Stellt eine Bibliothek dar, die eine Liste von Büchern enthält (books). Bietet die folgenden Methoden:add_book(title, author): Fügt ein Buch zur Bibliothek hinzu.remove_book(title): Löscht ein Buch anhand des Titels aus der Bibliothek.find_book(title): Sucht ein Buch anhand des Titels.list_books(): Listet alle Bücher in der Bibliothek auf.
- Beispielverwendung: Erstellen Sie eine
LibraryInstanz, fügen Sie zwei Bücher hinzu, listen Sie alle Bücher auf, suchen und löschen Sie ein Buch und listen Sie dann alle Bücher erneut auf.
Ausgabeergebnis:
Added: 1984 by George Orwell Added: To Kill a Mockingbird by Harper Lee Books in the library: 1984 by George Orwell To Kill a Mockingbird by Harper Lee Found: 1984 by George Orwell Removed: 1984 by George Orwell Books in the library: To Kill a Mockingbird by Harper LeeWeitere Erweiterungen
Python3-Beispiele