The Payroll Class: This class simulates the payroll for a company. It has the fo
ID: 3663071 • Letter: T
Question
The Payroll Class: This class simulates the payroll for a company. It has the following attributes: Attributes: employeeList: An array of type Employee that contains the employees in the company. Every element of the array will contain an instance of class Employee. count: A count of the number of employees in the company, this may be less than the maximum number of employees the company can have. max: A count of the maximum number of employees the company can have. Methods: 1. Constructor: This will be a constructor that will receive the maximum number of employees in the company. In this method you will also instantiate the employeeList array using the maximum number of employees. 2. “Add Employee”: This void instance method will create an instance of class Employee, and insert it into the employeeList array at the END of the current set of employees, and increments the “count” of the employees. 3. “Calculate Paycheck”: This void instance method iterates through the employeeList array calculating pay checks for all of the employees by applying the “Calculate Paycheck” instance method from Class Employee to each element of the array. 4. “Print List”: This void instance method prints the contents of the payroll. First a column heading for each of the attributes of an employee are printed, and then iterate through the employeeList array printing the contents of each instance using the print method from Class Employee. The main program The main program is responsible for reading a fixed format data file, and creating a Payroll. Main must: • You create an instance of class Payroll • You will read the data file and call Add Employee for each employee one at a time, until the class is filled. • Your program will calculate paychecks and print the payroll using the appropriate methods from class Payroll.
Explanation / Answer
import java.io.FileReader;
import java.util.*;
public class Payroll
{
private Scanner scanner;
private PayrollMenu menu;
private ArrayList<Employee> employees;
public Payroll(String input)
{
try
{
scanner = new Scanner(new FileReader(input));
}
catch(FileNotFoundException e)
{
System.out.println("Input file not found!");
System.exit(1);
}
menu = new PayrollMenu(scanner);
employees = new ArrayList<Employee>();
}
public Payroll()
{
scanner = new Scanner(System.in);
menu = new PayrollMenu(scanner);
employees = new ArrayList<Employee>();
}
public void go()
{
//welcome();
menu = new PayrollMenu(scanner);
PayrollCommand command = menu.getNextCommand();
if (command == PayrollCommand.ADD_EMPLOYEE)
addEmployee();
else if (command == PayrollCommand.REMOVE_EMPLOYEE)
removeEmployee();
else if (command == PayrollCommand.SHOW_EMPLOYEES)
showEmployees();
else if (command == PayrollCommand.DO_WEEKLY_PAYROLL)
doWeeklyPayroll();
else if (command == PayrollCommand.QUIT)
quit();
}
public boolean isThere(String name)
{
boolean present = false;
for (int index = 0; index < employees.size(); index++)
{
if (name.equals(employees.get(index).getName()))
{
present = true;
}
}
return present;
}
public void welcome()
{
System.out.println("");
System.out.println(": The Price You Pay");
System.out.println("Dany and Adam jack");
System.out.println("");
}
public void addEmployee()
{
String employeeName;
String employeeType;
int wage;
//System.out.println("Please enter new employee name:");
employeeName = scanner.nextLine();
if (isThere(employeeName))
{
//System.out.println("The employee, "+employeeName+" is already in the database. Please try again.");
go();
}
//System.out.println("Please specify employee type (hourly, salaried, contract):");
employeeType = scanner.nextLine();
if (employeeType.compareToIgnoreCase("hourly") == 0) //takes in employee type
{
//System.out.println("What is the hourly wage of this employee (in cents): ");
try
{
wage = (int)(scanner.nextDouble() * 100); //takes in hourly wage
scanner.nextLine();
}
catch (InputMismatchException ime)
{
//System.out.println("Invalid entry. Hourly wage needs to be a positive integer in cents. Please try again:");
wage = (int)(scanner.nextDouble() * 100);
scanner.nextLine();
go();
}
Employee newHourlyEmployee = new hourlyEmployee(employeeName, wage);
employees.add(newHourlyEmployee);
System.out.println("SUCCESS! - New hourly employee, "+employeeName+", added successfully!");
go();
}
else if (employeeType.compareToIgnoreCase("salaried") == 0)
{
//System.out.println("What is the annual wage of this employee (in cents): ");
try
{
wage = (int)(scanner.nextDouble() * 100);
scanner.nextLine();
}
catch (InputMismatchException ime)
{
//System.out.println("Invalid entry. Annual wage needs to be a positive integer in cents. Please try again:");
wage = (int)(scanner.nextDouble() * 100);
scanner.nextLine();
go();
}
Employee newSalariedEmployee = new salariedEmployee(employeeName, wage);
employees.add(newSalariedEmployee);
System.out.println("SUCCESS! - New salaried employee, "+employeeName+", added successfully!");
go();
}
else if (employeeType.compareToIgnoreCase("contract") == 0)
{
//System.out.println("What is the hourly wage of this employee (in cents): ");
try
{
wage = (int)(scanner.nextDouble() * 100);
scanner.nextLine();
}
catch (InputMismatchException ime)
{
//System.out.println("Invalid entry. Hourly wage needs to be a positive integer in cents. Please try again:");
wage = (int)(scanner.nextDouble() * 100);
scanner.nextLine();
go();
}
Employee newContractEmployee = new contractEmployee(employeeName, wage);
employees.add(newContractEmployee);
System.out.println("SUCCESS! - New contract employee, "+employeeName+", added successfully!");
go();
}
//System.out.println("Invalid employee type. Please try again.");
go();
}
public void removeEmployee()
{
String name;
boolean found = false;
int i = 0;
//scanner = new Scanner(System.in);
//System.out.println("Please input the full name of the employee you wish to remove: ");
name = scanner.nextLine();
while (i < employees.size() && (found == false))
{
if (name.compareToIgnoreCase(employees.get(i).getName()) == 0)
{
employees.remove(i);
found = true;
}
i++;
}
if (found == false)
System.out.println("ERROR! - The employee, "+name+", was not found in the database and could not be removed.");
else
System.out.println("SUCCESS! - Employee, "+name+", successfully removed from the database!");
}
private void showEmployees()
{
int c;
for (c = 0; c < employees.size(); c++)
{
System.out.println(employees.get(c).toString());
}
go();
}
public void doWeeklyPayroll()
{
ArrayList<String> payroll = new ArrayList<String>();
int d = 0;
int e = 0;
int hoursWorked;
for (d = 0; d < employees.size(); d++)
{
//System.out.println("How many hours did " +employees.get(d).getName()+" work this week?");
String temp = scanner.nextLine();
hoursWorked = scanner.nextInt();
scanner.nextLine();
String tempPayroll = employees.get(d).createWeeklyPaycheck(hoursWorked).toString();
payroll.add(d, tempPayroll);
}
for (e = 0; e < payroll.size(); e++)
{
System.out.println (payroll.get(e));
}
go();
}
public void quit()
{
System.out.println("Thank you for using the payroll program. Good Bye...");
System.exit(1);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.