一个算法问题
000000042011-10-0808:21:59
000000012011-10-0813:36:59
000000012011-10-0908:17:59
000000012011-10-0918:31:59
000000012011-10-1008:08:59
000000012011-10-1019:01:59
000000012011-10-1108:12:59
000000012011-10-1117:21:59
000000012011-10-1118:51:59
000000012011-10-1208:56:59
上面是一个List<Employee>,分别为(ID,打卡日期,打卡时间)请问如何比较每天第一次打卡和最后一次打卡时间。
如何统计出只有一次打卡的情况。请大神给点代码
[解决办法]
下班时看到,匆忙写了些,没写注释,楼主可以参考下..
现在写完才发现一个问题,"如何统计出只有一次打卡的情况" 是指一个人一天只有一次打卡 还是一个人只有一天打卡 ? 我的代码是按"一个人一天只有一次打卡处理的",下面是我的代码:
/* * To change this template, choose Tools | Templates and open the template in * the editor. */package com.csdn.question;import java.io.*;import java.util.HashMap;import java.util.Map;import java.util.Set;/** * * @author karl */public class QueryPunchCard { public static void init( File src ) throws FileNotFoundException, IOException{ BufferedReader reader = new BufferedReader(new FileReader(src)); String temp = null; while( (temp = reader.readLine()) != null && !temp.equals("")){ String[] subs = temp.split(" "); String id = subs[0]; String date = subs[1]; String time = subs[2]; Employee emplyee = Employee.employees.get(id); if( emplyee == null ){ emplyee = new Employee(id); Employee.employees.put(id,emplyee); } PunchCardRecord record = emplyee.records.get(date); if( record == null ){ record = new PunchCardRecord(date); emplyee.records.put(date,record); } String[] time_subs = time.split(":"); String hour = time_subs[0]; int hour_int = Integer.parseInt(hour); if( hour_int < 12 ){ record.mor_time = time; }else { record.eve_time = time; } } } public static void compareMor_timeAndEve_Time(){ Set<String> ids = Employee.employees.keySet(); if( ids.size() == 0 ) return; for( String id : ids ){ Employee employee = Employee.employees.get(id); employee.show(); } } public static void onlyone(){ Set<String> ids = Employee.employees.keySet(); if( ids.size() == 0 ) return; for( String id : ids ){ Employee employee = Employee.employees.get(id); employee.onlyone(); } } public static void main( String[] args ) throws FileNotFoundException, IOException{ init( new File("H:/employ.txt")); //compareMor_timeAndEve_Time(); //onlyone(); } }class Employee { static Map<String,Employee> employees = new HashMap<String,Employee>(); String id; Map<String,PunchCardRecord> records = new HashMap<String,PunchCardRecord>(); Employee(){} Employee( String id){ this.id = id; } public void show(){ Set<String> dates = records.keySet(); if( dates.size() == 0 ){ return ; } for( String date : dates ){ PunchCardRecord record = records.get(date); System.out.println(" ID: "+id+" Date: "+date+" 第一次打卡: " +record.mor_time+" 最后一次打卡: "+record.eve_time); } } public void onlyone(){ Set<String> dates = records.keySet(); if( dates.size() == 0 ){ return ; } for( String date : dates ){ PunchCardRecord record = records.get(date); if( ( record.mor_time == null && record.eve_time != null ) ||(record.mor_time != null && record.eve_time == null) ){ System.out.println(" ID: "+id+" Date: "+date+" 打卡一次 "); } } } }class PunchCardRecord{ String date; String mor_time; String eve_time; PunchCardRecord(){} PunchCardRecord(String date){ this.date = date; } }测试结果 : 1.比较每天第一次打卡和最后一次打卡时间 ID: 00000001 Date: 2011-10-12 第一次打卡: 08:56:59 最后一次打卡: null ID: 00000001 Date: 2011-10-08 第一次打卡: null 最后一次打卡: 13:36:59 ID: 00000001 Date: 2011-10-09 第一次打卡: 08:17:59 最后一次打卡: 18:31:59 ID: 00000001 Date: 2011-10-11 第一次打卡: 08:12:59 最后一次打卡: 18:51:59 ID: 00000001 Date: 2011-10-10 第一次打卡: 08:08:59 最后一次打卡: 19:01:59 ID: 00000004 Date: 2011-10-08 第一次打卡: 08:21:59 最后一次打卡: null 2.统计出只有一次打卡的情况 ID: 00000001 Date: 2011-10-12 打卡一次 ID: 00000001 Date: 2011-10-08 打卡一次 ID: 00000004 Date: 2011-10-08 打卡一次