繁体   English   中英

添加从文本文件中读取的双打

[英]Add the doubles read from a text file

我差不多完成了我的项目,但我需要一些帮助。 我需要添加工作时间和工作小时数(例如,加班时间的输出为7,因为只有两个人工作时间。)但我不知道如何继续工作。 这是代码:

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)
        {


}

这是输出应该是什么样子:

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

这是现在的情况,因为我不知道如何添加小时数:

 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

你已经有了grossAmount = 0 ,所以你可以做同样的事情来计算加班时间。 由于您已经有条件,因此将加班时间添加到“新”变量(例如hoursOvertime )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM