如何在ArrayList中添加员工
How to add employee in ArrayList
请谁帮我找出代码中的错误。我无法使用 switch 运算符将员工添加到 arraylist。代码没有显示任何错误,但不能正常工作,并且数组中没有任何内容。提前致谢!
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;
}
}
}
}
在 switch case 中,你从命令行读取某些值,并将它们存储在一些局部变量(名称、级别、雇用日期)中,但你根本没有创建 Employee 对象:Employee emp = Employee(name, level, hiringdate
).然后,您必须将该员工添加到 ArrayList:employees.add(emp)
.
请谁帮我找出代码中的错误。我无法使用 switch 运算符将员工添加到 arraylist。代码没有显示任何错误,但不能正常工作,并且数组中没有任何内容。提前致谢!
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;
}
}
}
}
在 switch case 中,你从命令行读取某些值,并将它们存储在一些局部变量(名称、级别、雇用日期)中,但你根本没有创建 Employee 对象:Employee emp = Employee(name, level, hiringdate
).然后,您必须将该员工添加到 ArrayList:employees.add(emp)
.