I Have written this program to add workers into a list and print them. I'd like a review of this. I'm especially concerned if the method Addworker
should be inside the class or in the main program.
Person
:
class Person
{
public uint NumberId;
public string Name;
public string LastName;
public uint Age; // Age Should Be Beetween 18 And 40
public uint numberId
{
set { this.NumberId = value; }
get { return this.NumberId; }
}
public string name
{
set { this.Name = value; }
get { return this.Name; }
}
public string lastName
{
set { this.LastName = value; }
get { return this.LastName; }
}
public uint age
{
set
{
if (value < 18 || value > 40)
{
throw new ArgumentOutOfRangeException();
}
this.Age = value;
}
get { return this.Age; }
}
}
Worker
:
class Worker:Person
{
private uint WageHour;
private uint Total_Hours;
public uint wageHour
{
set { this.WageHour = value; }
get { return this.WageHour; }
}
public uint total_Hours
{
set { this.Total_Hours = value; }
get { return this.Total_Hours; }
}
public double SalaryCalcul()
{
return this.wageHour * this.total_Hours;
}
public override string ToString()
{
return string.Format("Name : {0} Last Name : {1} Age : {2} WageHour : {3} Total Hours : {4} Salary : {5}",
this.name,this.lastName,this.age,this.wageHour,this.total_Hours,this.SalaryCalcul().ToString());
}
}
Main program:
class Program
{
static List<Worker> WorkersList = new List<Worker>();
static void Main(string[] args)
{
do
{
Console.Clear();
Console.WriteLine("1 - Add A Worker .");
Console.WriteLine("2 - Print List .");
int Choice = int.Parse(Console.ReadLine());
switch (Choice)
{
case 1:
{
AddWorker();
break;
}
case 2:
{
Console.Clear();
foreach (Worker W in WorkersList)
{
Console.WriteLine(W.ToString());
}
Console.ReadKey();
break;
}
}
}
while (true);
}
static void AddWorker()
{
Worker W = new Worker();
Console.WriteLine("Enter ID : ");
W.numberId = uint.Parse(Console.ReadLine());
Console.WriteLine("Enter Name : ");
W.name = Console.ReadLine();
Console.WriteLine("Enter Last Name : ");
W.lastName = Console.ReadLine();
try
{
Console.WriteLine("Enter Age : ");
W.age = uint.Parse(Console.ReadLine());
}
catch (Exception)
{
Console.WriteLine("Age Should Be Between 18 and 40 ");
}
Console.WriteLine("Enter Hour Wage : ");
W.wageHour = uint.Parse(Console.ReadLine());
Console.WriteLine("Enter Total Hours : ");
W.total_Hours = uint.Parse(Console.ReadLine());
WorkersList.Add(W);
}
}
SalaryCalcul()
hasdouble
as a return type, while its based on twouint32
s? – Caramiriel yesterday