Catalog - Python Programming Exercise

In this exercise, you will develop a Python class diagram for a catalog utility that stores details about music files, movies, and computer programs. This exercise is perfect for practicing class definition, inheritance, and project organization in Python. By implementing these classes, you will gain hands-on experience in handling class definitions, inheritance, and project organization in Python. This exercise not only reinforces your understanding of object-oriented programming but also helps you develop efficient coding practices for managing user interactions.

 Category

Mastering Python Classes in OOP

 Exercise

Catalog

 Objective

Develop a Python class diagram for a catalog utility that stores details about music files, movies, and computer programs. Develop a Python project to implement the classes based on the diagram.

For each item, the class should store the following attributes: name, code, category, and size. Additionally, movies will have extra attributes: director, main actor, and main actress. Music files will include the singer and the length (in seconds).

Implement the following:

A Play method (to be further developed).
A RetrieveInformation method (which, in a later version, will connect to an internet server to fetch details about the item).
Use inheritance to avoid code duplication and streamline the design. Finally, in the Main section, create arrays to hold objects for each type (music files, movies, and computer programs), and populate them with sample data.

 Example Python Exercise

 Copy Python Code
# Base class for all catalog items
class CatalogItem:
    def __init__(self, name, code, category, size):
        """
        Constructor to initialize the name, code, category, and size of the catalog item.
        """
        self.name = name
        self.code = code
        self.category = category
        self.size = size

    def RetrieveInformation(self):
        """
        Placeholder method to retrieve information about the item from an internet server (to be developed later).
        """
        print(f"Retrieving information for {self.name} (Code: {self.code})...")
        # Later, this will be connected to an internet server to fetch real details.
        return f"Information for {self.name} retrieved."

    def Play(self):
        """
        Placeholder method for playing the item (can be further developed).
        """
        print(f"Playing {self.name}...")

    def __str__(self):
        return f"Name: {self.name}, Code: {self.code}, Category: {self.category}, Size: {self.size}MB"


# Music class inherits from CatalogItem
class Music(CatalogItem):
    def __init__(self, name, code, category, size, singer, length):
        """
        Constructor for Music, adding singer and length (in seconds) as extra attributes.
        """
        super().__init__(name, code, category, size)
        self.singer = singer
        self.length = length

    def Play(self):
        """
        Override Play method to play music.
        """
        print(f"Playing music '{self.name}' by {self.singer}, length: {self.length} seconds.")

    def __str__(self):
        return f"{super().__str__()}, Singer: {self.singer}, Length: {self.length} sec"


# Movie class inherits from CatalogItem
class Movie(CatalogItem):
    def __init__(self, name, code, category, size, director, main_actor, main_actress):
        """
        Constructor for Movie, adding director, main actor, and main actress as extra attributes.
        """
        super().__init__(name, code, category, size)
        self.director = director
        self.main_actor = main_actor
        self.main_actress = main_actress

    def Play(self):
        """
        Override Play method to play movie.
        """
        print(f"Playing movie '{self.name}' directed by {self.director}, starring {self.main_actor} and {self.main_actress}.")

    def __str__(self):
        return f"{super().__str__()}, Director: {self.director}, Main Actor: {self.main_actor}, Main Actress: {self.main_actress}"


# ComputerProgram class inherits from CatalogItem
class ComputerProgram(CatalogItem):
    def __init__(self, name, code, category, size, version, developer):
        """
        Constructor for ComputerProgram, adding version and developer as extra attributes.
        """
        super().__init__(name, code, category, size)
        self.version = version
        self.developer = developer

    def Play(self):
        """
        Override Play method to run the computer program.
        """
        print(f"Running the program '{self.name}' (Version: {self.version}) developed by {self.developer}.")

    def __str__(self):
        return f"{super().__str__()}, Version: {self.version}, Developer: {self.developer}"


# Main function to create and display catalog items
if __name__ == "__main__":
    # Create arrays to hold different types of catalog items
    music_files = [
        Music("Song1", "M001", "Music", 5, "Artist1", 210),
        Music("Song2", "M002", "Music", 4, "Artist2", 180)
    ]

    movies = [
        Movie("Movie1", "MOV001", "Movie", 700, "Director1", "Actor1", "Actress1"),
        Movie("Movie2", "MOV002", "Movie", 650, "Director2", "Actor2", "Actress2")
    ]

    computer_programs = [
        ComputerProgram("Program1", "CP001", "Software", 300, "1.0", "Dev1"),
        ComputerProgram("Program2", "CP002", "Software", 250, "2.0", "Dev2")
    ]

    # Display details of music files
    print("Music Files:")
    for music in music_files:
        print(music)
        music.Play()  # Play music
        print(music.RetrieveInformation())
        print()

    # Display details of movies
    print("Movies:")
    for movie in movies:
        print(movie)
        movie.Play()  # Play movie
        print(movie.RetrieveInformation())
        print()

    # Display details of computer programs
    print("Computer Programs:")
    for program in computer_programs:
        print(program)
        program.Play()  # Run program
        print(program.RetrieveInformation())
        print()

 Output

Music Files:
Name: Song1, Code: M001, Category: Music, Size: 5MB, Singer: Artist1, Length: 210 sec
Playing music 'Song1' by Artist1, length: 210 seconds.
Retrieving information for Song1 (Code: M001)...
Information for Song1 retrieved.

Name: Song2, Code: M002, Category: Music, Size: 4MB, Singer: Artist2, Length: 180 sec
Playing music 'Song2' by Artist2, length: 180 seconds.
Retrieving information for Song2 (Code: M002)...
Information for Song2 retrieved.

Movies:
Name: Movie1, Code: MOV001, Category: Movie, Size: 700MB, Director: Director1, Main Actor: Actor1, Main Actress: Actress1
Playing movie 'Movie1' directed by Director1, starring Actor1 and Actress1.
Retrieving information for Movie1 (Code: MOV001)...
Information for Movie1 retrieved.

Name: Movie2, Code: MOV002, Category: Movie, Size: 650MB, Director: Director2, Main Actor: Actor2, Main Actress: Actress2
Playing movie 'Movie2' directed by Director2, starring Actor2 and Actress2.
Retrieving information for Movie2 (Code: MOV002)...
Information for Movie2 retrieved.

Computer Programs:
Name: Program1, Code: CP001, Category: Software, Size: 300MB, Version: 1.0, Developer: Dev1
Running the program 'Program1' (Version: 1.0) developed by Dev1.
Retrieving information for Program1 (Code: CP001)...
Information for Program1 retrieved.

Name: Program2, Code: CP002, Category: Software, Size: 250MB, Version: 2.0, Developer: Dev2
Running the program 'Program2' (Version: 2.0) developed by Dev2.
Retrieving information for Program2 (Code: CP002)...
Information for Program2 retrieved.

 Share this Python Exercise

 More Python Programming Exercises of Mastering Python Classes in OOP

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Random Value Generation

    In this exercise, you will develop a Python project with a class RandomNumber that includes three static methods. This exercise is perfect for practicing class...

  •  Converting Text to HTML

    In this exercise, you will develop and program a Python class "TextToHTML" that can convert multiple texts entered by the user into a sequence of HTML lines. This ...

  •  Text on Screen Class

    In this exercise, you will develop a Python class called "DisplayText" that allows you to show text at specific coordinates on the screen. This exercise is per...

  •  Improved ComplexNumber Class

    In this exercise, you will develop a Python program to enhance the "ComplexNumber" class by overloading the addition (+) and subtraction (-) operators. This exerci...

  •  3D Coordinates

    In this exercise, you will develop a Python class "Point3D" that will represent a point in three-dimensional space with coordinates X, Y, and Z. This exercise ...

  •  Catalog & Navigation

    In this exercise, you will develop a Python program to enhance the Catalog application, where the "Main" function displays a menu that allows users to input new data ...