Catalog & Navigation - Python Programming Exercise

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 of various types and view all the stored information. This exercise is perfect for practicing class definition, method implementation, and user interaction in Python. By implementing this program, you will gain hands-on experience in handling class definitions, method implementation, and user interaction 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 & Navigation

 Objective

Develop a Python program to enhance the Catalog application, where the "Main" function displays a menu that allows users to input new data of various types and view all the stored information.

 Example Python Exercise

 Copy Python Code
class Item:
    def __init__(self, name, code, category, size):
        """
        Initializes a generic item with basic details.

        Parameters:
            name (str): The name of the item.
            code (str): The unique code of the item.
            category (str): The category (e.g., music, movie, program).
            size (float): The size of the item in MB.
        """
        self.name = name
        self.code = code
        self.category = category
        self.size = size

    def retrieve_information(self):
        """
        This method could connect to an internet server to fetch additional details in future versions.
        For now, it just returns the basic information of the item.
        """
        return f"Name: {self.name}, Code: {self.code}, Category: {self.category}, Size: {self.size}MB"

    def __str__(self):
        """
        Returns the string representation of the item.
        """
        return self.retrieve_information()


class Movie(Item):
    def __init__(self, name, code, category, size, director, main_actor, main_actress):
        """
        Initializes a Movie object with additional attributes for director, main actor, and actress.

        Parameters:
            director (str): The director of the movie.
            main_actor (str): The main actor of the movie.
            main_actress (str): The main actress of the movie.
        """
        super().__init__(name, code, category, size)
        self.director = director
        self.main_actor = main_actor
        self.main_actress = main_actress

    def retrieve_information(self):
        """
        Returns detailed information about the movie.
        """
        base_info = super().retrieve_information()
        return f"{base_info}, Director: {self.director}, Main Actor: {self.main_actor}, Main Actress: {self.main_actress}"

class MusicFile(Item):
    def __init__(self, name, code, category, size, singer, length):
        """
        Initializes a MusicFile object with additional attributes for singer and length.

        Parameters:
            singer (str): The artist or singer.
            length (int): The length of the music in seconds.
        """
        super().__init__(name, code, category, size)
        self.singer = singer
        self.length = length

    def retrieve_information(self):
        """
        Returns detailed information about the music file.
        """
        base_info = super().retrieve_information()
        return f"{base_info}, Singer: {self.singer}, Length: {self.length} seconds"

class ComputerProgram(Item):
    def __init__(self, name, code, category, size, version, language):
        """
        Initializes a ComputerProgram object with additional attributes for version and language.

        Parameters:
            version (str): The version of the program.
            language (str): The programming language used to develop the program.
        """
        super().__init__(name, code, category, size)
        self.version = version
        self.language = language

    def retrieve_information(self):
        """
        Returns detailed information about the computer program.
        """
        base_info = super().retrieve_information()
        return f"{base_info}, Version: {self.version}, Language: {self.language}"


def display_menu():
    """
    Displays the menu for the Catalog application.
    """
    print("\nCatalog Menu:")
    print("1. Add a Music File")
    print("2. Add a Movie")
    print("3. Add a Computer Program")
    print("4. View All Items")
    print("5. Exit")


def main():
    # List to store all the catalog items
    catalog = []

    while True:
        display_menu()
        choice = input("Enter your choice (1-5): ")

        if choice == '1':
            # Add a Music File
            name = input("Enter the name of the music file: ")
            code = input("Enter the code: ")
            category = 'Music'
            size = float(input("Enter the size (in MB): "))
            singer = input("Enter the singer: ")
            length = int(input("Enter the length (in seconds): "))
            music_file = MusicFile(name, code, category, size, singer, length)
            catalog.append(music_file)

        elif choice == '2':
            # Add a Movie
            name = input("Enter the name of the movie: ")
            code = input("Enter the code: ")
            category = 'Movie'
            size = float(input("Enter the size (in MB): "))
            director = input("Enter the director: ")
            main_actor = input("Enter the main actor: ")
            main_actress = input("Enter the main actress: ")
            movie = Movie(name, code, category, size, director, main_actor, main_actress)
            catalog.append(movie)

        elif choice == '3':
            # Add a Computer Program
            name = input("Enter the name of the program: ")
            code = input("Enter the code: ")
            category = 'Program'
            size = float(input("Enter the size (in MB): "))
            version = input("Enter the version: ")
            language = input("Enter the programming language: ")
            program = ComputerProgram(name, code, category, size, version, language)
            catalog.append(program)

        elif choice == '4':
            # View All Items
            print("\nCatalog Items:")
            for item in catalog:
                print(item)

        elif choice == '5':
            # Exit the program
            print("Exiting the Catalog application...")
            break

        else:
            print("Invalid choice. Please choose a valid option from the menu.")


if __name__ == "__main__":
    main()

 Output

Catalog Menu:
1. Add a Music File
2. Add a Movie
3. Add a Computer Program
4. View All Items
5. Exit
Enter your choice (1-5): 1
Enter the name of the music file: Song 1
Enter the code: M001
Enter the size (in MB): 3.5
Enter the singer: Artist A
Enter the length (in seconds): 180

Catalog Menu:
1. Add a Music File
2. Add a Movie
3. Add a Computer Program
4. View All Items
5. Exit
Enter your choice (1-5): 4

Catalog Items:
Name: Song 1, Code: M001, Category: Music, Size: 3.5MB, Singer: Artist A, Length: 180 seconds

 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.

  •  Object array: tables

    In this exercise, you will develop a Python class called "Table". The class should include a constructor to initialize the width and height of the table. It should al...

  •  House

    In this exercise, you will develop a Python program with the following classes: House: Create a class called "House" with an attribute for "area". Include a construc...

  •  Array of Tables and Coffee Tables

    In this exercise, you will develop a Python project called "Tables2," extending the "Tables" project. In this project, define a class called "CoffeeTable" that inheri...

  •  Encryptor & Decryptor

    In this exercise, you will develop a Python class called "Encryptor" for text encryption and decryption. This exercise is perfect for practicing class definiti...

  •  Advanced Number Systems

    In this exercise, you will develop a Python program to represent complex numbers, which consist of a real part and an imaginary part. This exercise is perfect ...

  •  Table, Coffee Table, and Legs

    In this exercise, you will develop a Python project based on the tables and coffee tables example, but now introduce a new class named "Leg". This exercise is ...