'How to add employee in ArrayList
Please someone help me how to find the error in the code. I can't add an employee to the arraylist using the switch operator. Тhe code does not show any errors but does not work properly and the array is not filled with anything. Thanks in advance!
class Program
{
class Employee : IComparable
{
private string name;
private int level;
private DateTime hiringDate;
public Employee(string name, int level, DateTime hiringDate)
{
this.name = name;
this.level = level;
this.hiringDate = hiringDate;
}
public string Name
{
get { return name; }
set { this.name = value; }
}
public int Level
{
get { return level; }
set { this.level = value; }
}
public DateTime HiringDate
{
get { return hiringDate; }
set { this.hiringDate = value; }
}
public int CompareTo(object obj)
{
if(obj == null)
{
return 1;
}
Employee other = obj as Employee;
int value = this.level.CompareTo(other.level);
if (value == 0)
{
value = this.hiringDate.CompareTo(other.hiringDate);
}
return value;
}
public void Description()
{
Console.WriteLine("Name: {0}, Level: {1}, Hiring Date: {2}", name, level, hiringDate.ToString());
}
}
static void Main(string[] args)
{
bool exit = false;
ArrayList employees = new ArrayList();
string name;
int level, pos;
Employee mostExp;
DateTime hiringDate;
while (!exit)
{
Console.WriteLine("---------------MENU!---------------");
Console.WriteLine("1. Display employees by level.");
Console.WriteLine("2. Add new employee to list.");
Console.WriteLine("3. Remove employee from list.");
Console.WriteLine("4. Search for employee by name.");
Console.WriteLine("5. Get employee with most work expirience. ");
Console.WriteLine("6. Exit!");
int choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
employees.Sort();
Console.WriteLine("----------------------------------");
foreach (Employee employee in employees)
{
employee.Description();
}
Console.WriteLine("----------------------------------");
break;
case 2:
Console.WriteLine("Enter employee name: ");
name = Console.ReadLine();
Console.WriteLine("Enter employee level: ");
level= int.Parse(Console.ReadLine());
Console.WriteLine("Enter hiring date 'yyyy-mm-dd': ");
hiringDate = DateTime.Parse(Console.ReadLine());
break;
case 3:
Console.WriteLine("Delete employee at position: ");
pos = int.Parse(Console.ReadLine());
employees.RemoveAt(pos);
break;
case 4:
Console.WriteLine("Enter employee name: ");
name = Console.ReadLine();
Console.WriteLine("----------------------------------");
foreach (Employee employee in employees)
{
if (employee.Name == name)
{
employee.Description();
}
}
Console.WriteLine("----------------------------------");
break;
case 5:
mostExp = (Employee)employees[0];
foreach (Employee employee in employees)
{
if(mostExp.HiringDate.CompareTo(employee.HiringDate) == 1)
{
mostExp = employee;
}
}
Console.WriteLine("Employee with most work experience:");
mostExp.Description();
break;
default:
return;
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
