Problem Statement | |||||||||||||
Often employees at a company time stamp their arrivals and departures, so when the month is over the boss can check how much each employee has worked. Given the time stamps for a single employee during a single day as well as his (or her) hourly wage, calculate how much the employee has earned that day. The time stamps are given in the format "hh:mm:ss" (quotes for clarity only) where hh is the hour (between 00 and 23 inclusive), mm is the minute (between 00 and 59 inclusive) and ss is the second (between 00 and 59 inclusive). All these numbers have exactly two digits. The arrival time stamps are inclusive, and the departure time stamps are exclusive, so an employee arriving at 09:00:00 one day and departing 17:30:00 the same day has worked exactly 8 hours 30 minutes 0 seconds during that interval. An employee working during evenings (between 18:00:00 and 23:59:59, inclusive) or nights (between 00:00:00 and 05:59:59, inclusive) gets paid one and a half times as much during that period. Create a class Salary containing the method howMuch which takes a String[], arrival, and a String[], departure, the arrival and departures times of an employee, respectively, as well an intwage, the hourly wage (in cents). Your method should return an int representing the total amount (in cents) the employee earned during the time he or she worked. The amount should be rounded down to the largest integer less than or equal to the actual amount. Element i in arrival corresponds to elementi in departure. | |||||||||||||
Definition | |||||||||||||
| |||||||||||||
Constraints | |||||||||||||
- | arrival will contain between 1 and 10 elements, inclusive. | ||||||||||||
- | departure will contain between 1 and 10 elements, inclusive. | ||||||||||||
- | arrival will contain the same number of elements as departure. | ||||||||||||
- | All elements in arrival and departure will be in the form "hh:mm:ss" (quotes for clarity only) satisfying the constraints given above. | ||||||||||||
- | All time stamps will be strictly increasing; that is, arrival[0]<departure[0]<arrival[1]<departure[1] and so on. | ||||||||||||
- | wage will be between 100 and 10000, inclusive. | ||||||||||||
Examples | |||||||||||||
0) | |||||||||||||
| |||||||||||||
1) | |||||||||||||
| |||||||||||||
2) | |||||||||||||
| |||||||||||||
3) | |||||||||||||
| |||||||||||||
4) | |||||||||||||
|
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Salary {
public int howMuch(String[] arrival, String[] departure, int wage) {
double sum = 0;
try {
DateFormat df = new SimpleDateFormat("HH:mm:ss");
Date d1 = df.parse("00:00:00");
Date d2 = df.parse("06:00:00");
Date d3 = df.parse("18:00:00");
// Date d4 = df.parse("23:59:59");
int len = arrival.length;
Date arrivalDate[] = new Date[len];
Date departureDate[] = new Date[len];
long oneAndhalf = 0;
long one = 0;
for (int i = 0; i < len; i++) {
arrivalDate[i] = df.parse(arrival[i]);
departureDate[i] = df.parse(departure[i]);
if (arrivalDate[i].compareTo(d1) >= 0
&& arrivalDate[i].before(d2)) {
if (departureDate[i].before(d2))
oneAndhalf += (departureDate[i].getTime() - arrivalDate[i]
.getTime());
else if (departureDate[i].compareTo(d2) >= 0
&& departureDate[i].before(d3)) {
oneAndhalf += (d2.getTime() - arrivalDate[i].getTime());
one += (departureDate[i].getTime() - d2.getTime());
} else {
oneAndhalf += (d2.getTime() - arrivalDate[i].getTime()
+ departureDate[i].getTime() - d3.getTime());
one += (d3.getTime() - d2.getTime());
}
} else if (arrivalDate[i].compareTo(d2) >= 0
&& arrivalDate[i].before(d3)) {
if (departureDate[i].before(d3)) {
one += (departureDate[i].getTime() - arrivalDate[i]
.getTime());
} else {
oneAndhalf += (departureDate[i].getTime() - d3
.getTime());
one += (d3.getTime() - arrivalDate[i].getTime());
}
} else {
oneAndhalf += (departureDate[i].getTime() - arrivalDate[i]
.getTime());
}
}
sum = (oneAndhalf * 1.5 + one) / 3600000.0 * wage;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (int) sum;
}
}