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:
-
LibraryItemBase Class:- Should have an
__init__method that acceptstitle(string) anditem_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_detailsmethod that prints the output of the__str__method.
- Should have an
-
BookDerived Class:- Should inherit from
LibraryItem. - Its
__init__method should accepttitle,item_id,author(string), andnum_pages(integer). - It must call the parent class's
__init__method to initializetitleanditem_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_detailsmethod to print its specific details.
- Should inherit from
-
DVDDerived Class:- Should inherit from
LibraryItem. - Its
__init__method should accepttitle,item_id,director(string), andruntime_minutes(integer). - It must call the parent class's
__init__method to initializetitleanditem_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_detailsmethod to print its specific details.
- Should inherit from
Expected Behavior:
- When a
BookorDVDobject is created, its details should be correctly initialized. - When
display_details()is called on either aBookorDVDobject, 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, ordirector? (Your implementation should handle these gracefully, though specific error handling beyond basic string assignment isn't strictly required for this challenge.) - What if
num_pagesorruntime_minutesare 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, anddirectorattributes must be strings. num_pagesmust be an integer.runtime_minutesmust 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.