gpt4 book ai didi

c# - 在 C# 中需要帮助以删除列表中的 obj

转载 作者:行者123 更新时间:2023-11-30 21:29:14 25 4
gpt4 key购买 nike

我有一个员工管理系统,我试图在 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";
}
}
}

管理Emp类

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);
}
}
}
}

最佳答案

您的问题是您的员工列表在员工类中——因此每个员工都有自己的员工列表——并且该列表仅包含该员工。

关于c# - 在 C# 中需要帮助以删除列表中的 obj,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55546345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com