// 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
}
}
}
Salida
Song1 - Music
Playing music by Singer1 for 200 seconds...
Retrieving information about the music by Singer1...
Song2 - Music
Playing music by Singer2 for 180 seconds...
Retrieving information about the music by Singer2...
Film1 - Film
Playing film directed by Director1 with Actor1 and Actress1...
Retrieving information about the film directed by Director1...
Film2 - Film
Playing film directed by Director2 with Actor2 and Actress2...
Retrieving information about the film directed by Director2...
Program1 - Program
Retrieving information about the computer program...
Program2 - Program
Retrieving information about the computer program...
Código de Ejemplo Copiado!