Catalog - C# Programming Exercise

In this exercise of C#, you are required to create a class diagram and then, using Visual Studio, develop a project with the corresponding classes for a catalog utility. This catalog should be able to store information about music files, films, and computer programs.

For each item in the catalog, it must store the following information: name, code, category, and size. For films, it should also store the director, the main actor, and the main actress. For music files, it should include the singer and the length (in seconds).

Both music and movies should have two methods: Play (which will not be implemented yet) and RetrieveInformation, which in a future version will connect to an internet server to fetch additional information about the item.

It is recommended to use inheritance to organize and structure the classes efficiently, so that common attributes between music, movies, and computer programs can be shared. In the Main method, arrays of each type of object should be created. This exercise allows you to practice class creation, the use of inheritance, data management in a catalog, and efficient code organization in C#.

 Category

OOP More On Classes

 Exercise

Catalog

 Objective

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 information about music files, films and computer programs.
For each item, it must store: name, code, category and size. For films it must also hold the director, the main actor and the main actress. For music files, the singer and the length (in seconds).
For music and movies it must have a method "Play" (not implemented yet) and also a method "RetrieveInformation", which will (in a later version) connect to an internet server to get information about it.
Use inheritance if needed. In "Main", create arrays of each kind of object.

 Write Your C# Exercise

// Importing the System namespace for input/output functionalities
using System;

// Base class for catalog items
public class CatalogItem
{
    // Private fields for the name, code, category, and size
    private string name;
    private string code;
    private string category;
    private double size;

    // Constructor to initialize the catalog item with necessary information
    public CatalogItem(string name, string code, string category, double size)
    {
        this.name = name; // Setting the name of the item
        this.code = code; // Setting the code of the item
        this.category = category; // Setting the category (e.g., Music, Film, Program)
        this.size = size; // Setting the size of the item
    }

    // Method to simulate playing the item (not yet implemented)
    public virtual void Play()
    {
        Console.WriteLine("Playing item...");
    }

    // Method to simulate retrieving information about the item from an internet server (not yet implemented)
    public virtual void RetrieveInformation()
    {
        Console.WriteLine("Retrieving information...");
    }

    // Getter for the name
    public string GetName() => name;

    // Getter for the code
    public string GetCode() => code;

    // Getter for the category
    public string GetCategory() => category;

    // Getter for the size
    public double GetSize() => size;
}

// Derived class for music files
public class Music : CatalogItem
{
    // Additional fields specific to music
    private string singer;
    private double length; // Length in seconds

    // Constructor to initialize a music item
    public Music(string name, string code, string category, double size, string singer, double length)
        : base(name, code, category, size) // Calling the base class constructor
    {
        this.singer = singer; // Setting the singer's name
        this.length = length; // Setting the length of the music file in seconds
    }

    // Overriding the Play method for music
    public override void Play()
    {
        Console.WriteLine($"Playing music by {singer} for {length} seconds...");
    }

    // Overriding the RetrieveInformation method for music
    public override void RetrieveInformation()
    {
        Console.WriteLine($"Retrieving information about the music by {singer}...");
    }

    // Getter for the singer's name
    public string GetSinger() => singer;

    // Getter for the length of the music file
    public double GetLength() => length;
}

// Derived class for films
public class Film : CatalogItem
{
    // Additional fields specific to films
    private string director;
    private string mainActor;
    private string mainActress;

    // Constructor to initialize a film item
    public Film(string name, string code, string category, double size, string director, string mainActor, string mainActress)
        : base(name, code, category, size) // Calling the base class constructor
    {
        this.director = director; // Setting the director's name
        this.mainActor = mainActor; // Setting the main actor's name
        this.mainActress = mainActress; // Setting the main actress's name
    }

    // Overriding the Play method for films
    public override void Play()
    {
        Console.WriteLine($"Playing film directed by {director} with {mainActor} and {mainActress}...");
    }

    // Overriding the RetrieveInformation method for films
    public override void RetrieveInformation()
    {
        Console.WriteLine($"Retrieving information about the film directed by {director}...");
    }

    // Getter for the director's name
    public string GetDirector() => director;

    // Getter for the main actor's name
    public string GetMainActor() => mainActor;

    // Getter for the main actress's name
    public string GetMainActress() => mainActress;
}

// Derived class for computer programs
public class ComputerProgram : CatalogItem
{
    // Constructor to initialize a computer program item
    public ComputerProgram(string name, string code, string category, double size)
        : base(name, code, category, size) // Calling the base class constructor
    {
    }

    // Overriding the RetrieveInformation method for computer programs
    public override void RetrieveInformation()
    {
        Console.WriteLine($"Retrieving information about the computer program...");
    }
}

// Main class to test the catalog system
public class Program
{
    public static void Main()
    {
        // Creating an array of music items
        Music[] musicItems = new Music[2]
        {
            new Music("Song1", "M001", "Music", 5.2, "Singer1", 200),
            new Music("Song2", "M002", "Music", 4.8, "Singer2", 180)
        };

        // Creating an array of film items
        Film[] filmItems = new Film[2]
        {
            new Film("Film1", "F001", "Film", 700, "Director1", "Actor1", "Actress1"),
            new Film("Film2", "F002", "Film", 800, "Director2", "Actor2", "Actress2")
        };

        // Creating an array of computer programs
        ComputerProgram[] programItems = new ComputerProgram[2]
        {
            new ComputerProgram("Program1", "P001", "Program", 150),
            new ComputerProgram("Program2", "P002", "Program", 200)
        };

        // Displaying the information of each item in the arrays
        foreach (var music in musicItems)
        {
            Console.WriteLine($"{music.GetName()} - {music.GetCategory()}");
            music.Play(); // Playing the music
            music.RetrieveInformation(); // Retrieving information about the music
        }

        foreach (var film in filmItems)
        {
            Console.WriteLine($"{film.GetName()} - {film.GetCategory()}");
            film.Play(); // Playing the film
            film.RetrieveInformation(); // Retrieving information about the film
        }

        foreach (var program in programItems)
        {
            Console.WriteLine($"{program.GetName()} - {program.GetCategory()}");
            program.RetrieveInformation(); // Retrieving information about the program
        }
    }
}

 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#.

  •  Random number

    In this exercise of C#, you need to create a class called RandomNumber with three static methods. The first method, GetFloat, should return a number bet...

  •  Text to HTML

    In this exercise of C#, you need to create a class called TextToHTML, which should be able to convert several texts entered by the user into an HTML sequence. ...

  •  Class ScreenText

    In this exercise of C#, you need to create a class called ScreenText, which will display a certain text at specified screen coordinates. The class must have a ...

  •  Enhanced ComplexNumber class

    In this exercise of C#, you need to improve the ComplexNumber class by overloading the + and - operators to allow the addition and subtraction of...

  •  3D point

    In this exercise of C#, you need to create a class called Point3D to represent a point in 3-D space, with coordinates X, Y, and Z. The class must include the f...

  •  Catalog + Menu

    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 wel...