Catalog C# Exercise - C# Programming Course

 Exercise

Catalog

 Objetive

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.

 Example Code

// 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
        }
    }
}

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 ...
 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...
 Catalog + Menu
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....

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