Home > Mobile >  How to add employee in ArrayList
How to add employee in ArrayList

Time:12-02

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

}

CodePudding user response:

In you switch case you are reading certain values from the command line, and you store them in some local variables (name, level, hiringdate) but you aren't making an Employee object at all: Employee emp = Employee(name, level, hiringdate). Then you have to add that employee to the ArrayList: employees.add(emp).

  • Related