c#--Dictionary练习

class Employee :IComparable< Employee >
    {
        static void Main()
        {
            Dictionary<int, Employee> dictory = new Dictionary<int, Employee>();
            int i = 0;
            while (i<15)
            {
                OperationEmployee(dictory);
                i++;
            }
        }
        public Employee() { }

        public Employee(int id,string name,DateTime  start,DateTime  end,string intro)
        {
            EmployeeId = id;
            EmployeeName = name;
            InDate = start;
            OutDate = end;
            Intro = intro;
        }
        public int EmployeeId { get; set; }
        public string EmployeeName { get; set; }
        public DateTime InDate { get; set; }
        public DateTime OutDate { get; set; }
        public string Intro { get; set; }


        public int CompareTo(Employee other)
        {
            return InDate.CompareTo(other.InDate);
        }

        public static void OperationEmployee(Dictionary<int, Employee> dictory)
        {
            Console.WriteLine("1,添加员工;2,访问员工;3,删除员工;4,遍历员工;5,比较入职时间");
            int operation = Convert.ToInt32(Console.ReadLine());
            switch (operation)
            {
                case 1:
                    AddNewEmployee(dictory);
                    break;
                case 2:
                    SearchEmployee(dictory);
                    break;
                case 3:
                    DeleteEmployee(dictory);
                    break;
                case 4:
                    TraversalEmployee(dictory);
                    break;
                case 5:
                    CompareInDate(dictory);
                    break;
                default:
                    Console.WriteLine("输入不符合要求");
                    Console.ReadLine();
                    break;

            }
        }
        public static bool AddNewEmployee(Dictionary<int, Employee> dictory)
        {
            Console .Write("请输入员工ID:");
            int id = Convert.ToInt32(Console.ReadLine());
            Console.Write("请输入员工姓名:");
            string name = Console.ReadLine();
            string regex = "^[a-zA-Z]\\w{5,15}$";
            Regex reg = new Regex(regex);
            if (!reg.IsMatch(name))
            {
                Console.WriteLine("员工名称必须是字母、下划线或数字,且必须以字母开头,长度在6-16位之间");
                return false;
            }
            else
            {
                Console.WriteLine("请输入员工介绍:");
                string intro = Console.ReadLine();
                StringBuilder sb = new StringBuilder(intro);
                sb.Append("新来的员工,请多关照");
                Employee employee = new Employee(id, name, DateTime.Now, DateTime.Now.AddYears(3), sb.ToString());
                dictory.Add(employee.EmployeeId, employee);
            }

            return true;
        }

        public static void SearchEmployee(Dictionary<int, Employee> dictory)
        {
            Console.WriteLine("请输入要查看的员工ID");
            int id = Convert.ToInt32(Console.ReadLine());
            if (dictory.ContainsKey(id))
            {
                Employee employee = dictory[id];
                Console.WriteLine("员工ID\t 员工名称\t 入职时间");
                Console.WriteLine(employee.EmployeeId + "\t" + employee.EmployeeName + "\t" + employee.InDate);
            }
            else
            {
                Console.WriteLine("不存在该员工");
            }
        }

        public static void DeleteEmployee(Dictionary<int, Employee> dictory)
        {
            Console.Write("请输入要删除的员工ID:");
            int id = Convert.ToInt32(Console.ReadLine());
            if (dictory.Remove(id))
            {
                Console.WriteLine("删除成功");
            }
            else
            {
                Console.WriteLine("删除失败");
            }
        }

        public static void TraversalEmployee(Dictionary<int, Employee> dictory)
        {
            Console.WriteLine("员工ID\t 员工名称\t 入职时间\t个人介绍");
            foreach (Employee temp in dictory.Values)
            {
                Console.WriteLine(temp.EmployeeId + "\t" + temp.EmployeeName + "\t" + temp.InDate+"\t"+ temp.Intro );
            }
        }

        public static void CompareInDate(Dictionary<int, Employee> dictory)
        {
            Console.Write("要比较的员工ID(中间用','分隔开):");
            string ids = Console.ReadLine();
            string[] isdInfo = ids.Split(",");
            Employee e1 = dictory[Convert.ToInt32(isdInfo[0])];
            Employee e2 = dictory[Convert.ToInt32(isdInfo[1])];
            if(e1.CompareTo( e2)>0)
            {
                Console.WriteLine(e2.EmployeeName + "的入职时间较早");
            }
            else
            {
                Console.WriteLine(e1.EmployeeName + "的入职时间较早");
            }

        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值