m11418166 2017-12-06 03:29 采纳率: 0%
浏览 3764
已采纳

请问一个一个java写的计算考勤的系统 有个小逻辑问题

 import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class daochudaoexcel {
    public static void main(String[] args) throws BiffException, IOException, ParseException {
        String fileName = "D://Io//jisuan.xls"; // Excel文件所在路径  
        File file = new File(fileName); // 创建文件对象  
        Workbook wb = Workbook.getWorkbook(file); // 从文件流中获取Excel工作区对象(WorkBook)  
        Sheet sheet = wb.getSheet(0); // 从工作区中取得页(Sheet)

        int n =0;
        int j =0;
        int f = 0;

        Date max = null;
        String   Stringmax = null;
        String Stringmin = null;
        String zgs = "总公司";

        DateFormat df = new SimpleDateFormat("HH:mm:ss");
        //规定早上最晚打卡时间
        Date time = df.parse("10:00:00");


    while(n<sheet.getRows()){
        //如果是总公司
        if(sheet.getCell(0,n).getContents().equals(zgs)){
           //如果姓名相等
           if(sheet.getCell(1,n).getContents().equals(sheet.getCell(1,n+1).getContents())){
                //如果日期相等
                if(sheet.getCell(2,n).getContents().equals(sheet.getCell(2,n+1).getContents())){
                    //如果第n个时间小于n+1,那么将max等于n+1
                    if(df.parse(sheet.getCell(3,n).getContents()).getTime()<df.parse(sheet.getCell(3,n+1).getContents()).getTime() ){
                        max=df.parse(sheet.getCell(3,n+1).getContents());
                        //将max转成String
                        Stringmax = sheet.getCell(3,n+1).getContents();
                    }
                }else {
                    //如果日期不相等那么下个日期的第一个打卡时间 就是min
                    Date min= df.parse(sheet.getCell(3,n+1).getContents());
                    //将min转为String类型 
                    Stringmin = sheet.getCell(3,n+1).getContents();
                    //如果最早打卡时间大于规定早上最晚打卡时间 那么则判定为迟到
                    if(min.getTime()>time.getTime()){
                    System.err.print("迟到");
                    //如果上班时间大于小于八个小时 则说明早退
                    }else if((max.getTime()-min.getTime())/3600000<8){
                    System.err.print("早退");
                    }
                    //输出员工上班下班时间 和上班多少小时
                    System.out.println(sheet.getCell(0,n).getContents()+"员工:" +sheet.getCell(1,n).getContents()+sheet.getCell(2,n+1).getContents() + "    "+ "上班时间:" + Stringmin + "  " + "下班时间" + Stringmax + "上班了" + "  " +(max.getTime()-min.getTime())/3600000 + "个小时" );
                    }
                }
        }
    n++;
}      
}
}

图片说明

图片说明

需求是输出这个人最早上班时间 最晚下班时间和是否迟到早退 我的逻辑是先判断第一列是不是相等 再判断第二列是否相等 直到判断到日期相等 然后遍历最大的时间替换为max 因为打卡时间是按照从早到晚排列的 到了下一天 那么下一天的第一个打卡时间是最早打卡时间 但是这样第一天的也就是表格中的第一天自动给略过了 因为第一天没有在else中判断 这个怎么解决 新手 请各位大牛帮帮忙 谢谢

  • 写回答

4条回答 默认 最新

  • 张大教主 2017-12-06 07:46
    关注
        //换了一个逻辑,由于打卡时间是从小到大排列的,那么只需定位一个人同部门 ,同一天的最早和最晚打卡时间即可,参照如下
        public static void main(String[] args) throws BiffException, IOException, ParseException {
        String fileName = "D://Io//jisuan.xls"; // Excel文件所在路径  
        File file = new File(fileName); // 创建文件对象  
        Workbook wb = Workbook.getWorkbook(file); // 从文件流中获取Excel工作区对象(WorkBook)  
        Sheet sheet = wb.getSheet(0); // 从工作区中取得页(Sheet)
    
        int n =1;//数据行起始值
    
        DateFormat df = new SimpleDateFormat("HH:mm:ss");
        //规定早上最晚打卡时间
        Date time = df.parse("10:00:00");
        //记录一个周期(同一个地区、同一个人、同一天的上下班)数据
        String firm="";//公司
        String name="";//人员
        String date="";//日期
        String time_start="";//最早打卡时间
        String time_end="";//最迟打卡时间
        int hours=0;//上班时间
        boolean isBegin=true;
        while(n<sheet.getRows()){
            //新起始行,记录最早打卡数据
            if(isBegin){
                firm=sheet.getCell(0,n).getContents();
                name=sheet.getCell(1,n).getContents();
                date=sheet.getCell(2,n).getContents();
                time_start=sheet.getCell(3,n).getContents();
                isBegin=false;
            }
            //找结束行,即最晚打卡数据
            else{
                String this0=sheet.getCell(0,n).getContents();
                String this1=sheet.getCell(1,n).getContents();
                String this2=sheet.getCell(2,n).getContents();
                //判断和上一行是相同性质行
                if(this0.equals(firm) && this1.equals(name) && this2.equals(date)){
                    //判断是最后一行
                    if(!sheet.getCell(0,n+1).getContents().equals(this0) || !sheet.getCell(1,n+1).getContents().equals(this1) && !sheet.getCell(2,n+1).getContents().equals(this2)){
                        time_end=sheet.getCell(3,n).getContents();
                        hours=(df.parse(time_end).getTime()-df.parse(time_start).getTime())/3600000;
                        //判断是否迟到
                        if(df.parse(time_start).getTime()>time.getTime()){
                            System.err.print("迟到");
                        }
                        //判断是否早退
                        else if(hours<8){
                            System.err.print("早退");
                        }
                        //输出员工上班下班时间 和上班多少小时
                        System.out.println(firm+"员工:" +name+date+ "上班时间:" + time_start + "  " + "下班时间" + time_end + "上班了" + "  " +hours.toString()+ "个小时" );
    
                        //当前数据结束,进行下一次统计
                        isBegin=true;
                    }
                }
            }
            n++;
        }      
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻看一个题
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)