我需要一些关于C#编程的帮助。我正在编写一个关于Employee和事件的小型C#编程。 员工参与活动。我已经编写了员工参加单一活动的代码。
我需要帮助员工参与多个活动。例如员工Parvez
参与多个活动Football
,chess
,cricket
。
我需要一些帮助才能显示参与该特定活动的员工。
以下是代码:
using System;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
List<Employee> empList = new List<Employee>();
empList.Add(new Employee() { ID = 101, Name = "Parvez Ali", EventID = 1 });
empList.Add(new Employee() { ID = 102, Name = "Ashik Ali", EventID = 1 });
empList.Add(new Employee() { ID = 103, Name = "aftab Ali", EventID = 2 });
empList.Add(new Employee() { ID = 104, Name = "Muhammad Ali", EventID = 3 });
string Choice2 = null;
do
{
Console.Clear();
MenuDisplay.DisplayEvents();
Console.WriteLine("\nPlease Enter your choice");
int Choice = int.Parse(Console.ReadLine());
Output(Choice, empList);
Console.WriteLine("\nDo you want to Continue?(y/n)");
Choice2 = Console.ReadLine();
} while (Choice2 == "y");
Console.WriteLine("GOODBYE");
Console.ReadLine();
}
public static void Output(int Choices, List<Employee> employList)
{
switch (Choices)
{
case 1:
Console.Clear();
Console.WriteLine("List of PLayers Participated in Football");
Console.WriteLine("========================================");
Employee.DataBaseSearch(employList, Choices);
break;
case 2:
Console.Clear();
Console.WriteLine("List of PLayers Participated in Cricket");
Console.WriteLine("=======================================");
Employee.DataBaseSearch(employList, Choices);
break;
case 3:
Console.Clear();
Console.WriteLine("List of PLayers Participated in Chess");
Console.WriteLine("=====================================");
Employee.DataBaseSearch(employList, Choices);
break;
case 4:
Console.Clear();
Console.WriteLine("Entire DataBase");
Console.WriteLine("===============");
Employee.DataBaseDisplay(employList);
break;
default:
Console.WriteLine("Invalid Input");
break;
}
}
}
//public enum G
//{
// Football = 1,
// Cicket = 2,
// Chess = 3
//}
class MenuDisplay
{
public static void DisplayEvents()
{
Console.WriteLine("Menu");
Console.WriteLine("==========");
Console.WriteLine("1.FootBall");
Console.WriteLine("2.Cricket");
Console.WriteLine("3.Chess");
Console.WriteLine("4.Display DataBase");
}
}
class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public int EventID { get; set; }
public static void DataBaseSearch(List<Employee> employeeList, int Choices)
{
foreach (Employee employee in employeeList)
{
if (employee.EventID == Choices)
{
Console.WriteLine(employee.ID + " - " + employee.Name);
}
}
}
public static void DataBaseDisplay(List<Employee> employeeList)
{
foreach (Employee employee in employeeList)
{
Console.WriteLine(employee.ID + " - " + employee.Name);
}
}
}
谢谢
答案 0 :(得分:0)
正如您所设计的那样,员工无法参加多个活动。这是因为您的Employee
类使用此行存储与事件的关联:
public int EventID { get; set; }
这意味着员工只有一个事件。您想要的是让员工能够与一系列事件相关联。所以你需要这样的东西:
public IEnumerable<int> EventIDs { get; set; }
您需要对DataBaseSearch
方法进行相应的更新
public static void DataBaseSearch(List<Employee> employeeList, int Choices)
{
foreach (Employee employee in employeeList)
{
if (employee.EventIDs.Contains(Choices))
{
Console.WriteLine(employee.ID + " - " + employee.Name);
}
}
}
您还需要在程序开头更新添加员工的行,例如:
empList.Add(new Employee() { ID = 101, Name = "Parvez Ali", EventID = new List<int> { 1, 2 } });