Find Us On: Facebook Twitter Rss Feed Blog
Need To Register? Sign Up Login

Java - Payroll System - Phase 2

dickersonka
dickersonka 


For assignment number 2, we are going to enhance our Payroll System to add documentation comments and exception handling. We are also adding an implementation for the equals method to a few of our classes, as well as adding new methods to the PayrollUtility class. Below is the list of modifications we are making to the project (feel free to use the solution I posted in moodle for phase 1). Note: The Object class has a method called equals, which does reference comparison (compares the address of two objects). We are overriding this method in a few of our classes so that they compare the values stored in the instance variables of the objects (compare the state of the objects). Documentation comments are going to be added before the header of each class, constructors and public methods. Employee class: Modification 1: Make the Employee class abstract and remove the calculateGrossAmount, calculateTaxDeduction, and calculateVacationHours methods. The Employee class can no longer be instantiated. Modification 2: In the constructor, as well as in the setAccruedVacationHours method, when the parameter is negative, throw an IllegalArgumentException. Code provided below: // if the vacationHrs parameter is negative, throw an exception. if( vacationHrs >= 0 ) accruedVacationHours = vacationHrs; else throw new IllegalArgumentException("The number of vacation hours cannot be negative."); Modification 3: In the constructor, as well as in the setYearToDate method, when the parameter is negative, throw an IllegalArgumentException. The message for the exception would be: "The year to date amount cannot be negative." Modification 4: Override the equals method that gets inherited from the Object class. This method compares two Employee objects and returns true if they both have the same value for the firstName, lastName, and dateOfBirth instance variables. Code provided below: @Override public boolean equals(Object obj) { // check that the type of the parameter is Employee if( !(obj instanceof Employee) ) return false; // we already know that obj is of type Employee, so it's safe to cast Employee employee = (Employee) obj; // return true or false depending on whether firstName, lastName, and dateOfBirth have the same value return employee.firstName.equals(this.firstName) && employee.lastName.equals(this.lastName) && employee.dateOfBirth.equals(this.dateOfBirth); } HourlyEmployee class: Modification 1: Add an implementation for the calculateVacationHours method that just returns 0 (since HourlyEmployees don’t accrue vacation hours). Modification 2: In the constructor, as well as in the setHourlyRate method, when the parameter is negative, throw an IllegalArgumentException. The message for the exception would be: "The hourly rate cannot be negative." Modification 3: In the constructor, as well as in the setPeriodHours method, when the parameter is negative, throw an IllegalArgumentException. The message for the exception would be: "The period hours cannot be negative." SalariedEmployee class: Modification 1: In the constructor, as well as in the setAnnualSalary method, when the parameter is negative, throw an IllegalArgumentException. The message for the exception would be: "The salary cannot be negative." Manager class: Modification 1: In the constructor, as well as in the setWeeklyBonus method, when the parameter is negative, throw an IllegalArgumentException. The message for the exception would be: "The bonus cannot be negative." Paycheck class: Modification 1: Override the equals method that gets inherited from the Object class. This method compares two Paycheck objects and returns true if they both have the same value for the employeeID, periodBeginDate, and periodEndDate instance variables. Department class: Modification 1: Override the equals method that gets inherited from the Object class. This method compares two Department objects and returns true if they both have the same value for the departmentID and departmentName instance variables. PayrollUtility class: Modification 1: Add a public static method called findEmployee that has two parameters, an ArrayList of Employee elements, and an Employee object to search for in the list. This method returns the element in the list found to be equal to the parameter. Otherwise, it returns null. Note: make sure to use the equals method in the Employee class. The following is the method signature: public static Employee findEmployee(ArrayList employeeList, Employee employee) Modification 2: Add a public static method called findPaycheck that has two parameters, an ArrayList of Paycheck elements, and a Paycheck object to search for in the list. This method returns the element in the list found to be equal to the parameter. Otherwise, it returns null. Note: make sure to use the equals method in the Paycheck class. Modification 3: Add a public static method called findDepartment that has two parameters, an ArrayList of Department elements, and a Department object to search for in the list. This method returns the element in the list found to be equal to the parameter. Otherwise, it returns null. Note: make sure to use the equals method in the Department class. Attachments Tags Clarifications

Answers

Java - Payroll System - Phase 2 - Solution

dickersonka
dickersonka 



Attached is the solution to the Java - Payroll System - Phase 2 assignment including source and comments Attachments
payrollsystem_phase2.zip (10 K) Download
Preview
//generate payroll between dates public String generatePayroll(String beginDate, String endDate, String payDate) { String output = ""; Manager manager; Paycheck paycheck; double grossPay, tax, bonus, netPay; for(Department dept: departmentList) { output += "------------------------------------------------------------------\n"; output += dept.getDepartmentName() + " (" + dept.getDepartmentID() + ")\n"; output += "------------------------------------------------------------------\n"

Preview
{ // instance variables private String departmentID; private String departmentName; private Manager departmentManager; private ArrayList<Employee> listOfEmployees; // constructor public Department(String id, String name, Manager manager, ArrayList<Employee> employees) { departmentID = id; departmentName = name; departmentManager = manager; listOfEmployees = employees; } // overloaded constructor public Department(String id, String

Preview
an exception. if(vacationHrs < 0) throw new IllegalArgumentException("The number of vacation hours cannot be negative."); accruedVacationHours = vacationHrs; } //set year to date pay public void setYearToDate(double ytd) { // if the ytd parameter is negative, throw an exception. if(ytd < 0) throw new IllegalArgumentException("The year to date amount cannot

Preview
new IllegalArgumentException("The hourly rate cannot be negative."); hourlyRate = rate; } //set period hours public void setPeriodHours(double periodHrs) { //period hours cannot be less than 0 if(periodHrs < 0) throw new IllegalArgumentException("The period hours cannot be negative."); periodHours = periodHrs; } @Override public String toString() {

Preview
be less than 0 if(bonus < 0) throw new IllegalArgumentException("The bonus cannot be negative."); weeklyBonus = bonus; } // overloaded constructor public Manager(String id, String first, String last, String birth, String hired, double salary, double bonus) { super(id, first, last, birth, hired, salary); //bonus cannot be less than

Preview
for calculations * @author Mayelin */ public

Preview
} //set period begin date public void setPeriodBeginDate(String beginDate) { periodBeginDate = beginDate; } //set period end date public void setPeriodEndDate(String endDate) { periodEndDate = endDate; } //set pay date public void setPayDate(String payDt) { payDate = payDt; } //set gross amount public void setGrossAmount(double payAmt) { grossAmount =

Preview
hourly rate."); try { Employee employee_3 = new HourlyEmployee("3", "Rosa", "Ramirez", "11/07/1988", "08/25/2014", 0, 1400.0, null, -10.0, 30.0); } catch( IllegalArgumentException ex) { System.out.println(ex.getMessage()); } System.out.println("\nTrying to create an HourlyEmployee with negative period hours."); try { Employee

Preview
double VAC_HOURS_LESS_THAN_THRESHOLD = 2; public static final double VAC_HOURS_THRESHOLD_OR_MORE = 3; public static final double BONUS_TAX_RATE = 0.20; //convert value to currency formatted string public static String convertToCurrencyString(double value) { NumberFormat fmt = NumberFormat.getCurrencyInstance(); return String.format("%13s%n", fmt.format(value)); } //convert value to currency formatted string with left alignment

Preview
} // setter method //set annual salary public void setAnnualSalary(double salary) { //salary cannot be less than 0 if(salary < 0) throw new IllegalArgumentException("The salary cannot be negative."); annualSalary = salary; } @Override public String toString() { return super.toString() + String.format("%-27s%s\n", "Annual Salary:", PayrollUtility.convertToCurrencyStringLeftAligned(annualSalary) ); }

PayrollSystemPhase2_output.png (8 K) Download Preview

Screenshots
Screenshot
Purchase Answer