定义一个员工的集合,对员工集合内的元素进行查询和删除。
实现员工的签到和签退,要求如下:
//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 = ; i < list.Count; i++)
{
if (list[i].ID == nID)
{
list.RemoveAt(i);
}
}
} public void PrintAllEmp()
{
for (int i = ; i < list.Count; i++)
{
list[i].ShowMsg();
} } public Employee FindEmp(int nID)
{
for (int i = ; i < list.Count; i++)
{
if (list[i].ID == nID)
{
return list[i];
}
}
return null;
}
}
}

考勤记录类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace StringTest
{
class KaoqinRecord
{ public int nID;
public DateTime dtStart;
public DateTime dtEnd;
}
}

主程序类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace StringTest
{
class Program
{
static Company c = new Company();
static void Main(string[] args)
{
//// 1、使用你所学的C#的容器类实现员工考勤信息管理, 
//实现以下功能:要求:使用泛型集合list的添加,查询和删除操作)
// A:实现新增员工(工号,年龄,姓名,性别)
List<Employee> list = new List<Employee>(); c.AddEmp(new Employee(, , "zhang3", "男"));
c.AddEmp(new Employee(, , "li4", "女"));
c.AddEmp(new Employee(, , "wang5", "男"));
c.AddEmp(new Employee(, , "zhao6", "女"));
c.AddEmp(new Employee(, , "qian7", "男"));
c.AddEmp(new Employee(, , "sun8", "女")); c.PrintAllEmp(); //C:根据工号删除员工信息 Console.WriteLine("请输入删除员工信息!工号:");
int numDel = Convert.ToInt32(Console.ReadLine()); c.DelEmp(numDel); c.PrintAllEmp(); //D根据员工工号查询员工信息
Console.WriteLine("请输入查询员工信息!工号:");
int numShu = Convert.ToInt32(Console.ReadLine()); Employee sc = c.FindEmp(numShu);
sc.ShowMsg(); while(true)
{
Console.WriteLine("员工考勤系统");
Console.WriteLine("0--------------exit");
Console.WriteLine("1--------------qiandao");
Console.WriteLine("2--------------qiantui");
Console.WriteLine("3--------------ShowKaoqin"); int n = Convert.ToInt32(Console.ReadLine());
if (n == )
break;
switch(n)
{
case :
{
Console.WriteLine("请输入要签到的员工号码");
int nEmpNo = Convert.ToInt32(Console.ReadLine());
qiandao(nEmpNo);
break;
}
case :
{
Console.WriteLine("请输入要签退的员工号码");
int nEmpNo = Convert.ToInt32(Console.ReadLine());
qiantui(nEmpNo);
break;
}
case :
{
Console.WriteLine("请输入考勤日期:");
ShowKaoqins();
break;
}
default:
break; }
} } public static void ShowKaoqins()
{
string strDate = System.DateTime.Now.Date.ToShortDateString();
List<KaoqinRecord> lst = c.dicKaoQinEachDay[strDate];
Console.WriteLine("今天考勤的人数有:");
foreach(KaoqinRecord kr in lst)
{
int nId = kr.nID;
Employee sc = c.FindEmp(nId);
Console.WriteLine("{0} 今天签到时间为:{1} 签退时间为 {2}", sc.Name, kr.dtStart, kr.dtEnd);
}
} public static void qiantui(int nID)
{
Employee sc = c.FindEmp(nID);
string strDate = System.DateTime.Now.Date.ToShortDateString();
if (!c.dicKaoQinEachDay.Keys.Contains(strDate))//第一个签到的员工
{
Console.WriteLine("{0}今天没有签到", sc.Name);
return;
}
else
{
List<KaoqinRecord> lst = c.dicKaoQinEachDay[strDate];
int nFindIndex = -;
int i = ;
foreach (KaoqinRecord kr in lst)
{
if (kr.nID == nID)
{
nFindIndex = i;
break;
}
i++;
} if(nFindIndex >= )
{
KaoqinRecord kr = lst[nFindIndex];
kr.dtEnd = System.DateTime.Now;
}
else
{
Console.WriteLine("{0}今天没有签到", sc.Name);
}
}
}
public static void qiandao(int nID)
{
Employee sc = c.FindEmp(nID);
string strDate = System.DateTime.Now.Date.ToShortDateString();
if(!c.dicKaoQinEachDay.Keys.Contains(strDate))//第一个签到的员工
{
KaoqinRecord kr = new KaoqinRecord();
kr.nID = sc.ID;
kr.dtStart = System.DateTime.Now;
List<KaoqinRecord> lst = new List<KaoqinRecord>();
lst.Add(kr);
c.dicKaoQinEachDay.Add(strDate, lst);
}
else
{
List<KaoqinRecord> lst = c.dicKaoQinEachDay[strDate]; foreach(KaoqinRecord kr in lst)
{
if(kr.nID == nID)
{
Console.WriteLine("{0}今天已经签到了",sc.Name);
return;
}
} KaoqinRecord krNew = new KaoqinRecord();
krNew.nID = sc.ID;
krNew.dtStart = System.DateTime.Now;
lst.Add(krNew); } Console.WriteLine("{0}签到成功",sc.Name); }
}
}

java版

公司信息类

package com.company;

import java.text.SimpleDateFormat;
import java.util.*; /**
* Created by ttc on 2017/6/30.
*/
public class Company {
private List<String> lstEmps = new ArrayList<String>();
//key是签到日期
private Map<String,List<KaoqinRecord>> map = new HashMap<>(); public void qiandao()
{
Scanner sc = new Scanner(System.in);
System.out.println("请输入要签到的员工姓名");
String strName = sc.nextLine();
if(!lstEmps.contains(strName))
{
System.out.println("不存在该员工");
return;
} Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat sdfTime = new SimpleDateFormat("hh:mm:ss");
String strDate = sdf.format(date);
String strTime = sdfTime.format(date);
if(!map.containsKey(strDate))//第一个签到的员工
{
List<KaoqinRecord> lst = new ArrayList<>(); KaoqinRecord kq = new KaoqinRecord();
kq.setEmpName(strName);
kq.setTimeStart(strTime);
kq.setTimeEnd("");
lst.add(kq); map.put(strDate,lst);
System.out.println(strName+"签到成功");
}
else//说明之前,今天已经有人签过到了
{
List<KaoqinRecord> lst = map.get(strDate); //判断是否已经签到过
boolean bIsFind = false;//假设没找到,没签到过
for(KaoqinRecord qr : lst)
{
if(qr.getEmpName().equals(strName))
{
System.out.println("已经签过到了");
bIsFind = true;
break;
}
}
if(!bIsFind)
{
KaoqinRecord kq = new KaoqinRecord();
kq.setEmpName(strName);
kq.setTimeStart(strTime);
kq.setTimeEnd("");
lst.add(kq);
System.out.println(strName+"签到成功");
} } }
public void qiantui()
{
System.out.println("请输入要签退的员工姓名");
Scanner sc = new Scanner(System.in);
String strName = sc.nextLine();
if(!lstEmps.contains(strName))
{
System.out.println("不存在该员工");
return;
}
Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat sdfTime = new SimpleDateFormat("hh:mm:ss");
String strDate = sdf.format(date);
String strTime = sdfTime.format(date); if(!map.containsKey(strDate))//今天一个人也没签到
{
System.out.println("你没有签到,不能签退");
}
else
{
List<KaoqinRecord> lst = map.get(strDate);
boolean bIsFind = false;//假设没找到
for(KaoqinRecord kr : lst)
{
if(kr.getEmpName().equals(strName))
{
kr.setTimeEnd(strTime);
bIsFind = true;
break;
}
} if(!bIsFind)//真的没找到,也即意味着没签到
{
System.out.println("你没有签到,不能签退");
}
else
{
System.out.println(strName+"签退成功");
}
}
}
public void showKaoqinInfo()
{
System.out.println("请输入要查找的日期(yyyyMMdd)");
Scanner sc = new Scanner(System.in);
String strDate = sc.nextLine();
List<KaoqinRecord> lst = map.get(strDate);
for(KaoqinRecord kr : lst)
{
System.out.println(kr.getEmpName() + "签到时间为" + kr.getTimeStart()
+ "签退时间为" + kr.getTimeEnd());
}
} public List<String> getLstEmps() {
return lstEmps;
} public void setLstEmps(List<String> lstEmps) {
this.lstEmps = lstEmps;
} public Map<String, List<KaoqinRecord>> getMap() {
return map;
} public void setMap(Map<String, List<KaoqinRecord>> map) {
this.map = map;
}
}

考勤记录类

package com.company;

import java.util.Date;

/**
* Created by ttc on 2017/6/30.
*/
//某一个员工,某天的的考勤情况
public class KaoqinRecord {
private String empName;
private String timeStart;
private String timeEnd; public String getTimeStart() {
return timeStart;
} public void setTimeStart(String timeStart) {
this.timeStart = timeStart;
} public String getTimeEnd() {
return timeEnd;
} public void setTimeEnd(String timeEnd) {
this.timeEnd = timeEnd;
} public String getEmpName() {
return empName;
} public void setEmpName(String empName) {
this.empName = empName;
} }

主程序类:

package com.company;

import java.util.Calendar;
import java.util.Scanner; public class Main { public static void main(String[] args) {
// write your code here
//
// 20170630---zhangsan 9:00 18:00,lisi 9:00 18:00,wangwu 10:00 21:00
// 20170629---zhangsan 9:00 18:00,lisi 9:00 ,wangwu 10:00 21:00
// Map<String,List<KaoqinRecord>>
//
// map.put("20170630",[{name:hanwei,startTime:8:00,endTime:00},xiuwei]) Company company = new Company(); company.getLstEmps().add("马云");
company.getLstEmps().add("李连杰");
company.getLstEmps().add("赵本山"); Scanner sc = new Scanner(System.in);
int cmd = -1;
Calendar calendar = Calendar.getInstance();
String str = calendar.toString();
do
{
System.out.println("员工考勤系统");
System.out.println("0------exit");
System.out.println("1------签到");
System.out.println("2------签退");
System.out.println("3------显示考勤信息"); cmd = sc.nextInt();
switch (cmd)
{
case 1: {
company.qiandao();
break;
}
case 2: {
company.qiantui();
break;
}
case 3: {
company.showKaoqinInfo();
break;
}
default:
{
break;
}
} }while (cmd != 0); return;
}
}
 
05-23 12:21