gpt4 book ai didi

java - 添加来自 2 个不同文件的值

转载 作者:太空宇宙 更新时间:2023-11-04 10:42:05 26 4
gpt4 key购买 nike

在我的程序中,我扫描了 2 个文件,第一个文件获取了工作时间和 payRate 来计算每个员工的基本工资,第二个文件获取了每个员工的销售额以计算该周的佣金。

然后,我将结果合并到一个文件中,但我需要添加每位员工的佣金和基本工资,以获得每位员工的每周总工资。我在这里迷失了,我想添加每个人的基本工资及其各自的佣金以获得每周总工资,另外,我添加了社会保障号码,有没有一种方法可以通过单独的文件或相同的文件扫描具有相同标识符的号码(在本例中为相同的社会保障号码)并添加相应的值来做到这一点? Two files (1) Salary_Hours (2) Sales

import java.util.*;
import java.io.*;

public class Payroll_Sales {

private static Scanner kb = new Scanner(System.in);

public static void main(String[] args) throws IOException {

File fileSalary = new File ("Salary.txt");
File salaryFile = new File ("NewPrint.txt");
PrintWriter salaryPrint = new PrintWriter (salaryFile);
salaryPrint = getSalary (fileSalary, salaryFile);

File fileSales = new File ("Sales.txt");
FileWriter salesFile = new FileWriter ("NewPrint.txt", true);
PrintWriter salesPrint = new PrintWriter (salesFile);
salesPrint = getSales (fileSales, salesFile);
}
private static PrintWriter getSales(File fileSales, FileWriter salesFile) throws FileNotFoundException {
PrintWriter salesPrint = new PrintWriter (salesFile);
Scanner scan = new Scanner (fileSales);
String ssn;
double sales = 0, commission=0, salesCommission=0;
while (scan.hasNext()) {
ssn = scan.next();
sales = scan.nextDouble();
if (sales >= 10000) {
commission = .15;
}
else if (sales >= 7500) {
commission = .10;
}
else if (sales >= 4500) {
commission = .07;
}
else {
commission = .05;
}
salesCommission = commission*sales;
salesPrint.printf("%11s $ %,3.2f \n", ssn, salesCommission);
System.out.printf("%11s $ %,3.2f \n", ssn, salesCommission);
}
salesPrint.close();
return salesPrint;
}
private static PrintWriter getSalary(File fileSalary, File salaryFile) throws FileNotFoundException {
PrintWriter salaryPrint = new PrintWriter (salaryFile);
Scanner scan = new Scanner (fileSalary);
String ssn;
double salary = 0, hours=0, payRate=0;
while (scan.hasNext()) {
ssn = scan.next();
payRate = scan.nextDouble();
hours = scan.nextDouble();
salary = payRate * hours;
salaryPrint.printf("%11s $ %,3.2f \n", ssn, salary);
System.out.printf("%11s $ %,3.2f \n", ssn, salary);

}
System.out.println();
salaryPrint.println();
salaryPrint.close();
return salaryPrint;
}
}

CombineFile_Salary and SalesCommissions

最佳答案

根据我的经验,我倾向于将文件或 IO 工作分为两个步骤。

1)将输入解析为模型

这可能是一个代表您的应用程序的类,其中包含针对不同数据类型的附加类(员工有销售数量、工资率、工时数等)

2)现在您已经有了模型,请介绍您的业务逻辑,因为解析完成后您应该拥有所需的所有信息。

请注意,根据输入的大小,所需的内存量将按比例缩放,因此如果这些文件的大小非常大,我建议您引入数据库或其他东西,因为这将更容易管理(根据您的初始代码,我认为这不是必需的)

伪代码看起来像这样

public static <ModelClass> parseInput(file1, file2){
//Read through the files and instantiate your model data
//This should cover any information you need in your business logic
//(Meaning you should be able to close the files and not open them again
}

public <ModelClass> calculatePay(<ModelClass> model){
//Use your data structure to compute what you want to compute
//Return modified instance of the model
}

我建议这样做的原因是因为我过去花了太多时间试图一次性完成所有工作(从文件中解析数据并计算逻辑)。这并不总是正确的答案,但这就是重构的目的!

关于java - 添加来自 2 个不同文件的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48857244/

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