Catalog + Menu C# Exercise - C# Programming Course

 Exercise

Catalog + Menu

 Objetive

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.

 Example Code

// 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.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();
    }
}

More C# Exercises of OOP More On Classes

 Array of objects: table
Create a class named "Table". It must have a constructor, indicating the width and height of the board. It will have a method "ShowData" which will wr...
 House
Create a class "House", with an attribute "area", a constructor that sets its value and a method "ShowData" to display "I am a house, my area is 200 m...
 Table + coffetable + array
Create a project named "Tables2", based on the "Tables" project. In it, create a class "CoffeeTable" that inherits from "Table". Its method "ShowDa...
 Encrypter & Decrypter
Create a class "Encrypter" to encrypt and decrypt text. It will have a "Encrypt" method, which will receive a string and return another string. It ...
 Complex numbers
A complex number has two parts: the real part and the imaginary part. In a number such as a+bi (2-3i, for example) the real part would be "a" (2) and ...
 Table + coffetable + leg
Extend the example of the tables and the coffee tables, to add a class "Leg" with a method "ShowData", which will write "I am a leg" and then it will ...
 Catalog
Create the classes diagram and then, using Visual Studio, a project and the corresponding classes for a catalog utility: It will be able to store i...
 Random number
Create a class RandomNumber, with three static methods: - GetFloat will return a number between 0 and 1 using the following algorithm: seed = (s...
 Text to HTML
Create a class "TextToHTML", which must be able to convert several texts entered by the user into a HTML sequence, like this one: Hola Soy yo Ya ...
 Class ScreenText
Create a class ScreenText, to display a certain text in specified screen coordinates. It must have a constructor which will receive X, Y and the strin...
 Enhanced ComplexNumber class
Improve the "ComplexNumber" class, so that it overloads the operators + and - to add and subtract numbers....
 3D point
Create a class "Point3D", to represent a point in 3-D space, with coordinates X, Y and Z. It must contain the following methods: MoveTo, which will...

Juan A. Ripoll - Programming Tutorials and Courses © 2025 All rights reserved.  Legal Conditions.