【发布时间】: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;
}
}
【问题讨论】:
-
一般建议,首先定义您需要的信息。你似乎需要某种
Employee,我认为它有某种标识符;某种TimeSheet和PayRate;和某种Sale对象,我假设它包含有关销售总额的信息。从文件中,您需要将信息加载到这些类的实例中,然后从那里组合结果,这就是能够识别员工的重要之处
标签: java methods printwriter