package demo.java.date;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
// 日付
public class AttendanceDate {
private List<Person> Persons = new ArrayList<>();
private LocalDate localDate;
public List<Person> getPersons() {
return Persons;
}
public void setPersons(Person person) {
Persons.add(person);
}
public LocalDate getLocalDate() {
return localDate;
}
public AttendanceDate(LocalDate localDate) {
this.localDate = localDate;
}
}
package demo.java.date;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
public class AttendanceManagement {
List<AttendanceDate> dates = new ArrayList<>();
public List<AttendanceDate> getDates() {
return dates;
}
public void setDates(AttendanceDate date) {
this.dates.add(date);
}
}
package demo.java.date;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Random;
public class Client {
public static void main(String[] args) {
// 勤務記録仕組み
AttendanceManagement ams = new AttendanceManagement();
// 社員作成(初期値作成)
Person p = new Person("");
// 勤務日付(初期値作成)
LocalDate localDate = LocalDate.now();
// 時間登録(初期値作成)
LocalDateTime tempTime = LocalDateTime.now();
for (int j=0; j<10;j++) {
// 勤務日付登録する
int day = 1 + j;
localDate = LocalDate.of(2021, 4, day);
// 勤務日付登録
AttendanceDate ad = new AttendanceDate(localDate);
for (int n=0;n<10;n++) {
// 勤務対象者登録
p = new Person("wang" + n);
int startHour = new Random().nextInt(8);
for(int i=0;i<10;i++) {
// 勤務時間作成
int hour = startHour + i;
int minute = 0 + n;
tempTime = LocalDateTime.of(localDate.getYear(), localDate.getMonth(), localDate.getDayOfMonth(), hour, minute);
// 勤務記録する
// 8時前、17時以降は勤務とならない
if (tempTime.getHour() >= 8 && tempTime.getHour() <= 17) {
p.setLocalTimes(tempTime.toLocalTime());
}
}
ad.setPersons(p);
}
// 勤務日付を登録
ams.setDates(ad);
}
// 勤務リスト表示
long minHour = Long.MAX_VALUE;
for (AttendanceDate item : ams.getDates()) {
System.out.println(item.getLocalDate().toString() + "の勤怠状況:");
for (Person person : item.getPersons()) {
System.out.println(" " + person);
Duration duration = Duration.between(person.getLocalTimes().get(0), person.getLocalTimes().get(person.getLocalTimes().size() -1));
if (duration.toHours() < minHour) {
minHour = duration.toHours();
}
System.out.println(" 本日の勤務時間:" + duration.toHours());
}
System.out.println(" 本日の最短勤務時間は:" + minHour + "\n");
}
}
}
package demo.java.date;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
public class Person {
// 名前
private String name;
// 時間
private List<LocalTime> localTimes = new ArrayList<>();
public List<LocalTime> getLocalTimes() {
return localTimes;
}
public void setLocalTimes(LocalTime localTime) {
this.localTimes.add(localTime);
}
public String getName() {
return name;
}
public Person(String name) {
this.name = name;
}
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("社員:");
sb.append(name);
sb.append(" 時間記録:");
for (LocalTime localTime : getLocalTimes()) {
sb.append("[");
sb.append(localTime.toString());
sb.append("]");
}
return sb.toString();
}
}