gpt4 book ai didi

java - 如何读取、处理和打印文本文件中的数据?

转载 作者:行者123 更新时间:2023-12-01 04:18:41 26 4
gpt4 key购买 nike

我需要帮助:

A.进一步增强您的薪资计划。添加一个名为 inputData 的 boolean 方法,该方法具有引用参数,用于输入 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.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;
}
}

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

最佳答案

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

关于java - 如何读取、处理和打印文本文件中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19197532/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com