【问题标题】:Adding values from 2 different files从 2 个不同的文件中添加值
【发布时间】:2018-02-18 22:20:36
【问题描述】:

在我的程序中,我扫描 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

【问题讨论】:

  • 一般建议,首先定义您需要的信息。你似乎需要某种Employee,我认为它有某种标识符;某种TimeSheetPayRate;和某种Sale 对象,我假设它包含有关销售总额的信息。从文件中,您需要将信息加载到这些类的实例中,然后从那里组合结果,这就是能够识别员工的重要之处

标签: java methods printwriter


【解决方案1】:

根据我的经验,我倾向于将文件或 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
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-13
    • 2020-09-27
    • 2022-11-03
    • 1970-01-01
    • 1970-01-01
    • 2014-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多