简体   繁体   English

如何从文本文件读取,处理和打印数据?

[英]How to read, process, and print data from a text file?

Please I need help with this: 请为此我需要帮助:

A. Further enhance your payroll program. 答:进一步完善您的薪资计划。 Add a Boolean method named inputData that has reference parameters to input an employeeName, hourlyRate, hoursWorked, and taxRate from the payroll.txt file. 添加一个名为inputData的布尔方法,该方法具有参考参数,可从payroll.txt文件中输入employeeName,hourlyRate,hoursWorked和taxRate。 After the statement within the method main that calls the method reportTitle, add a loop that allows the program to read, process, and print information for multiple employees untill all data has been read and processed. 在方法main中的调用方法reportTitle的语句之后,添加一个循环,该循环使程序可以读取,处理和打印多个雇员的信息,直到所有数据都已被读取和处理为止。 The program you write in this exercise does not calculate the grossAmount and netAmount; 您在本练习中编写的程序不会计算grossAmount和netAmount; this will be done in the later exercise. 这将在以后的练习中完成。 In this program, just assign 0.0 to each of those variables. 在此程序中,只需将0.0分配给每个变量。

To permit the method inputData to read and return an employee name, change the type of the variable employeeName from class String to class StringBuffer; 要允许方法inputData读取并返回雇员姓名,请将变量employeeName的类型从String类更改为StringBuffer类; you will have to change the type of the corresponding method parameter in printEmployeeInfo, as well. 您还必须在printEmployeeInfo中更改相应方法参数的类型。

The method inputData also needs to read and return values for hourlyRate, hoursWorked, and taxRate. 方法inputData还需要读取和返回hourlyRate,hoursWorked和taxRate的值。 To make this possible, change the types of the variables hourlyRate, hoursWorked, and taxRate from the primitive type double to the class DoubleClass. 为此,请将变量hourlyRate,hoursWorked和taxRate的类型从原始类型double更改为DoubleClass类。 Defined in the class is a constructor with no parameters that is used to initialize the objects instantiated to 0.0. 在类中定义的是没有参数的构造函数,该构造函数用于初始化实例化为0.0的对象。 The method setNum() is used to set the data member of the object using the method' parameter. setNum()方法用于通过方法的参数设置对象的数据成员。 The method getNum() is used to retrieve the double value stored within the object. 方法getNum()用于检索存储在对象内的double值。

Here's the payroll.txt file: 这是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

Here's the desired output: 这是所需的输出:

                                  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

Here's my code so far: 到目前为止,这是我的代码:

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;
    }
}

Please help me with the next step. 请协助我进行下一步。 I really don't know. 我真的不知道 Thanks in advance. 提前致谢。

Use Scanner class to input from file and System.out.printf to formate your output. 使用Scanner类从文件输入,使用System.out.printf合并输出。 I have modified your code like below: 我已经修改了您的代码,如下所示:

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;
//    }
}

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

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