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

时间:2014-05-05 22:36:53

标签: java file-io text-files

我几乎完成了我的项目,但我需要一些帮助。我需要添加工作时间和工作小时数(例如,加班时间的输出为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

1 个答案:

答案 0 :(得分:2)

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