Implémenter une classe Python pour simuler un système de gestion de bibliothèque
Nous allons créer une classe Python simple pour simuler un système de gestion de bibliothèque. Ce système permettra aux utilisateurs d'ajouter des livres, de supprimer des livres, de rechercher des livres et de lister tous les livres. Chaque livre aura deux attributs : titre et auteur.
Exemple
class Book:
def __init__(self, title, author):
self.title = title
self.author = author
def __str__(self):
return f"{self.title} par {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"Ajouté : {new_book}")
def remove_book(self, title):
for book in self.books:
if book.title == title:
self.books.remove(book)
print(f"Supprimé : {book}")
return
print(f"Livre avec le titre '{title}' introuvable.")
def find_book(self, title):
for book in self.books:
if book.title == title:
print(f"Trouvé : {book}")
return book
print(f"Livre avec le titre '{title}' introuvable.")
return None
def list_books(self):
if not self.books:
print("Aucun livre dans la bibliothèque.")
else:
print("Livres dans la bibliothèque :")
for book in self.books:
print(book)
# Exemple d'utilisation
library = Library()
library.add_book("1984", "George Orwell")
library.add_book("Ne tirez pas sur l'oiseau moqueur", "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} par {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"Ajouté : {new_book}")
def remove_book(self, title):
for book in self.books:
if book.title == title:
self.books.remove(book)
print(f"Supprimé : {book}")
return
print(f"Livre avec le titre '{title}' introuvable.")
def find_book(self, title):
for book in self.books:
if book.title == title:
print(f"Trouvé : {book}")
return book
print(f"Livre avec le titre '{title}' introuvable.")
return None
def list_books(self):
if not self.books:
print("Aucun livre dans la bibliothèque.")
else:
print("Livres dans la bibliothèque :")
for book in self.books:
print(book)
# Exemple d'utilisation
library = Library()
library.add_book("1984", "George Orwell")
library.add_book("Ne tirez pas sur l'oiseau moqueur", "Harper Lee")
library.list_books()
library.find_book("1984")
library.remove_book("1984")
library.list_books()
Explication du code :
BookClasse : Représente un livre, contenant le titre (title) et auteur (author) deux attributs.__str__Méthode pour renvoyer la représentation sous forme de chaîne d'un livre.LibraryClasse : Représente une bibliothèque, contenant une liste de livres (books). Fournit les méthodes suivantes :add_book(title, author): Ajouter un livre à la bibliothèque.remove_book(title): Supprimer un livre de la bibliothèque en fonction du titre.find_book(title): Rechercher un livre en fonction du titre.list_books(): Lister tous les livres de la bibliothèque.
- Exemple d'utilisation : Créez une
Libraryinstance, ajoutez deux livres, listez tous les livres, recherchez et supprimez un livre, puis listez à nouveau tous les livres.
Résultat de sortie :
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 LeeAutres extensions
Exemples Python3