一道综合练习题实践list及dictionary集合类
定义一个员工的集合,对员工集合内的元素进行查询和删除。
实现员工的签到和签退,要求如下:
//A:每天只能签到一次
//B:签退前必须已经签到
//C:显示打卡记录
代码如下:
员工信息类:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StringTest { class Employee { private int id; public int ID { get { return id; } set { id = value; } } private string name; public string Name { get { return name; } set { name = value; } } private string sex; public string Sex { get { return sex; } set { sex = value; } } private int age; public int Age { get { return age; } set { age = value; } } // A:实现新增员工(工号,年龄,姓名,性别) public Employee(int _id, int _age, string _name, string _sex) { this.Name = _name; this.Age = _age; this.Sex = _sex; this.ID = _id; } //B:展示员工信息, public void ShowMsg() { Console.WriteLine("工号:{0} 年龄:{1} 姓名:{2} 性别:{3}", this.ID, this.Age, this.Name, this.Sex); } } }
公司信息类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StringTest { class Company { public List<Employee> list = new List<Employee>(); public Dictionary<string, List<KaoqinRecord>> dicKaoQinEachDay = new Dictionary<string, List<KaoqinRecord>>(); public void AddEmp(Employee sc) { list.Add(sc); } public void DelEmp(int nID) { for (int i = 0; i < list.Count; i++) { if (list[i].ID == nID) { list.RemoveAt(i); } } } public void PrintAllEmp() { for (int i = 0; i < list.Count; i++) { list[i].ShowMsg(); } } public Employee FindEmp(int nID) { for (int i = 0; i < list.Count; i++) { if (list[i].ID == nID) { return list[i]; } } return null; } } }