【问题标题】:How to add employee in ArrayList如何在 ArrayList 中添加员工
【发布时间】:2022-01-07 05:35:54
【问题描述】:

请有人帮助我如何找到代码中的错误。我无法使用 switch 运算符将员工添加到数组列表中。Т他的代码没有显示任何错误,但无法正常工作,并且数组没有填充任何东西提前谢谢!!

类程序 { 员工类:IComparable { 私有字符串名称; 私有 int 级别; 私人日期时间招聘日期;

        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 expirience:");
                    mostExp.Description();
                    break;
                default:
                    return;
            }
        }
        
    }
}

}

【问题讨论】:

    标签: class arraylist switch-statement


    【解决方案1】:

    在切换情况下,您正在从命令行读取某些值,并将它们存储在一些局部变量(名称、级别、招聘日期)中,但您根本没有创建 Employee 对象:Employee emp = Employee(name, level, hiringdate)。然后你必须将该员工添加到 ArrayList:employees.add(emp)

    【讨论】:

    • 非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多