User Input
In this problem the goal is to qualify the text input. We want to validate the input values by ensuring that they are numerical values.
We are using inheritance. NumericInput will inherit TextInput so that we don't re-invent the wheel. You can use TextInput as your base class and then
NumericInput can access methods and members from that base class. You can create any other type of validation class that inherits from TextInput.
It is important to note that the function TextInput.Add(char c) returns a 'virtual void' data type. What that means is that our concrete class can override
that function with its own version of that function that includes the particular validation criteria.
using System;
public class TextInput
{
public string data;
public virtual void Add(char c)
{
data += c;
}
public string GetValue()
{
return data;
}
}
public class NumericInput : TextInput
{
public override void Add(char c)
{
if (Char.IsDigit(c))
{
data += c;
}
}
}
public class UserInput
{
public static void Main(string[] args)
{
TextInput input = new NumericInput();
input.Add('1');
input.Add('a');
input.Add('0');
Console.WriteLine(input.GetValue());
}
}