Catalog + Menu - C# Programming Exercise

In this exercise of C#, you need to improve the catalog program so that the Main method displays a menu allowing the user to enter new data of any kind, as well as display all the stored data. The menu should offer options to add new items to the catalog, such as music files, movies, and computer programs, and to show the information stored in the system.

The program must be able to add information for each type of object (name, code, category, size, and specific attributes for music and movies) and allow the user to view all the items saved in the catalog. This exercise is a great opportunity to enhance C# skills related to user interaction, menu handling, and working with collections of objects, in addition to practicing data organization and display.

 Category

OOP More On Classes

 Exercise

Catalog + Menu

 Objective

Improve the Catalog program, so that "Main" displays a menu to allow entering new data of any kind, as well as displaying all the data stored.

 Write Your C# Exercise

// Importing the System namespace to handle basic functionalities and console output
using System;
using System.Collections.Generic;

// Defining the base class 'CatalogItem' to represent an item in the catalog
public class CatalogItem
{
    public string Name { get; set; }     // Name of the catalog item
    public string Code { get; set; }     // Code to identify the catalog item
    public string Category { get; set; } // Category of the catalog item (e.g., music, film, program)
    public double Size { get; set; }     // Size of the catalog item (e.g., size of a file)

    // Constructor to initialize the catalog item with necessary details
    public CatalogItem(string name, string code, string category, double size)
    {
        Name = name;         // Set the name of the catalog item
        Code = code;         // Set the code for the catalog item
        Category = category; // Set the category of the catalog item
        Size = size;         // Set the size of the catalog item
    }

    // Method to display information about the catalog item
    public virtual void Display()
    {
        Console.WriteLine($"Name: {Name}, Code: {Code}, Category: {Category}, Size: {Size}MB");
    }
}

// Defining the MusicFile class that extends CatalogItem, with additional properties for music
public class MusicFile : CatalogItem
{
    public string Singer { get; set; }  // Singer of the music file
    public double Length { get; set; }  // Length of the music file in seconds

    // Constructor to initialize a music file with additional properties
    public MusicFile(string name, string code, string category, double size, string singer, double length)
        : base(name, code, category, size)
    {
        Singer = singer;   // Set the singer of the music file
        Length = length;   // Set the length of the music file in seconds
    }

    // Overriding the Display method to show music-specific information
    public override void Display()
    {
        base.Display(); // Call the base class method to display common information
        Console.WriteLine($"Singer: {Singer}, Length: {Length} seconds");
    }
}

// Defining the Film class that extends CatalogItem, with additional properties for films
public class Film : CatalogItem
{
    public string Director { get; set; }   // Director of the film
    public string MainActor { get; set; }   // Main actor in the film
    public string MainActress { get; set; } // Main actress in the film

    // Constructor to initialize a film with additional properties
    public Film(string name, string code, string category, double size, string director, string mainActor, string mainActress)
        : base(name, code, category, size)
    {
        Director = director;       // Set the director of the film
        MainActor = mainActor;     // Set the main actor of the film
        MainActress = mainActress; // Set the main actress of the film
    }

    // Overriding the Display method to show film-specific information
    public override void Display()
    {
        base.Display(); // Call the base class method to display common information
        Console.WriteLine($"Director: {Director}, Main Actor: {MainActor}, Main Actress: {MainActress}");
    }
}

// Defining the ComputerProgram class that extends CatalogItem, with additional properties for computer programs
public class ComputerProgram : CatalogItem
{
    // Constructor to initialize a computer program with properties
    public ComputerProgram(string name, string code, string category, double size)
        : base(name, code, category, size)
    {
    }

    // Overriding the Display method to show program-specific information
    public override void Display()
    {
        base.Display(); // Call the base class method to display common information
    }
}

// Main program to implement the catalog system and the menu
public class Program
{
    // List to store different catalog items (music, films, programs)
    private static List catalogItems = new List();

    public static void Main()
    {
        bool keepRunning = true; // Flag to keep the program running until the user decides to exit
        while (keepRunning)
        {
            // Displaying the menu options to the user
            Console.Clear();  // Clear the console screen before showing the menu
            Console.WriteLine("Catalog Menu:");
            Console.WriteLine("1. Add a new music file");
            Console.WriteLine("2. Add a new film");
            Console.WriteLine("3. Add a new computer program");
            Console.WriteLine("4. Display all catalog items");
            Console.WriteLine("5. Exit");
            Console.Write("Select an option (1-5): ");
            
            // Reading the user's selection
            int option = Convert.ToInt32(Console.ReadLine());

            // Handling the menu options
            switch (option)
            {
                case 1:
                    AddMusicFile(); // Call the method to add a music file
                    break;
                case 2:
                    AddFilm(); // Call the method to add a film
                    break;
                case 3:
                    AddComputerProgram(); // Call the method to add a computer program
                    break;
                case 4:
                    DisplayCatalog(); // Call the method to display all catalog items
                    break;
                case 5:
                    keepRunning = false; // Set the flag to false to exit the program
                    break;
                default:
                    Console.WriteLine("Invalid option. Please try again.");
                    break;
            }
        }
    }

    // Method to add a new music file to the catalog
    private static void AddMusicFile()
    {
        // Prompting the user for music file details
        Console.Write("Enter the name of the music file: ");
        string name = Console.ReadLine();
        Console.Write("Enter the code of the music file: ");
        string code = Console.ReadLine();
        Console.Write("Enter the category of the music file: ");
        string category = Console.ReadLine();
        Console.Write("Enter the size of the music file (MB): ");
        double size = Convert.ToDouble(Console.ReadLine());
        Console.Write("Enter the singer: ");
        string singer = Console.ReadLine();
        Console.Write("Enter the length of the music file (seconds): ");
        double length = Convert.ToDouble(Console.ReadLine());

        // Creating a new MusicFile object and adding it to the catalog
        MusicFile musicFile = new MusicFile(name, code, category, size, singer, length);
        catalogItems.Add(musicFile);
    }

    // Method to add a new film to the catalog
    private static void AddFilm()
    {
        // Prompting the user for film details
        Console.Write("Enter the name of the film: ");
        string name = Console.ReadLine();
        Console.Write("Enter the code of the film: ");
        string code = Console.ReadLine();
        Console.Write("Enter the category of the film: ");
        string category = Console.ReadLine();
        Console.Write("Enter the size of the film (MB): ");
        double size = Convert.ToDouble(Console.ReadLine());
        Console.Write("Enter the director of the film: ");
        string director = Console.ReadLine();
        Console.Write("Enter the main actor of the film: ");
        string mainActor = Console.ReadLine();
        Console.Write("Enter the main actress of the film: ");
        string mainActress = Console.ReadLine();

        // Creating a new Film object and adding it to the catalog
        Film film = new Film(name, code, category, size, director, mainActor, mainActress);
        catalogItems.Add(film);
    }

    // Method to add a new computer program to the catalog
    private static void AddComputerProgram()
    {
        // Prompting the user for computer program details
        Console.Write("Enter the name of the computer program: ");
        string name = Console.ReadLine();
        Console.Write("Enter the code of the computer program: ");
        string code = Console.ReadLine();
        Console.Write("Enter the category of the program: ");
        string category = Console.ReadLine();
        Console.Write("Enter the size of the program (MB): ");
        double size = Convert.ToDouble(Console.ReadLine());

        // Creating a new ComputerProgram object and adding it to the catalog
        ComputerProgram program = new ComputerProgram(name, code, category, size);
        catalogItems.Add(program);
    }

    // Method to display all catalog items (music files, films, programs)
    private static void DisplayCatalog()
    {
        // Checking if the catalog has any items
        if (catalogItems.Count == 0)
        {
            Console.WriteLine("No items in the catalog.");
        }
        else
        {
            // Displaying each catalog item
            Console.WriteLine("\nCatalog Items:");
            foreach (var item in catalogItems)
            {
                item.Display();  // Calling the Display method of each catalog item
                Console.WriteLine(); // Adding a blank line for readability
            }
        }

        // Waiting for the user to press a key before returning to the menu
        Console.WriteLine("Press any key to return to the menu.");
        Console.ReadKey();
    }
}

 Share this C# exercise

 More C# Programming Exercises of OOP More On Classes

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. 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 C#.

  •  Array of objects: table

    In this exercise, you need to create a class named "Table". This class should have a constructor that accepts the width and height of the table. ...

  •  House

    In this exercise, you need to create a class called "House" with an attribute called "area". The class should have a constructor to set the value of this attri...

  •  Table + coffetable + array

    In this exercise, you need to create a project named "Tables2", based on the "Tables" project. In this new project, you should create a class called "CoffeeTable" tha...

  •  Encrypter & Decrypter

    In this exercise, you need to create a class called "Encrypter" to encrypt and decrypt text. The class will have an "Encrypt" method, whic...

  •  Complex numbers

    In this exercise of C#, the concept of complex numbers is introduced, which consist of two parts: the real part and the imaginary part. In an expression like a+bi (fo...

  •  Table + coffetable + leg

    In this exercise of C#, the example of tables and coffee tables is extended by adding a class called "Leg". This class should include a method named ShowData, ...