I am having trouble, the code I have below seems to be computing total grossSala
ID: 3898557 • Letter: I
Question
I am having trouble, the code I have below seems to be computing total grossSalary of all employees in file together rather than individually, same with taxes and net income. I need it to do it individually as well as totaling it all at the end of the output file. It should display in order to the file payRoll.txt : firstName: lastName: payRate: hoursWorked: grossSalary: Taxes: netIncome: and then at the end it should write to the file, number of employees processed: sum of all salaries: sum of all taxes: sum of netIncome:
I reallly just need help with how to get the totals individually from each employee within the text file.
Q: Your program should then read one employee at a time, compute their grossSalary(hoursWorked*payRate), taxes (taxRate*grossSalary), and netIncome (grossSalary-taxes). The programs should produce the payroll information to the output file named payroll.txt. The program should use the following payRate table to compute taxes: package employeeTXT;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class Assignment1 { public static void main(String[] args) throws IOException {
String fileName;
String firstName;
String lastName;
int hoursWorked, payRate, grossSalary;
double netIncome; int countOfEmployees;
fileName = JOptionPane.showInputDialog("Enter the name of the file that holds employee information");
countOfEmployees = Integer.parseInt(JOptionPane.showInputDialog("Enter the number of students in the file"));
Scanner inFile = new Scanner(new FileReader("\Users\sever2845\workspace\" + fileName + ".txt"));
PrintWriter outFile = new PrintWriter("\Users\sever2845\workspace\" + fileName + "-out.txt");
for (int i = 1; i <=countOfEmployees; i++) {
firstName = inFile.next();
lastName = inFile.next();
hoursWorked = inFile.nextInt();
payRate = inFile.nextInt();
JOptionPane.showMessageDialog(null, String.format("Processing %s%s %d%d ", firstName, lastName, hoursWorked, payRate), "Processing", JOptionPane.INFORMATION_MESSAGE);
BufferedWriter bw = new BufferedWriter(new FileWriter("\Users\sever2845\workspace\payroll.txt"));
grossSalary = hoursWorked * payRate; double taxes;
if(grossSalary < 250) taxes = 0.18 * grossSalary;
else if(grossSalary < 550) taxes = 0.23 * grossSalary;
else if(grossSalary < 1100) taxes = 0.28 * grossSalary;
else taxes = 0.33 * grossSalary; netIncome = grossSalary - taxes;
bw.write("Employee " + String.valueOf(i) + " ");
bw.write("Gross Salary = " + String.valueOf(grossSalary)+ " ");
bw.write("Taxes = " + String.valueOf(taxes)+ " "); bw.write("Net Income = " + String.valueOf(netIncome)+ " "); bw.close();
}
}
}
Explanation / Answer
Given below is the fixed code for the question. Please let me know in case of any issues, I'll help.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
import java.io.*;
import java.util.*;
import javax.swing.*;
public class Assignment1 {
public static void main(String[] args) throws IOException {
String fileName;
String firstName;
String lastName;
double hoursWorked, payRate, grossSalary;
double netIncome;
double totalGross = 0, totalNet =0, totalTaxes = 0;
int countOfEmployees;
fileName = JOptionPane.showInputDialog("Enter the name of the file that holds employee information");
countOfEmployees = Integer.parseInt(JOptionPane.showInputDialog("Enter the number of employees in the file"));
Scanner inFile = new Scanner(new FileReader("\Users\sever2845\workspace\" + fileName + ".txt"));
String outfileName = "\Users\sever2845\workspace\payroll.txt";
PrintWriter outFile = new PrintWriter(outfileName);
for (int i = 1; i <=countOfEmployees; i++) {
firstName = inFile.next();
lastName = inFile.next();
hoursWorked = inFile.nextDouble();
payRate = inFile.nextDouble();
JOptionPane.showMessageDialog(null, String.format("Processing %s %s %.2f%.2f ", firstName, lastName, hoursWorked, payRate), "Processing", JOptionPane.INFORMATION_MESSAGE);
grossSalary = hoursWorked * payRate;
double taxes;
if(grossSalary < 250)
taxes = 0.18 * grossSalary;
else if(grossSalary < 550)
taxes = 0.23 * grossSalary;
else if(grossSalary < 1100)
taxes = 0.28 * grossSalary;
else
taxes = 0.33 * grossSalary;
netIncome = grossSalary - taxes;
totalGross +=grossSalary;
totalNet += netIncome;
totalTaxes += taxes;
outFile.printf("%10s %10s %10.2f %10.2f %10.2f %10.2f %10.2f ", firstName, lastName, payRate, hoursWorked, grossSalary, taxes, netIncome);
}
inFile.close();
outFile.println("No. of employees processed: "+ countOfEmployees);
outFile.printf("Total Gross salary: %.2f ", totalGross );
outFile.printf("Total Taxes: %.2f ", totalTaxes);
outFile.printf("Total Net Income: %.2f ", totalNet );
outFile.close();
System.out.println("Please check output file " + outfileName);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.