我有一个员工管理系统,该系统试图在c#控制台应用程序中构建,但无法添加新员工。

但我不确定如何从列表中删除员工。

我必须将两种方法放在一起,然后它才能工作。

似乎我无法从我的removeEmployee方法调用obj(emp)

主班

using System;

namespace HRM
{
    class Program
    {
        static void Main(string[] args)
        {

             manageEmp emp = new manageEmp();

             emp.addEmployee();
             emp.removeEmployee();
        }
    }
}


员工阶层

using System;
using System.Collections.Generic;

namespace HRM
{
    public class Employee
    {
        private String empID;
        private String empFirstName;
        private String empLastName;
        private String empDep;
        private String empDOB;
        private String empAddress;
        private int PostalCode;
        private double empSal;


        public Employee()
        {

        }

        public Employee(String aempID, string aempFirstName, string aempLasttName, string aempDep, String aEmpDOB, string aempAddress, int aPostalCode, double aempSal)
        {
            this.EmpID = aempID;
            this.EmpFirstName = aempFirstName;
            this.EmpLastName = aempLasttName;
            this.EmpDep = aempDep;
            this.EmpDOB = aEmpDOB;
            this.EmpAddress = aempAddress;
            this.PostalCode1 = aPostalCode;
            this.EmpSal = aempSal;
        }

        public string EmpID { get => empID; set => empID = value; }
        public string EmpFirstName { get => empFirstName; set => empFirstName = value; }
        public string EmpLastName { get => empLastName; set => empLastName = value; }
        public string EmpDep { get => empDep; set => empDep = value; }
        public string EmpDOB { get => empDOB; set => empDOB = value; }
        public string EmpAddress { get => empAddress; set => empAddress = value; }
        public int PostalCode1 { get => PostalCode; set => PostalCode = value; }
        public double EmpSal { get => empSal; set => empSal = value; }
        public List<Employee> El { get => el; set => el = value; }

        public override string ToString()
        {
            return "Employment ID : " + empID + "\n"
                  + "FirstName : " + EmpFirstName + "\n"
                  + "LastName : " + EmpLastName + "\n"
                  + "Department : " + EmpDep + "\n"
                  + "Date of Birth: " + EmpDOB + "\n"
                  + "Address : " + EmpAddress + "\n"
                  + "PostalCode : " + PostalCode1 + "\n"
                  + "empSal : " + EmpSal + "\n";
        }
    }
}


manageEmp类

using System;
using System.Collections.Generic;

namespace HRM
{
    public class manageEmp
    {
        private List<Employee> el = new List<Employee>();
        public List<Employee> El { get => el; set => el = value; }

        public void addEmployee()
        {

            Console.WriteLine("===================================" + "\n");
            Console.WriteLine("Add an Employee");
            Console.WriteLine("===================================" + "\n");
            Console.WriteLine("");

            Console.WriteLine("Please enter your Employment ID");
            String eID = Console.ReadLine();

            Console.WriteLine("Please enter your First Name");
            String eFirstName = Console.ReadLine();

            Console.WriteLine("Please enter your Last Name");
            String eLasttName = Console.ReadLine();

            Console.WriteLine("Please entter your department");
            String eDep = Console.ReadLine();

            Console.WriteLine("Please enter your Date of Birth");
            String eDOB = Console.ReadLine();

            Console.WriteLine("Please entter your Address");
            String eAddress = Console.ReadLine();

            Console.WriteLine("Please enter your Postal Code");
            int ePostalCode = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Please enter your Salary");
            double eSal = Convert.ToDouble(Console.ReadLine());

            Employee emp = new Employee(eID, eFirstName, eLasttName, eDep, eDOB, eAddress, ePostalCode, eSal);

            emp.El.Add(emp);

        }

        public void viewEmployee()
        {
            Employee nemp = new Employee();

            nemp.El.ForEach(Console.WriteLine);


        }

        public void removeEmployee()
        {
            Console.WriteLine("Please enter a employee Id to be deleted");


            String delemp = Console.ReadLine();
            for (int i = 0; i < El.Count; i++)
            {
                emp = El[i];
                if (delemp.Equals(eID))
                {
                    el.Remove(emp);

                }
                Console.WriteLine(delemp + " Has been deleted sucessfully");
                el.ForEach(Console.WriteLine);
            }
        }
    }
}

最佳答案

在功能RemoveEmployee中,您正在创建一个新的manageEmp对象。就像“新”一词所暗示的,这是一个不同的,新创建的manageEmp,带有自己的,新创建的List<Employee>,其中不包含任何项目。

此外,您已将该函数声明为public void removeEmployee(string eID),因此不能在emp.removeEmployee()行中调用它。

07-26 02:00