C#员工事件程序获取事件打印

本文关键字:事件 获取 打印 程序 | 更新日期: 2023-09-27 17:58:13

我需要一些关于C#编程的帮助。我正在写一个关于Employee和events的小C#编程。参与活动的员工。问题是,当我选择DataBase display时,数据库会显示,但问题是员工参与的事件没有显示。

当我选择以下格式的DataBase display时,我需要一些逻辑来显示事件。


需求输出:

Entire DataBase
===============
101 - Parvez Ali  -FootBall Cricket Chess
102 - Ashik Ali  - FootBall Cricket 
103 - aftab Ali  - Chricket
104 - Muhammad Ali - Chess

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        List<Employee> empList = new List<Employee>();
        empList.Add(new Employee() { ID = 101, Name = "Parvez Ali", EventIDs = new List<int> { 1, 2, 3 } });
        empList.Add(new Employee() { ID = 102, Name = "Ashik Ali", EventIDs = new List<int> { 1, 2 } });
        empList.Add(new Employee() { ID = 103, Name = "Aftab Ali", EventIDs = new List<int> { 2 } });
        empList.Add(new Employee() { ID = 104, Name = "Muhammad Ali", EventIDs = new List<int> { 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;
        }
    }
}
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 IEnumerable<int> EventIDs { get; set; }
    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);
            }
        }
    }
    public static void DataBaseDisplay(List<Employee> employeeList)
    {
        foreach (Employee employee in employeeList)
        {
            Console.WriteLine(employee.ID + " - " + employee.Name);
        }
    }
}

谢谢

C#员工事件程序获取事件打印

它不会显示,因为在DatabaseDisplay中,您完全没有做任何事情来显示事件。。。在编写名称之后,您需要编写事件的逻辑。

首先,您需要一种从ID中获取事件名称的方法。一种快速的方法是使用dictionary。由于名称是string,ID是int,因此Dictionary<int, string>将完成以下任务:

class Events
{
    public static Dictionary<int, string> EventNames = new Dictionary<int, string>{{1, "FootBall"}, {2, "Cricket"}, {3, "Chess"} }; 
}

请注意,我使用带有静态公共字段的私有类只是为了与您现有的保持一致,这远不是一个好的编程实践。

之后,您所需要做的就是迭代Employee的事件并显示数据:

    public static void DataBaseDisplay(List<Employee> employeeList)
    {
        foreach (Employee employee in employeeList)
        {
            Console.Write(employee.ID + " - " + employee.Name + " - ");
            foreach (var eventId in employee.EventIDs)
            {
                Console.Write(" " + Events.EventNames[eventId]);
            }
            Console.WriteLine();
        }
    }

正如您所看到的,在写入名称(使用Write而不是WriteLine,因为我们希望保持在同一行)之后,我们可以通过简单的foreach循环来实现这一点。我们只是对每个事件ID进行迭代,以获取并打印之前创建的字典中的值(字符串名称)。然后我们可以创建一个换行符来分隔数据。就这样!