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
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
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")); } }
//Java code
import java.text.NumberFormat; public class Employee { private String firstName; private String lastName; private double hours; private double rate; //Overload constructor public Employee(String firstName, String lastName, double hours, double rate) { this.firstName = firstName; this.lastName = lastName; this.hours = hours; this.rate = rate; } //Constructor public Employee() { } //getters and setters public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public double getHours() { return hours; } public void setHours(double hours) { this.hours = hours; } public double getRate() { return rate; } public void setRate(double rate) { this.rate = rate; } // Calculate pay of employee public double calPay() { double salary =0; if (hours <= 40) { salary = hours * rate; } else { salary = 40*rate + (hours-40)*rate*1.5; } return salary; } /** * * @return Description of the employee object */ @Override public String toString() { return String.format("%5s %20.20s %10.20s","Name: ",firstName+" "+lastName," Salary: "+ NumberFormat.getCurrencyInstance().format(calPay())); } }
//=======================================
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class SalaryCalcLoop { public static void main(String[] args) { //Create employee object Employee employee = new Employee(); try { //Open file to read Scanner sc = new Scanner(new File("payroll.txt")); //Read the file until it has next line while (sc.hasNextLine()) { //Set the attributes employee.setFirstName(sc.next()); employee.setLastName(sc.next()); employee.setHours(Double.parseDouble(sc.next())); employee.setRate(Double.parseDouble(sc.next())); //Print employee object with pay details System.out.println(employee); } } catch (FileNotFoundException e) { System.err.println("File not found!"); } } }
//File
//Output
//If you need any help regarding this solution .......... please leave a comment ...... thanks