You already have grossAmount = 0
, so you can do the same thing for calculating overtime hours. Since you already have a conditional, simple add the overtime hours to your "new" variable (such as hoursOvertime
).
Add the doubles read from a text file
-
16-07-2023 - |
题
I'm almost done with my project but I need some help. I need to add the Hours Overtime and Hours Worked (e.g. the output for Hours Overtime would be 7 because only two people worked ovvertime.) but I do not know how to proceed. Here is the code:
static final double OVERTIME_RATE = 1.5;
static final double FULL_TIME = 40.0;
static double totalGrossAmount;
static double grossAmount;
static double hourlyRate;
static double hoursWorked;
static double hoursOT;
static double totalHoursOT;
static StringBuffer employeeName;
public static void main(String[] args) throws FileNotFoundException {
reportOverlay();
makeFile();
Scanner scan = new Scanner(new FileReader("Employee.dat"));
while(scan.hasNextLine()) {
employeeName = new StringBuffer(scan.nextLine());
hoursWorked = new Double(scan.nextDouble());
hoursOT = new Double(scan.nextDouble());
hourlyRate = new Double(scan.nextDouble());
scan.nextLine();
double[] pay = processPay(hoursWorked, hoursOT, hourlyRate);
printEmployeeInfo(employeeName, hoursWorked, hoursOT, hourlyRate,
pay[0]);
}
scan.close();
totalAmounts();
}
static void reportOverlay() {
String reportStr = "Employee Hours Hours Pay Amount \n"
+ "Name Worked Overtime Rate Earned \n"
+ "-------------------- -------- --------- -------- --------\n";
System.out.print(reportStr);
}
static double[] processPay(double hoursWorked, double hoursOT, double payRate ) {
grossAmount = 0; // <*********
if(hoursWorked > FULL_TIME)
{
grossAmount = (payRate * FULL_TIME) +
(payRate * OVERTIME_RATE * (hoursWorked - FULL_TIME));
}
else
grossAmount = payRate * hoursWorked;
totalGrossAmount = totalGrossAmount + grossAmount;
return new double[] {grossAmount};
}
static void printEmployeeInfo(StringBuffer employeeName, double hoursWorked,
double hoursOT, double payRate,
double gross)
{ // <*********
System.out.printf("%-20s %8.2f %9.2f %8.2f %8.2f%n", employeeName,
hoursWorked, hoursOT, payRate, gross); // <*********
}
static void totalAmounts() {
System.out.printf("Total %51.2f%n", totalGrossAmount);
System.out.println();
}
static void makeFile() throws FileNotFoundException {
PrintWriter printF = new PrintWriter("Employee.dat");
printF.write("Bugs Bunny\n40 0 15.25\nRoad Runner\n35 0 15.35\n"
+"Wild E. Coyote\n45 5 16.00\nDaffy Duck\n42 2 15.75\n");
printF.close();
}
/*static void totalHours() {
totalHoursOT = hoursWorked - FULL_TIME;
if (totalHoursOT < 0)
{
}
Here is what the output should look like:
Employee Hours Hours Pay Amount
Name Worked Overtime Rate Earned
-------------------- -------- --------- -------- --------
Bugs Bunny 40.00 0.00 15.25 610.00
Road Runner 35.00 0.00 15.35 537.25
Wild E. Coyote 45.00 5.00 16.00 760.00
Daffy Duck 42.00 2.00 15.75 677.25
Total 162 7 2584.50
Here is what it is right now because I don't know how to add the hours:
Employee Hours Hours Pay Amount
Name Worked Overtime Rate Earned
-------------------- -------- --------- -------- --------
Bugs Bunny 40.00 0.00 15.25 610.00
Road Runner 35.00 0.00 15.35 537.25
Wild E. Coyote 45.00 5.00 16.00 760.00
Daffy Duck 42.00 2.00 15.75 677.25
Total 2584.50
解决方案
不隶属于 StackOverflow