Employee.java
public class Employee {
private int empno;
private String name;
public Employee() {
}
public Employee(int empno, String name) {
super();
this.empno = empno;
this.name = name;
}
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Company.java
public class Company {
// 打卡记录名单
private Map<String,List<DaKaInfo>> map = new HashMap<String,List<DaKaInfo>>();
// 员工集合--名单
private List<Employee> list = new ArrayList<Employee>();
public List<Employee> getList(){
return list;
}
public Map<String,List<DaKaInfo>> getMap(){
return map;
}
/**
* 添加员工
* @param e 员工对象
*/
public void addEmployee(Employee e) {
// 查询员工工号是否被占用
for(Employee emp:list) {
if(emp.getEmpno() == e.getEmpno()) {
System.out.println("员工工号已经占用");
return;
}
}
if(list != null) {
list.add(e);
System.out.println("员工添加成功");
}
}
/**
* 根据empno查找员工
* @param empno 员工工号
* @return 找到的员工对象
*/
public Employee findEmployee(int empno) {
if(list != null && !list.isEmpty()) {
for(Employee emp:list) {
if(emp.getEmpno() == empno) {
return emp;
}
}
}
return null;
}
/**
* 根据empno删除员工
* @param empno 员工工号
*/
public void deleteEmployee(int empno) {
if(list != null && !list.isEmpty()) {
for(Employee emp:list) {
if(emp.getEmpno() == empno) {
list.remove(emp);
System.out.println("员工删除成功...");
return;
}
}
System.out.println("查无此人");
}
}
/**
* 显示所有员工信息
*/
public void showInfo() {
if(list != null && !list.isEmpty()) {
for(Employee emp:list) {
System.out.println("员工工号:"+emp.getEmpno()+",员工姓名: "+emp.getName());
}
}
}
}
DaKaInfo.java
public class DaKaInfo {
private int empno;// 工号
private Date startTime;// 签到时间
private Date endTime;// 签退时间
public DaKaInfo() {
}
public DaKaInfo(int empno, Date startTime, Date endTime) {
super();
this.empno = empno;
this.startTime = startTime;
this.endTime = endTime;
}
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public Date getStartTime() {
return startTime;
}
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
public Date getEndTime() {
return endTime;
}
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
}
Main.java
public class Main {
public static Company company = new Company();
// 签到
public static void start(int empno) {
// 1.判断是否有这个人,调用company的findEmployee
Employee e = company.findEmployee(empno);
if(e != null) {
// 2.判断是否打过卡
// 2.1判断是不是第一个打卡的人--查看Map中是否有当天key值
Date date = new Date();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");
String strDate = sdf1.format(date);
// 判断map中是否有strDate,从而判断出是否是以一个打卡的人
if(company.getMap().containsKey(strDate)) {
// 不是第一个打卡
List<DaKaInfo> list = company.getMap().get(strDate);
// 查看是否打过卡
if(list != null && !list.isEmpty()) {
for(DaKaInfo dk1:list) {
if(dk1.getEmpno() == empno) {
System.out.println("您今天 "+sdf2.format(dk1.getStartTime())+" 已经打过卡");
return;
}
}
}
// 没有打过卡直接存打卡信息
DaKaInfo dk = new DaKaInfo();
dk.setEmpno(empno);
dk.setStartTime(date);
list.add(dk);
}else {
// 是第一个打卡
List<DaKaInfo> list = new ArrayList<DaKaInfo>();
// 存打卡信息
DaKaInfo dk = new DaKaInfo();
dk.setEmpno(empno);
dk.setStartTime(date);
// 将打卡记录存入list
list.add(dk);
// 将key值和list存入map
company.getMap().put(strDate, list);
}
System.out.println(empno+"号员工打卡成功");
}else {
System.out.println("查无此人");
}
}
// 签退
public static void end(int empno) {
Employee e = company.findEmployee(empno);
if(e != null) {
Date date = new Date();
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
String strDate = sdf1.format(date);
if(company.getMap().containsKey(strDate)) {
List<DaKaInfo> list = company.getMap().get(strDate);
if(list != null && !list.isEmpty()) {
for(DaKaInfo dk1:list) {
if(dk1.getEmpno() == empno) {
dk1.setEndTime(date);
System.out.println("签退成功");
return;
}
}
System.out.println(empno+"工号员工今天还没有签到");
}
}else {
System.out.println(strDate+"暂无打卡记录");
}
}else {
System.out.println("查无此人");
}
}
// 查询
public static void showDaKaInfo() {
System.out.println("打卡信息如下:");
SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss");
for(Entry<String, List<DaKaInfo>> entry:company.getMap().entrySet()) {
System.out.println(entry.getKey()+"打卡记录如下:");
for(DaKaInfo dk:entry.getValue()) {
System.out.println("工号:"+dk.getEmpno()+",签到时间:"+sdf2.format(dk.getStartTime())+",签退"
+ "时间:"+(dk.getEndTime()==null?"暂未签退":sdf2.format(dk.getEndTime())));
}
}
}
public static void main(String[] args) {
company.addEmployee(new Employee(1001, "小明"));
company.addEmployee(new Employee(1002, "小红"));
company.addEmployee(new Employee(1003, "小虎"));
company.addEmployee(new Employee(1004, "小兰"));
company.addEmployee(new Employee(1005, "小刚"));
while(true) {
System.out.println("欢迎使用Java5班打卡系统");
System.out.println("==================");
System.out.println("员工打卡-----------1");
System.out.println("员工签退-----------2");
System.out.println("查询所有打卡记录-----3");
System.out.println("Exit-------------4");
Scanner scan = new Scanner(System.in);
try {
int option = scan.nextInt();
if(option < 1 || option > 4) {
System.out.println("无效选项");
continue;
}else {
switch(option) {
case 1:
// 打卡
System.out.println("请输入工号:");
int empno1 = scan.nextInt();
start(empno1);
break;
case 2:
// 签退
System.out.println("请输入工号:");
int empno2 = scan.nextInt();
end(empno2);
break;
case 3:
// 查询
showDaKaInfo();
break;
case 4:
System.out.println("谢谢,再见!");
return;
}
}
} catch (Exception e) {
System.out.println("请输入数字");
}
}
}
}