Hello Everyone.
Create a class Employee which will implement the below interface: IEmployee. The application should accept the function to invoke and its parameters from the
console in the Format:
[MethodName]:[Parameter1]:[Parameter2]
Ex: SetProperty:Age:44
The implemetation shold have the necessary checks on whether the input is in correct format by handling like FormatException.And the program should be able to continuously accept the input and print corresponding output until the input given as "Exit".
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Example
{
interface IEmployee
{
int ID { get; set; }
int Age { get; set; }
}
class Employee : IEmployee
{
public int ID
{ get; set; }
public int Age
{ get; set; }
public void Set(string propertyName, Object value)
{
switch (propertyName.ToLower())
{
case "id":
ID = Convert.ToInt32(value);
break;
case "age":
Age = Convert.ToInt32(value);
break;
default:
break;
}
Console.WriteLine("Property {0} set successfully", propertyName);
}
public void Print(string propertyName)
{
dynamic value = null;
switch (propertyName.ToLower())
{
case "id":
value = ID;
break;
case "age":
value = Age;
break;
default:
break;
}
if (value != null)
{
Console.WriteLine("{0} : {1}", propertyName, value);
}
else
{
Console.WriteLine("No Property Found");
}
}
}
static void Main(string[] args)
{
var emp = new Employee();
var str = String.Empty;
do
{
try
{
Console.WriteLine("Input : ");
str = Console.ReadLine();
if (string.IsNullOrEmpty(str))
{
throw new FormatException("No input provided");
}
var words = str.Split(':');
switch (words[0].ToLower())
{
case "set":
if (words.Count() != 3)
{
throw new FormatException("input is not in correct format");
}
emp.Set(words[1], words[2]);
break;
case "print":
if (words.Count() != 2)
{
throw new FormatException("Input is not in correct format");
}
emp.Print(words[1]);
break;
default: break;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
while (!str.ToString().Equals("Exit"));
Console.Read();
}
}
}
Issue: I am unable take the user input in the corresponding format.in the above code everytime I have to give the user input in the format "Set:Age:33" but this is not I m looking for .Actually firstly the expected input and output should be given in the format as per the screenshot I have attached.