In: Computer Science
(Salary Calculator) Develop a Java application that determines the gross pay for each of three employees. The company pays straight time for the first 40 hours worked by each employee and time and a half for all hours worked in excess of 40. You’re given a list of the employees, their number of hours worked last week and their hourly rates. Your program should input this information for each employee, then determine and display the employee’s gross pay. Use class Scanner to input the data.
Program
import java.util.ArrayList;
import java.util.Scanner;
/*for storing deatils about the employees*/
class EmployeeDetails{
private String name;
private double hoursWorked, payRate;
public EmployeeDetails(String name, double
hoursWorked, double payRate) {
this.name = name;
this.hoursWorked =
hoursWorked;
this.payRate = payRate;
}
public double calculateGrossPay() {
double payment = 0;
//calculating gross pay as per
instruction
if(hoursWorked > 40)
payment =
payment + (hoursWorked * (1.5 * payRate));
else
payment =
payment + (hoursWorked * payRate);
return payment;
}
@Override
public String toString() {//for showing output
return "Name=" + name + "\nHours
Worked=" + hoursWorked + "\nPay Rate= $" + payRate
+ "\nGross pay= $" + calculateGrossPay();
}
}
public class Test {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);
int size;
String name;
double payRate, hoursWorked;
//for storing details in the
arraylist
ArrayList
System.out.print("Enter the number
of Employees: ");
size = sc.nextInt();
for(int i=0; i
System.out.print("Enter name: ");
name =
sc.nextLine();
System.out.print("Enter hours worked: ");
hoursWorked =
sc.nextDouble();
System.out.print("Enter pay rate: ");
payRate =
sc.nextDouble();
ar.add(new
EmployeeDetails(name, hoursWorked, payRate));
System.out.println();
}
//output
System.out.println("Employee
Details and Gross pay");
System.out.println("--------------------------------");
for(EmployeeDetails i: ar) {
System.out.println(i);
System.out.println("--------------------------------");
}
sc.close();
}
}
Output