1

请我需要帮助:

A. 进一步加强您的薪资计划。添加一个名为 inputData 的布尔方法,该方法具有用于从 payroll.txt 文件输入employeeName、hourlyRate、hoursWorked 和taxRate 的引用参数。在方法 main 中调用方法 reportTitle 的语句之后,添加一个循环,允许程序读取、处理和打印多个员工的信息,直到所有数据都被读取和处理。您在本练习中编写的程序不会计算 GrossAmount 和 netAmount;这将在后面的练习中完成。在这个程序中,只需将 0.0 分配给每个变量。

要允许 inputData 方法读取并返回员工姓名,请将变量 employeeName 的类型从 String 类更改为 StringBuffer 类;您还必须更改 printEmployeeInfo 中相应方法参数的类型。

方法 inputData 还需要读取并返回 hourlyRate、hoursWorked 和 taxRate 的值。为此,请将变量 hourlyRate、hoursWorked 和 taxRate 的类型从原始类型 double 更改为类 DoubleClass。类中定义了一个不带参数的构造函数,用于初始化实例化为 0.0 的对象。方法 setNum() 用于使用方法的参数设置对象的数据成员。getNum() 方法用于检索存储在对象中的双精度值。

这是 payroll.txt 文件:

John Smith

9.45 40 15

Jane Doe

12.50 45 15

Harry Morgan

20.00 40 20

Carmen Martinez 

25.00 35 25

Jacintha Washington 

50.85 60 34

这是所需的输出:

                                  Instructions for Payroll Report Program

    This program calculates a paycheck for each employee.
    A text file containing the following information will be created:
    name, pay rate, hours worked, and tax percentage to be deducted.

    The program will create a report in columnar format showing the empoyee name, hourly rate, number of hours worked, tax rate, gross pay, and net pay.

    After all employees are processed, totals will be displayed, including total gross amount and total net pay.


                                          Payroll Report

    Employee                Hourly          Hours           Tax         Gross           Net
    Name                    Rate            Worked          Rate        Amount          Amount
    --------------------    --------        --------        --------    --------        --------
    John Smith                  9.45           40.00           15.00        0.00            0.00
    Jane Doe                   12.50           45.00           15.00        0.00            0.00
    Harry Morgan               20.00           40.00           20.00        0.00            0.00
    Carmen Martinez            25.00           35.00           25.00        0.00            0.00
    Jacintha Washington        50.85           60.00           34.00        0.00            0.00

到目前为止,这是我的代码:

public class Payroll3
{
    final float FULL_TIME = 40;
    public static void main(String[] args)
    {
        StringBuffer employeeName;
        DoubleClass hourlyRate, hoursWorked, taxRate, grossAmount, netAmount;
        instructions();
        reportTitle();
        //printEmployeeInfo(employeeName, hourlyRate, hoursWorked, taxRate,   grossAmount, netAmount);
    }
    public static void instructions()
    {
        System.out.println("\t\t\t\t\t\t\tInstructions for Payroll Report Program\n\nThis program calculates a paycheck for each employee.\nA text file containing the following information will be created:\nname, pay rate, hours worked, and tax percentage to be deducted.\n\nThe program will create a report in columnar format showing the empoyee name, hourly rate, number of hours worked, tax rate, gross pay, and net pay.\n\nAfter all employees are processed, totals will be displayed, including total gross amount and total net pay.\n\n");
    }
    public static void reportTitle()
    {
        System.out.println("\t\t\t\t\t\t\t\tPayroll Report\n\nEmployee            \t\tHourly     \t\tHours       \t\tTax     \t\tGross       \t\tNet       ");
                                      System.out.println("Name\t\t\t\tRate\t\t\tWorked\t\t\tRate\t\t\tAmount\t\t\tAmount");
        System.out.println("--------------------\t\t--------\t\t--------\t\t--------\t\t--------\t\t--------");
    }
    public static void printEmployeeInfo(StringBuffer employeeName, DoubleClass hourlyRate, DoubleClass hoursWorked, DoubleClass taxRate, DoubleClass grossAmount, DoubleClass netAmount)
    {
        System.out.println(employeeName+"\t\t\t    "+hourlyRate+"   "+"\t\t    "+hoursWorked+"\t\t   "+taxRate+"   "+"\t\t  "+grossAmount+"\t\t  "+netAmount+"  OT");
    }
    public static boolean inputData(StringBuffer employeeName, DoubleClass hourlyRate, DoubleClass hoursWorked, DoubleClass taxRate)
    {
        return true;
    }
}

请帮助我进行下一步。我真的不知道。提前致谢。

4

1 回答 1

0

使用 Scanner 类从文件输入并System.out.printf格式化您的输出。我已经修改了您的代码,如下所示:

public class Payroll3
{
    final float FULL_TIME = 40;
    public static void main(String[] args) throws FileNotFoundException
    {
        String employeeName;
        Double hourlyRate, hoursWorked, taxRate, grossAmount=0.0, netAmount=0.0;
        instructions();
        reportTitle();
        Scanner sc=new Scanner(new File("payroll.txt"));
        while(sc.hasNext()){
             employeeName=sc.next()+" "+sc.next();
             hourlyRate=sc.nextDouble();
             hoursWorked=sc.nextDouble();
             taxRate=sc.nextDouble();
             printEmployeeInfo(employeeName, hourlyRate, hoursWorked, taxRate,   grossAmount, netAmount);
        }

    }
    public static void instructions()
    {
        System.out.println("\t\t\t\t\t\t\tInstructions for Payroll Report Program\n\nThis program calculates a paycheck for each employee.\nA text file containing the following information will be created:\nname, pay rate, hours worked, and tax percentage to be deducted.\n\nThe program will create a report in columnar format showing the empoyee name, hourly rate, number of hours worked, tax rate, gross pay, and net pay.\n\nAfter all employees are processed, totals will be displayed, including total gross amount and total net pay.\n\n");
    }
    public static void reportTitle()
    {
        System.out.println("\t\t\t\t\t\t\t\tPayroll Report\n\nEmployee            \t\tHourly     \t\tHours       \t\tTax     \t\tGross       \t\tNet       ");
                                      System.out.println("Name\t\t\t\tRate\t\t\tWorked\t\t\tRate\t\t\tAmount\t\t\tAmount");
        System.out.println("--------------------\t\t--------\t\t--------\t\t--------\t\t--------\t\t--------");
    }
    public static void printEmployeeInfo(String employeeName, Double hourlyRate, Double hoursWorked, Double taxRate, Double grossAmount, Double netAmount)
    {
        System.out.printf("%25s%20.2f%20.2f%20.2f%20.2f  OT\n",employeeName,hourlyRate,hoursWorked,taxRate,grossAmount,netAmount);
    }
//    public static boolean inputData(StringBuffer employeeName, Double hourlyRate, Double hoursWorked, Double taxRate)
//    {
//        return true;
//    }
}
于 2013-10-05T12:20:48.917 回答