Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Project Description The goal of this simple payroll system project is twofold: a

ID: 3587456 • Letter: P

Question

Project Description

The goal of this simple payroll system project is twofold: a) serve as a case-study and b) give students the opportunity to connect the dots and implement a full-fledged (albeit simple) application using most of Object-Oriented Programming. Understanding this project, by implementing, testing and debugging it, should provide students with the necessary OOP foundation (classes, information hiding, inheritance and polymorphism) so widely used in real-world applications nowadays. The project, spanning across a few chapters, is developed on a step-by-step basis, starting in unit 5, moving on to inheritance and polymorphism and finally putting it all together in unit 8. All you have to do is to follow the step-by-step instructions, creating your own application and submitting your project via Moodle. At the end of unit 8, you should have a program for a Simple Payroll System in Java that:

·         Keeps a list of employees: Id, Name, Vehicle in the parking lot (if applicable)
·         Enter and save the payroll information: Salary, Bonus, Hourly rate, worked hours
·         Store employee information to an ArrayList.
·         Calculate the payroll
Unit 5 – The first step is to create the necessary classes to the project: Employee, PartTime, FullTime and Vehicle. The main application, which makes use of those classes, is also created here (PayrollSystem). Creating classes and start working with inheritance.

Explanation / Answer

PayrollSystem.java

import java.util.ArrayList;
import java.util.Scanner;

public class PayrollSystem {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ArrayList<Employee> arrEmp = new ArrayList<Employee>();
String varCont ="N";
byte menuOption = 0;
do{
menuOption = showMenu();
switch (menuOption) {
case 1:
FullTime ft;
ft = readNewFullTime();
addEmployee(arrEmp, ft); // add full time employee to arraylist
break;
case 2:
PartTime pt;
pt = readNewPartTime();
addEmployee(arrEmp, pt); // add full time employee to arraylist
break;
case 3:
calcPayroll(arrEmp);
break;
default:
break;
}
} while (menuOption != 4);   
}
  
public static FullTime readNewFullTime (){
/* *****************************************************/
/* this methods CREATES and POPULATES Employee objects */
/* Parameters: None */
/* Return values: new FullTime
/* *****************************************************/
int id = 0;
String name = null;
double sal = 0.0;
double hourAndHalf = 0.0; // overtime
Scanner kbd = new Scanner(System.in);
System.out.print("Enter Id: "); id = kbd.nextInt();
System.out.print(" Enter Name: "); name = kbd.next();
System.out.print(" Enter Salary: "); sal = kbd.nextDouble();
System.out.print(" Enter Bonus: "); hourAndHalf = kbd.nextDouble();
  
FullTime ft1 = null;
ft1 = new FullTime(id, name, sal, hourAndHalf, getVehicle());
  
return ft1;
}
  
public static PartTime readNewPartTime (){
/* *****************************************************/
/* this methods CREATES and POPULATES Employee objects */
/* Parameters: None */
/* Return values: new FullTime
/* *****************************************************/
int id = 0;
String name = null;
double sal = 0.0;
double hourAndHalf = 0.0; // overtime
Scanner kbd = new Scanner(System.in);
System.out.print("Enter Id: "); id = kbd.nextInt();
System.out.print(" Enter Name: "); name = kbd.next();
System.out.print(" Enter Hourly Rate: "); double rate = kbd.nextDouble();
System.out.print(" Enter Number 3of Hours Worked: "); double hoursWorked = kbd.nextDouble();
  
Vehicle v1 = getVehicle();
PartTime pt1 = null;
pt1 = new PartTime(id, name, rate, hoursWorked, v1);
  
return pt1;
}
  
public static byte showMenu () {
  
byte menuOption = 0;
Scanner kbd = new Scanner(System.in);
  
System.out.println(""
+ "/* *************************************************/"
+ " /* 1. Add FullTime */"
+ " /* 2. Add PartTime */"
+ " /* 3. Calculate Payroll */"
+ " /* 4. Exit */"
+ " /* *************************************************/");
System.out.print("Input: "); menuOption = kbd.nextByte();
  
return menuOption;
}
  
public static Vehicle getVehicle() {
/*
* creates and returns a Vehicle Object if "Y. Else returns null
*/
Scanner kbd = new Scanner(System.in);
String hasVehicle = "N";
  
System.out.println(" Does this employee have a vehicle? Y/N : ");
hasVehicle = kbd.next();
  
if (hasVehicle.equalsIgnoreCase("Y")) {
// create and populate object Vehicle
System.out.println(" Enter plate number: "); String auxPlate = kbd.next();
System.out.println(" Enter vehicle colour: "); String auxColour = kbd.next();
return (new Vehicle(auxPlate, auxColour));
}
else { // employee does not have a vehicle
return (null);
}
}
  
public static void addEmployee(ArrayList<Employee> pArrEmp, Employee pEmp) {
// This method add one employee to the arraylist errEmp
pArrEmp.add(pEmp);
}
  
public static void calcPayroll (ArrayList<Employee> pArrEmp) {
double totalCompanyPay = 0.0;
double individualPay = 0.0;
  
// calculate salary - manipulating array only
for (int i = 0; i<pArrEmp.size(); i++) {
System.out.print(" ********************** ");
individualPay =(pArrEmp.get(i)).calculatePay();
Vehicle v = (pArrEmp.get(i)).getVehicle();
String hasVehicle;

// chek employee has a vehicle or not
if (v == null)
hasVehicle = "No";
else
hasVehicle = "Yes";
  
System.out.println("Employee Name: " +(pArrEmp.get(i)).getName());
System.out.println("Has Vehicle: " + hasVehicle);
  
if (v != null) {
System.out.println("Plate Number: " + v.getPlateNumber());
System.out.println("Colour: " + v.getColour());
}
  
System.out.println("Take Home Pay: " + individualPay);
  
totalCompanyPay = totalCompanyPay + individualPay;
}
System.out.println("------------ Total payroll of the company: " + totalCompanyPay + " ----");
}
}


Employee.java

public abstract class Employee {
// Variables
private int empId;
private String name;
private Vehicle vehicle;
  
// Class constructor
public Employee() {
// Default constructor is a constructor with zero parameter
System.out.println("... inside default constructor");
empId = 0;
name = "";
}
  
public Employee (int pEmpId, String pName, Vehicle pV) {
// Non default constructor with at least 1 parameter
System.out.println("... inside Employee non-default constructor");
empId = pEmpId;
name = pName;
this.vehicle = pV;
}
  
public Employee (int pEmpId, String pName, String pPlate, String pColour) {
// Non default constructor with at least 1 parameter
System.out.println("... inside Employee non-default constructor");
empId = pEmpId;
name = pName;
this.vehicle = new Vehicle(pPlate, pColour);
}
  
// Abstract method of the class: calculatePay() will be defined within FullTime and PartTime
// as these both classes are subclasses of Employee abstract class.
public abstract double calculatePay();
  
/**
* @return the empId
*/
public int getEmpId() {
return empId;
}

/**
* @param empId the empId to set
*/
public void setEmpId(int empId) {
this.empId = empId;
}

/**
* @return the name
*/
public String getName() {
return name;
}

/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}

/**
* @return the Vehicle
*/
public Vehicle getVehicle() {
return vehicle;
}

/**
* @param Vehicle the Vehicle to set
*/
public void setVehicle(Vehicle Vehicle) {
this.vehicle = Vehicle;
}
  
}

FullTime.java
public class FullTime extends Employee {
private double salary;
private double overtime;
  
// Define the Fullime class constructor
public FullTime (int id, String name, double sal,
double hourAndHalf, Vehicle vehicle) {
super (id, name, vehicle);
this.overtime = hourAndHalf;
this.salary = sal;
}
  
// Define calculatePay() method from the Employee.java
public double calculatePay() {
System.out.println("Full time employee.");
return (this.getSalary() + this.getOvertime());
}

/**
* @return the salary
*/
public double getSalary() {
return salary;
}

/**
* @param salary the salary to set
*/
public void setSalary(double salary) {
this.salary = salary;
}

/**
* @return the overtime
*/
public double getOvertime() {
return overtime;
}

/**
* @param overtime the overtime to set
*/
public void setOvertime(double overtime) {
this.overtime = overtime;
}
}

Vehicle.java


// Define the Vehicle
public class Vehicle {
private String plateNumber;
private String colour;
  
public Vehicle (String plateNumber, String colour){
this.plateNumber = plateNumber;
this.colour = colour;
}

/**
* @return the plateNumber
*/
public String getPlateNumber() {
return plateNumber;
}

/**
* @param plateNumber the plateNumber to set
*/
public void setPlateNumber(String plateNumber) {
this.plateNumber = plateNumber;
}

/**
* @return the colour
*/
public String getColour() {
return colour;
}

/**
* @param colour the colour to set
*/
public void setColour(String colour) {
this.colour = colour;
}
  
}

PartTime.java

public class PartTime extends Employee {
private double rate;
private double hoursWorked;
  
// Define the PartTime class constructor
public PartTime (int id, String name, double rate,
double hoursWorked2, Vehicle v1) {
super (id, name, v1);
this.rate = rate;
this.hoursWorked = hoursWorked2;
}
public double calculatePay() {
  
System.out.println("Part time employee.");
return (this.getHoursWorked() * this.getRate());
}

/**
* @return the rate
*/
public double getRate() {
return rate;
}

/**
* @param rate the rate to set
*/
public void setRate(double rate) {
this.rate = rate;
}

/**
* @return the hoursWorked
*/
public double getHoursWorked() {
return hoursWorked;
}

/**
* @param hoursWorked the hoursWorked to set
*/
public void setHoursWorked(double hoursWorked) {
this.hoursWorked = hoursWorked;
}
  
}