hyjggg
上官玉�
采纳率100%
2021-03-08 12:44

刚学C#,看不懂题目,不会写,有哪位大佬可以帮帮忙?(定义员工类及公司类并实现其功能)

20
已采纳

某公司员工拥有若干个职员,职员信息有(工号、姓名、性别、出生年月、职称、籍贯、爱好)

1.请设计如下的类

(1)社员(Staff):需要包含上述属性信息;本身支持按照员工工号排序;属性【爱好】要求是不重复的集合结构。

(2)公司(Company):需要包含所有的社员信息;需要支持根据工号和名字查找员工信息的方法。

2.请实现以下功能

(1)创建公司实例,并且添加10个以上的员工信息。

(2)请分别按照工号、姓名、性别、出生年月来排序员工信息,并输出。

(3)输入一个爱好,并判断本公司内是否有这个爱好的员工;若有则输出有这个爱好的员工信息。

(4)先获取公司人员的所有籍贯信息,然后按ji'gua归类

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • showbo GoCityPass新加坡曼谷通票 1月前

    希望你能看得懂。。。。控制台应用程序

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Company
    {
        class Program
        {
            static void Main(string[] args)
            {
                var Name1 = new[] { "张", "李","王" };
                var Name2 = new[] { "三", "四","六" };
                var sex = new[] { "男","女"};
                var Hobbies = new[] { "篮球", "足球", "看电影", "唱歌" };
                var BirthPlace = new[] { "桂林","南宁","柳州","贺州"};
                var rnd = new Random();
                int count = rnd.Next(10, 20);
    
                var cmp = new Company() { staffs=new List<Staff>()};
          
                for (var i = 0; i < count; i++)
                {
                    cmp.staffs.Add(new Staff
                    {
                        No = (i + 1).ToString(),
                        Name = Name1[rnd.Next(0, Name1.Length)] + Name2[rnd.Next(0, Name2.Length)] + i,
                        Sex = sex[rnd.Next(0, sex.Length)],
                        Birthdate = DateTime.Now.AddMonths(-rnd.Next(100, 200)),
                        JobTitle = "员工",
                        BirthPlace = BirthPlace[rnd.Next(0, BirthPlace.Length)],
                        Hobbies = Hobbies.Skip(rnd.Next(0, Hobbies.Length + 1)).ToList()
                    });
                }
                var searchFDs = new[] { "工号", "姓名", "爱好" };
                var sortFDs = new[] { "工号", "姓名", "性别", "出生年月" };
                bool isSearch;
                while (true)
                {
                    Console.WriteLine("\n\n1.按照籍贯归类并输出人员\n2.查找员工信息\n3.员工排序输出\n0.退出系统\n\n");
                    Console.Write("请输入指令:");
                    var k = Console.ReadLine();
                    switch (k)
                    {
                        case "0": return;
                        case "1":cmp.showStaffOrderByBirthPlace(); break;
                        case "2":
                        case "3":
                            isSearch = k == "2";
                            while (true)
                            {
                                Console.Write("输入要查询的字段名称,如"+(isSearch? "工号/姓名/爱好": "工号/姓名/性别/出生年月") + ":");
                                k = Console.ReadLine();
                                if ((isSearch && searchFDs.Contains(k)) || (!isSearch && sortFDs.Contains(k)))
                                {
                                    if (isSearch)
                                    {
                                        Console.Write("请输入字段值:");
                                        var value = Console.ReadLine();
                                        cmp.FindStaffByField(k, value);
                                    }
                                    else
                                    {
                                        cmp.ShowStaffOrderBy(k);
                                    }
                                    break;
                                }
                                else Console.WriteLine("字段名称输入错误");
                            }
                            break;
                        default: Console.WriteLine("指令错误"); break;
                    }
                }
            }
        }
    
        public class Staff
        {
            public string No { get; set; }
            public string Name { get; set; }
            public string Sex { get; set; }
            public DateTime Birthdate { get; set; }
            public string JobTitle { get; set; }
            public string BirthPlace { get; set; }
            public List<string> Hobbies { get; set; }
            public override string ToString()
            {
                return string.Format("{0,-5}{1,-10}{2,-5}{3,-15}{4,-5}{5,-10}\t{6}", No, Name, Sex, Birthdate.ToString("yyyy-MM-dd"), JobTitle, BirthPlace, String.Join(",", Hobbies));
            }
        }
        public class Company
        {
            public List<Staff> staffs { get; set; }
    
            public void FindStaffByField(string fd,string value)
            {
                var rs = staffs.Where(i => fd == "姓名" ? i.Name.Contains(value) : fd == "工号" ? i.No == value : i.Hobbies.Contains(value));
                if (rs.Count() == 0) Console.WriteLine("找不到" + fd + "为" + value + "的员工记录!");
                else foreach (var r in rs) Console.WriteLine(r);
            }
            public void ShowStaffOrderBy(string fd )
            {
                IEnumerable<Staff> rs;
                switch (fd)
                {
                    case "姓名": rs = staffs.OrderBy(i => i.Name); break;
                    case "性别": rs = staffs.OrderBy(i => i.Sex); break;
                    case "出生年月": rs = staffs.OrderBy(i => i.Birthdate); break;
                    default: rs = staffs.OrderBy(i => i.No); break;
                }
                Console.WriteLine("按照“"+ fd + "”排序");
                foreach (var r in rs) Console.WriteLine(r);
            }
    
            public void showStaffOrderByBirthPlace()
            {
                var bpg = staffs.GroupBy(i => i.BirthPlace);
                foreach (var b in bpg) {
                    Console.WriteLine(b.Key);
                    foreach (var s in b) Console.WriteLine(s);
                }
            }
        }
    }
    
    点赞 1 评论 复制链接分享