[英]Add the doubles read from a text file
I'm almost done with my project but I need some help. 我差不多完成了我的项目,但我需要一些帮助。 I need to add the Hours Overtime and Hours Worked (eg the output for Hours Overtime would be 7 because only two people worked ovvertime.) but I do not know how to proceed.
我需要添加工作时间和工作小时数(例如,加班时间的输出为7,因为只有两个人工作时间。)但我不知道如何继续工作。 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
You already have grossAmount = 0
, so you can do the same thing for calculating overtime hours. 你已经有了
grossAmount = 0
,所以你可以做同样的事情来计算加班时间。 Since you already have a conditional, simple add the overtime hours to your "new" variable (such as hoursOvertime
). 由于您已经有条件,因此将加班时间添加到“新”变量(例如
hoursOvertime
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.