Create a class in java with the following information: The Payroll Class contain
ID: 3702923 • Letter: C
Question
Create a class in java with the following information:
The Payroll Class contains three fields and methods to calculate the wage of an employee.
(1) Please write an application that would allow the user to enter employees’ name, pay rate, and number of hours he/she worked last week. Use the information to instantiate objects of Payroll class, and add each object into an ArrayList.
(2) The user can enter any number of employees. The program should ask the user whether there is any more data to be entered.
(3) After the user finishes entering the payroll information, print all the payroll information. (NOTE: you can directly use the toString() method of ArrayList.
(4) Use a loop to traverse the ArrayList, get each object, use the calWage() method of Payroll to calculate the total wage the employer needs to pay this week.
Explanation / Answer
Using the Payroll class as given below is the class to test it.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
* Class for testing functionality of Payroll class
*
*/
public class PayrollTest {
private static final Scanner in = new Scanner(System.in); // used to get inputs from user via console
public static void main(String[] args) {
List<Payroll> employees = new ArrayList<>(); // list of employees and their payroll information
while(true) {
// confirmation to see if there are any more employees to add
System.out.println("Is there any employee to enter, enter yes (or y) to add more");
String confirmation = in.next();
if(confirmation.equalsIgnoreCase("yes") || confirmation.equalsIgnoreCase("y")) {
// ask user for employee's name, pay rate and number of working hours
System.out.print("Enter employee's name : ");
String name = in.next();
System.out.print("Enter employee's pay rate : ");
double payRate = in.nextDouble();
System.out.print("Enter employee's number of working hours : ");
int numOfHours = in.nextInt();
// create an instance of employee and it's payroll
Payroll emp = new Payroll(name, payRate, numOfHours);
// add the employee to the list
employees.add(emp);
}
else {
break; // come out of the loop if user doesnt say yes to add more employees
}
}
// loop through the employees
for(Payroll emp : employees) {
// print employee's name and total wage
System.out.println(emp.getEmpName() + " needs to pay " + emp.calWage());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.