// Importing the System namespace to handle basic functionalities and console output
using System;
public class ScreenText
{
// Coordinates and text to be displayed
protected int x, y; // Changed from private to protected to allow access from derived classes
protected string text; // Changed from private to protected to allow access from derived classes
// Constructor that sets the X, Y coordinates and text
public ScreenText(int x, int y, string text)
{
this.x = x;
this.y = y;
this.text = text;
}
// Setter for X coordinate
public void SetX(int x)
{
this.x = x;
}
// Setter for Y coordinate
public void SetY(int y)
{
this.y = y;
}
// Setter for the text
public void SetText(string text)
{
this.text = text;
}
// Method to display the text at the X, Y coordinates
public virtual void Display()
{
Console.SetCursorPosition(x, y);
Console.WriteLine(text);
}
}
public class CenteredText : ScreenText
{
// Constructor that only needs Y and text. X will be calculated to center the text
public CenteredText(int y, string text) : base(0, y, text)
{
// Calculate the X position to center the text (in a console window with 80 columns)
int centeredX = (Console.WindowWidth - text.Length) / 2;
// Ensure that X is not less than 0 (if the text is too long for the window)
SetX(Math.Max(centeredX, 0)); // Set X to the calculated centered position, but not less than 0
}
// Override the Display method to center the text
public override void Display()
{
base.Display(); // Call the base class method to display the text
}
}
public class FramedText : CenteredText
{
// Constructor that receives the row and text and calls the base class constructor
public FramedText(int row, string text) : base(row, text) { }
// Override Display method to display the text inside a frame
public override void Display()
{
int width = text.Length + 4; // Adding space for the frame
string frame = new string('*', width); // Create a frame line
// Print top frame
Console.SetCursorPosition(0, y - 1); // Adjust Y position to make room for the frame
Console.WriteLine(frame);
// Print the framed text in the center
Console.SetCursorPosition(Math.Max(x - 1, 0), y); // Ensure the X position is not negative
Console.WriteLine("*" + text + "*");
// Print bottom frame
Console.SetCursorPosition(0, y + 1); // Adjust Y position for the bottom frame
Console.WriteLine(frame);
}
}
// Auxiliary class with the Main function to test the functionality of the classes
public class Program
{
public static void Main()
{
// Create a ScreenText object and display it at the given coordinates (X, Y)
ScreenText screenText = new ScreenText(5, 3, "This is ScreenText!");
screenText.Display();
// Create a CenteredText object and display it in the center of the screen (horizontally)
CenteredText centeredText = new CenteredText(5, "This is CenteredText!");
centeredText.Display();
// Create a FramedText object and display it inside a frame
FramedText framedText = new FramedText(7, "This is FramedText!");
framedText.Display();
}
}
Salida
This is ScreenText!
This is CenteredText!
***********************
*This is FramedText!*
***********************
Código de Ejemplo Copiado!