Ejercicio
Clase ScreenText
Objetivo
Cree una clase ScreenText, para mostrar un texto determinado en coordenadas de pantalla especificadas. Debe tener un constructor que recibirá X, Y y la cadena a escribir. También debe tener 3 setters y un método "Display".
Cree una clase CenteredText, basada en ScreenText, para mostrar texto centrado (horizontalmente) en una fila determinada de la pantalla. Su constructor recibirá solo Y y el texto. SetX no debe cambiar la posición horizontal.
Cree una clase FramedText, para mostrar texto centrado y dentro de un rectángulo. Recibirá la fila inicial y el texto.
Finalmente, cree un programa de prueba para todos ellos, que creará un objeto de cada tipo y los mostrará.
Código de Ejemplo
using System;
public class ScreenText
{
protected int x, y;
protected string text;
public ScreenText(int x, int y, string text)
{
this.x = x;
this.y = y;
this.text = text;
}
public void SetX(int x)
{
this.x = x;
}
public void SetY(int y)
{
this.y = y;
}
public void SetText(string text)
{
this.text = text;
}
public virtual void Display()
{
Console.SetCursorPosition(x, y);
Console.WriteLine(text);
}
}
public class CenteredText : ScreenText
{
public CenteredText(int y, string text) : base(0, y, text)
{
int centeredX = (Console.WindowWidth - text.Length) / 2;
SetX(Math.Max(centeredX, 0));
}
public override void Display()
{
base.Display();
}
}
public class FramedText : CenteredText
{
public FramedText(int row, string text) : base(row, text) { }
public override void Display()
{
int width = text.Length + 4;
string frame = new string('*', width);
Console.SetCursorPosition(0, y - 1);
Console.WriteLine(frame);
Console.SetCursorPosition(Math.Max(x - 1, 0), y);
Console.WriteLine("*" + text + "*");
Console.SetCursorPosition(0, y + 1);
Console.WriteLine(frame);
}
}
public class Program
{
public static void Main()
{
ScreenText screenText = new ScreenText(5, 3, "This is ScreenText!");
screenText.Display();
CenteredText centeredText = new CenteredText(5, "This is CenteredText!");
centeredText.Display();
FramedText framedText = new FramedText(7, "This is FramedText!");
framedText.Display();
}
}