public static Dictionary<String, List<AttendanceDate>> LoadFile(String path) { var result = new Dictionary<String, List<AttendanceDate>>(); using (StreamReader reader = new StreamReader(path)) { //丢弃第一行 reader.ReadLine(); while (true) { String line = reader.ReadLine(); if (line == null) { break; } try { var record = new AttendanceDate(line); String id = record.Id; if (!result.ContainsKey(id)) { result[id] = new List<AttendanceDate>(); } result[id].Add(record); } catch(Exception e) { throw new FileFormatException(line, e); } } } return result; }
//穿插上厕所的记录 private void insertToiletRecord(List<AttendanceDate> list, AttendanceDate start, AttendanceDate end) { int count = mRandom.Next(6); if (start.Date.AddMinutes(100) > end.Date) { return; } int totalMinutes = (end.Date.Hour - start.Date.Hour) * 60 + (end.Date.Minute - start.Date.Minute); int step = totalMinutes / 100; if (count > 0) { var positions = genRandomIntsBetween0To100(count); foreach (var item in positions) { list.Add(new AttendanceDate() { Department = start.Department, Name = start.Name, Id = start.Id, Date = start.Date.AddMinutes(item * step) }); } } }
public void TestReadRecord() { string str = "总公司 罗洪鹏 2 2012-04-05 12:33:41 1 FP "; var record = new AttendanceDate(str); Assert.AreEqual("总公司", record.Department); Assert.AreEqual("罗洪鹏", record.Name); Assert.AreEqual("2", record.Id); DateTime date = DateTime.Parse("2012-04-05 12:33:41"); Assert.AreEqual(date, record.Date); }
private static void insertSomebody(String name, String department, String id, List<AttendanceDate> list) { AttendanceDate morning; AttendanceDate afternoon; var morningDate = DateTime.Parse("2012-10-01 09:00:00"); var afternoonDate = DateTime.Parse("2012-10-01 18:00:00"); for (int i = 0; i < 31; i++) { morning = new AttendanceDate { Department = department, Name = name, Id = id, Date = morningDate.AddDays(i) }; afternoon = new AttendanceDate { Department = department, Name = name, Id = id, Date = afternoonDate.AddDays(i) }; list.Add(morning); list.Add(afternoon); } }
public void TestProduceTestData() { var morningDate = DateTime.Parse("2012-01-01 09:00:00"); var afternoonDate = DateTime.Parse("2012-01-01 18:00:00"); var list = new List<AttendanceDate>(); for (int i = 0; i < 31; i++) { var morning = new AttendanceDate { Department = "开发", Name = "郑文伟", Id = "10", Date = morningDate.AddDays(i) }; var afternoon = new AttendanceDate { Department = "开发", Name = "郑文伟", Id = "10", Date = afternoonDate.AddDays(i) }; list.Add(morning); list.Add(afternoon); } String path = "testdata.txt"; using (StreamWriter writer = new StreamWriter(path)) { writer.WriteLine("title"); foreach (var item in list) { writer.WriteLine(item.ToString()); } } var validate = AttendanceDate.LoadFile(path); Assert.AreEqual(list.Count, validate["10"].Count); }