Java面临迟到、缺勤和总工时问题

  • 本文关键字:问题 迟到 Java java
  • 更新时间 :
  • 英文 :


这个问题让我很为难。

我做了一个工资单项目,计算每周的工作时间,下班时间和迟到时间;满足以下条件

> An employee is considered LATE if he/she logged in beyond 10 minutes from 8AM (ex.8:11 AM and above) 
> An employee is considered LATE if he/she logged in beyond 10 minutes from 1PM (ex.1:11 AM and above)
> An employee is considered UNDERTIME if he/she logged-out 15 minutes prior to 12NN (ex. 11:45 PM and above) 
> An employee is considered UNDERTIME if he/she logged-out 15 minutes prior to 5PM (ex. 4:45 PM and above)

我所做的就是初始化一些变量

String [] days= {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
String [] tokyotumari={"Morning", "Afternoon"};
int  late=0, loghourIn=0, logminsIn=0,logminsOut=0, loghourOut=0, count=0;
int lateM=0, lateA=0, OrigLate=0, OrigUndertime=0, latehourM, latehourA,totalworkhours=0, LateTime=0, late2=0, under=0, under2=0, Undertime=0;

我做了2个循环。外部循环用于计算天数,而内部循环计算并询问所有内容。

for (int x=0; x < 5; x++){
count++;
System.out.println (days[x]);
for (int y=0; y < 2; y++){
System.out.println (tokyotumari[y]);
System.out.println ("Enter login: ");
String login=scan.next();
System.out.println ("Enter logout: ");
String logout=scan.next();

String in[]=login.split(":");
String out[]=logout.split(":");
loghourIn=Integer.parseInt(in[0]);
logminsIn=Integer.parseInt(in[1]);
loghourOut=Integer.parseInt(out[0]);
logminsOut=Integer.parseInt(out[1]);

//total worked time
lateM=loghourIn%8;
lateA=loghourOut%1;
totalworkhours = totalworkhours + (loghourIn - loghourOut);

//Determines if late or not in hours.
if ((lateM <= 4) && (lateM > 0)) {
late= (late + logminsIn) -10;
}
if ((lateA <= 4) && (lateA > 0)){
late2= (late2 + logminsOut)-10;
}
if ((lateM == 0) && (logminsIn >= 15)){
under= (60-logminsIn)-15;
}
if ((lateA == 0) && (logminsOut >= 15)){
under2=(60-logminsOut)-15;
} 

LateTime=late+late2;
Undertime=under+under2;
OrigLate=OrigLate+LateTime;
OrigUndertime=OrigUndertime+Undertime;
LateTime=0;
Undertime=0;
late=0;                  
late2=0;
under=0;
under2=0;     
} // end of inside loop
System.out.print (OrigLate+" "+OrigUndertime+" "+totalworkhours);

}// end of inside loop

我得到的是结果:

Monday
Morning
Enter login: 
8:00
Enter logout: 
12:00
Afternoon
Enter login: 
1:30
Enter logout: 
5:00
20 0 -8Tuesday
Morning
Enter login: 
7:50
Enter logout: 
11:30
Afternoon
Enter login: 
1:15
Enter logout: 
5:10
25 15 -16Wednesday
Morning
Enter login: 
8:15
Enter logout: 
11:45
Afternoon
Enter login: 
1:10
Enter logout: 
4:44
25 46 -22Thursday
Morning
Enter login: 
8:20
Enter logout: 
11:40
Afternoon
Enter login: 
1:30
Enter logout: 
4:30
45 91 -28Friday
Morning
Enter login: 
9:00
Enter logout: 
11:00
Afternoon
Enter login: 
12:30
Enter logout: 
5:00

输出:Late: 55 Undertime: 91 totalworkhours: 23

知道是怎么回事吗?预期的输出将是:

迟到:1小时50分钟(相当于110分钟)超时时间:1小时11分钟(应该是71分钟)工作时间:36小时59分钟

知道怎么做这个或者我哪里错了吗?

下面的代码不是一个完整的答案。它只计算延迟登录的分钟数。但我认为这足以让你完成你的程序,因为计算早期注销和总工作时间都使用相同的计算方法。

下面的代码使用Java 8中引入的日期时间API。

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.Scanner;
public class WorkHour {
private static final LocalTime  LATE_MORNING_LOGIN = LocalTime.parse("08:10");
private static final LocalTime  LATE_AFTERNOON_LOGIN = LocalTime.parse("13:10");
private static final LocalTime  EARLY_MORNING_LOGOUT = LocalTime.parse("11:45");
private static final LocalTime  EARLY_AFTERNOON_LOGOUT = LocalTime.parse("16:45");
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("h:mm a", Locale.ENGLISH);
Duration lateLogin = Duration.ZERO;
for (DayOfWeek weekday : DayOfWeek.values()) {
if (weekday != DayOfWeek.SATURDAY  &&  weekday != DayOfWeek.SUNDAY) {
System.out.printf("%-9s morning login    [hh:mm]: ", weekday);
String aTime = scan.nextLine();
aTime += " AM";
LocalTime lt = LocalTime.parse(aTime, timeFormatter);
if (lt.isAfter(LATE_MORNING_LOGIN)) {
lateLogin = lateLogin.plus(Duration.between(lt, LATE_MORNING_LOGIN));
}
System.out.printf("%-9s morning logout   [hh:mm]: ", weekday);
aTime = scan.nextLine();
if (Integer.parseInt(aTime.substring(0, 2)) == 12) {
aTime += " PM";
}
else {
aTime += " AM";
}
lt = LocalTime.parse(aTime, timeFormatter);
System.out.printf("%-9s afternoon login  [hh:mm]: ", weekday);
aTime = scan.nextLine();
aTime += " PM";
lt = LocalTime.parse(aTime, timeFormatter);
if (lt.isAfter(LATE_AFTERNOON_LOGIN)) {
lateLogin = lateLogin.plus(Duration.between(lt, LATE_AFTERNOON_LOGIN));
}
System.out.printf("%-9s afternoon logout [hh:mm]: ", weekday);
aTime = scan.nextLine();
aTime += " PM";
lt = LocalTime.parse(aTime, timeFormatter);
}
}
System.out.println(lateLogin);
}
}

请注意,如果您的默认Locale为ENGLISH,则在创建DateTimeFormatter时可能不需要Locale.ENGLISH参数。

下面是上述代码的示例运行-使用您的问题中的示例值。

MONDAY    morning login    [hh:mm]: 8:00
MONDAY    morning logout   [hh:mm]: 12:00
MONDAY    afternoon login  [hh:mm]: 1:30
MONDAY    afternoon logout [hh:mm]: 5:00
TUESDAY   morning login    [hh:mm]: 7:50
TUESDAY   morning logout   [hh:mm]: 11:30
TUESDAY   afternoon login  [hh:mm]: 1:15
TUESDAY   afternoon logout [hh:mm]: 5:10
WEDNESDAY morning login    [hh:mm]: 8:15
WEDNESDAY morning logout   [hh:mm]: 11:45
WEDNESDAY afternoon login  [hh:mm]: 1:10
WEDNESDAY afternoon logout [hh:mm]: 4:44
THURSDAY  morning login    [hh:mm]: 8:20
THURSDAY  morning logout   [hh:mm]: 11:40
THURSDAY  afternoon login  [hh:mm]: 1:30
THURSDAY  afternoon logout [hh:mm]: 4:30
FRIDAY    morning login    [hh:mm]: 9:00
FRIDAY    morning logout   [hh:mm]: 11:00
FRIDAY    afternoon login  [hh:mm]: 12:30
FRIDAY    afternoon logout [hh:mm]: 5:00
PT-1H-50M

注意lateLogin的值是1小时50分钟—正如您所期望的那样。

相关内容

  • 没有找到相关文章

最新更新