Hone logo
Hone
Problems

Building a Simple Library System with Inheritance

This challenge will test your understanding of inheritance in Python. You'll create a basic library system where different types of library items (books and DVDs) inherit common properties from a base LibraryItem class, and then add their specific attributes and behaviors. This is a fundamental concept for organizing and reusing code.

Problem Description

Your task is to design and implement a class hierarchy in Python to represent items in a library. You will create a base class called LibraryItem and then two derived classes: Book and DVD.

Requirements:

  1. LibraryItem Base Class:

    • Should have an __init__ method that accepts title (string) and item_id (string).
    • Should have an __str__ method that returns a formatted string representation of the item, including its title and ID.
    • Should have a display_details method that prints the output of the __str__ method.
  2. Book Derived Class:

    • Should inherit from LibraryItem.
    • Its __init__ method should accept title, item_id, author (string), and num_pages (integer).
    • It must call the parent class's __init__ method to initialize title and item_id.
    • It should have its own __str__ method that extends the parent's string representation to include the author and number of pages.
    • It should override the display_details method to print its specific details.
  3. DVD Derived Class:

    • Should inherit from LibraryItem.
    • Its __init__ method should accept title, item_id, director (string), and runtime_minutes (integer).
    • It must call the parent class's __init__ method to initialize title and item_id.
    • It should have its own __str__ method that extends the parent's string representation to include the director and runtime in minutes.
    • It should override the display_details method to print its specific details.

Expected Behavior:

  • When a Book or DVD object is created, its details should be correctly initialized.
  • When display_details() is called on either a Book or DVD object, it should print a comprehensive string representation of that item.

Edge Cases to Consider:

  • What happens if an empty string is provided for title, item_id, author, or director? (Your implementation should handle these gracefully, though specific error handling beyond basic string assignment isn't strictly required for this challenge.)
  • What if num_pages or runtime_minutes are zero or negative? (For this challenge, assume valid positive integers for simplicity, but be aware of potential real-world scenarios.)

Examples

Example 1:

# Creating a Book
book1 = Book("The Hitchhiker's Guide to the Galaxy", "BK101", "Douglas Adams", 224)
book1.display_details()

# Creating a DVD
dvd1 = DVD("Inception", "DV802", "Christopher Nolan", 148)
dvd1.display_details()

Output:

Item ID: BK101, Title: The Hitchhiker's Guide to the Galaxy, Author: Douglas Adams, Pages: 224
Item ID: DV802, Title: Inception, Director: Christopher Nolan, Runtime: 148 minutes

Explanation:

The Book object correctly initializes with its title, ID, author, and page count. display_details() prints a formatted string that includes all these attributes. The DVD object is similarly initialized and its display_details() method prints its specific attributes.

Example 2:

# Creating another Book with different details
book2 = Book("Pride and Prejudice", "BK102", "Jane Austen", 432)
print(book2) # Using the __str__ method directly

# Creating another DVD with different details
dvd2 = DVD("The Matrix", "DV803", "Lana Wachowski, Lilly Wachowski", 136)
print(dvd2) # Using the __str__ method directly

Output:

Item ID: BK102, Title: Pride and Prejudice, Author: Jane Austen, Pages: 432
Item ID: DV803, Title: The Matrix, Director: Lana Wachowski, Lilly Wachowski, Runtime: 136 minutes

Explanation:

This example demonstrates that the __str__ method itself returns the formatted string, which can then be printed. The output confirms correct initialization and formatting for both objects.

Constraints

  • All title, item_id, author, and director attributes must be strings.
  • num_pages must be an integer.
  • runtime_minutes must be an integer.
  • The solution must use inheritance as described.

Notes

  • Remember to use super() to call the parent class's __init__ method. This is crucial for ensuring that the base class attributes are properly initialized.
  • Consider how you can leverage the parent class's __str__ method within the derived class __str__ methods.
  • Think about the purpose of display_details() and why it might be useful to override it in the derived classes.
Loading editor...
python