C#员工活动计划打印活动

时间:2014-04-12 17:09:55

标签: c# class oop

我需要一些关于C#编程的帮助。我正在编写一个关于Employee和事件的小型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);
        }
    }
}

谢谢

1 个答案:

答案 0 :(得分:0)

它不显示,因为在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"} }; 
}

请注意我使用静态公共字段的私人课程只是为了与您已有的一致,这远远不是一个好的编程习惯。

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

    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循环来实现。我们只是迭代每个eventID来获取并打印先前创建的字典中的值(字符串名称)。然后我们可以创建一个换行符来分隔数据。那就是它!