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

时间:2013-10-05 11:58:30

标签: java text-files

我需要帮助:

一个。进一步增强您的工资计划。添加一个名为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()用于检索存储在对象中的double值。

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

请帮我完成下一步。我真的不知道。提前谢谢。

1 个答案:

答案 0 :(得分: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;
//    }
}
相关问题