In: Computer Science
I need to update this java program to take input about each employee from a file and write their information and salary/pay information to a file. use input file
payroll.txt
import java.util.Scanner;
public class SalaryCalcLoop
{
double Rpay = 0, Opay = 0;
void calPay(double hours, double rate) {
if (hours <= 40)
{
Rpay = hours * rate;
Opay = 0;
}
else
{
double Rhr, Ohr;
Rhr = 40;
Ohr = hours - Rhr;
Rpay = Rhr * rate;
Opay = Ohr * (1.5 * rate);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String name;
int shift = 0;
Double rate, hours;
System.out.println("Pay Calculator");
String ch = "";
do
{
System.out.println("Enter Your Name");
name = sc.next();
System.out.println("Enter Your Shift, Enter 0 for Day, Enterv1 for
Night");
System.out.println("0=Day, 1= Night");
shift=sc.nextInt();
System.out.println("Enter Number of Hours Worked");
hours = sc.nextDouble();
System.out.println("Enter Hourly Pay");
rate = sc.nextDouble();
SalaryCalc c = new SalaryCalc();
c.calPay(hours, rate);
Double Tpay = c.Rpay + c.Opay;
System.out.println();
System.out.println("Calculate Pay");
System.out.println("Employee Name: " + name);
System.out.println("Employee Regular Pay: " + c.Rpay);
System.out.println("Employee Overtime Pay: " + c.Opay);
System.out.println("Employee Total Pay: " + Tpay);
if (shift == 0)
{
System.out.println("Employee PayPeriod is Friday");
}
else
{
System.out.println("Employee PayPeriod is Saturday");
}
System.out.println("Press Y to continue. Press any other key to
exit ");
ch=sc.next();
}
while(ch.equalsIgnoreCase("y"));
}
}
Saturday"); } System.out.println("Press Y to continue. Press any other key to exit "); ch=sc.next(); } while(ch.equalsIgnoreCase("y")); } }
Payroll.txt
Kevin
Yang
60
20
Trey
Adams
30
15
Rick
Johnson
45
10
Cynthia
Wheeler
55
11.50
Sarah
Davis
24
10
Muhammad
Rabish
66
12
Dale
Allen
11
18
Andrew
Jimenez
80
15
Thanks for the question. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks Note: Update the input and output file file name in the below code // read from the below file private static final String INPUT_FILE = "Payroll.txt"; // writes the calculated pay to the below file private static final String OUTPUT_FILE = "Payroll_Output.txt"; =========================================================================== import java.io.*; import java.util.Scanner; public class SalaryCalculator { // read from the below file private static final String INPUT_FILE = "Payroll.txt"; // writes the calculated pay to the below file private static final String OUTPUT_FILE = "Payroll_Output.txt"; public static void main(String[] args) { try { Scanner fileReader = new Scanner(new File(INPUT_FILE)); PrintWriter writer = new PrintWriter(new FileWriter(OUTPUT_FILE)); while (fileReader.hasNext()) { String firstName = fileReader.nextLine(); String lastName = fileReader.nextLine(); int hours = Integer.parseInt(fileReader.nextLine()); double rate = Double.parseDouble(fileReader.nextLine()); double regularPay = regularPay(hours, rate); double overtimePay = overtimePay(hours, rate); writer.write(firstName + " " + lastName + "\r\n"); writer.write("Regular Pay: $" + String.format("%.2f\r\n", regularPay)); writer.write("Overtime Pay: $" + String.format("%.2f\r\n", overtimePay)); writer.write("Total Pay: $" + String.format("%.2f\r\n", overtimePay + regularPay)); } fileReader.close(); writer.close(); } catch (FileNotFoundException e) { System.out.println("Unable to read data from file: " + INPUT_FILE); } catch (IOException e) { System.out.println("Unable to write to file: " + OUTPUT_FILE); } } // takes in hours and rate returns the regular pay public static double regularPay(int hours, double rate) { if (hours <= 40) return hours * rate; else return 40 * rate; } // takes in hours and rate and returns the overtime pay public static double overtimePay(int hours, double rate) { if (hours <= 40) return 0; else return (hours - 40) * hours; } }
=================================================================