Menu
Coddy logo textTech

Sistema di prestito

Fa parte della sezione Programmazione orientata agli oggetti del percorso Python di Coddy. Lezione 57 di 64.

challenge icon

Sfida

Medio

In questa sfida, aggiungerai la funzionalità di prestito al sistema di gestione della biblioteca.

Modifica library.py e implementa il metodo borrow_book, seguendo i commenti TODO.

Gli altri file sono forniti e bloccati:

  • book.py: la classe Book che hai creato nella sfida precedente
  • user.py: la classe User, che contiene user_id, name e books_borrowed
  • driver.py: esegue gli scenari di test sulla tua implementazione

borrow_book(book_isbn, user_id) deve restituire esattamente uno tra: "Book not found", "User not found", "Book is not available" o "Book borrowed successfully".

Provalo tu

from book import Book
from user import User
from library import Library

# Gestore completo dei casi di test
test_case = input()

# Test delle funzionalità di base
if test_case == "borrow_success":
    library = Library("Community Library")
    
    # Aggiungi libri e utenti
    book = Book("The Great Gatsby", "F. Scott Fitzgerald", "123456")
    library.add_book(book)
    
    user = User("U001", "Alice")
    library.add_user(user)
    
    # Prendi in prestito un libro
    result = library.borrow_book("123456", "U001")
    print(result)
    print(f"Book available: {book.available}")
    print(f"Books borrowed by user: {len(user.books_borrowed)}")

elif test_case == "book_not_found":
    library = Library("Community Library")
    user = User("U001", "Alice")
    library.add_user(user)
    
    result = library.borrow_book("nonexistent", "U001")
    print(result)

elif test_case == "user_not_found":
    library = Library("Community Library")
    book = Book("The Great Gatsby", "F. Scott Fitzgerald", "123456")
    library.add_book(book)
    
    result = library.borrow_book("123456", "nonexistent")
    print(result)
    
elif test_case == "book_not_available":
    library = Library("Community Library")
    
    # Aggiungi libri e utenti
    book = Book("The Great Gatsby", "F. Scott Fitzgerald", "123456")
    book.available = False  # Il libro è già in prestito
    library.add_book(book)
    
    user = User("U001", "Alice")
    library.add_user(user)
    
    # Prova a prendere in prestito un libro non disponibile
    result = library.borrow_book("123456", "U001")
    print(result)

# Casi limite e test aggiuntivi
elif test_case == "multiple_books":
    library = Library("Community Library")
    user = User("U001", "Alice")
    library.add_user(user)
    
    # Crea più libri
    books = [
        Book("Book 1", "Author 1", "111"),
        Book("Book 2", "Author 2", "222"),
        Book("Book 3", "Author 3", "333")
    ]
    
    for book in books:
        library.add_book(book)
    
    # Prendi in prestito più libri
    results = []
    for book in books:
        results.append(library.borrow_book(book.isbn, "U001"))
    
    for result in results:
        print(result)
    
    print(f"Total books borrowed: {len(user.books_borrowed)}")

elif test_case == "multiple_users":
    library = Library("Community Library")
    book = Book("Popular Book", "Famous Author", "999")
    library.add_book(book)
    
    # Crea più utenti
    users = [
        User("U001", "Alice"),
        User("U002", "Bob"),
        User("U003", "Charlie")
    ]
    
    for user in users:
        library.add_user(user)
    
    # Prova a prendere in prestito lo stesso libro con utenti diversi
    result1 = library.borrow_book("999", "U001")
    result2 = library.borrow_book("999", "U002")  # Dovrebbe fallire
    
    print(result1)
    print(result2)
    print(f"Book's borrower: {book.borrower.name}")

elif test_case == "return_book":
    library = Library("Community Library")
    book = Book("The Great Gatsby", "F. Scott Fitzgerald", "123456")
    library.add_book(book)
    
    user = User("U001", "Alice")
    library.add_user(user)
    
    # Prendi in prestito il libro
    borrow_result = library.borrow_book("123456", "U001")
    print(borrow_result)
    
    # Verifica lo stato del libro
    print(f"Book available: {book.available}")
    print(f"Book borrower: {book.borrower.name}")
    print(f"User's borrowed books: {len(user.books_borrowed)}")
    print(f"First borrowed book title: {user.books_borrowed[0].title}")

elif test_case == "empty_library":
    library = Library("Empty Library")
    result = library.borrow_book("any_isbn", "any_user_id")
    print(result)

elif test_case == "borrower_reference":
    library = Library("Community Library")
    book = Book("The Great Gatsby", "F. Scott Fitzgerald", "123456")
    library.add_book(book)
    
    user = User("U001", "Alice")
    library.add_user(user)
    
    # Prendi in prestito il libro
    library.borrow_book("123456", "U001")
    
    # Verifica i riferimenti
    print(f"Book's borrower name: {book.borrower.name}")
    print(f"First book in user's borrowed list: {user.books_borrowed[0].title}")

Tutte le lezioni di Programmazione orientata agli oggetti

Esercitati da solo: Compilatore Python online