Question
Hello I am working on this java assignment and I'm not really sure how to fix an error im having. below is the UML for
Hello I am working on this java assignment and I'm not really sure how to fix an error im having. below is the UML for the program and under that is the code im using. When going through the driver and doing what the program asks I am given a NullPointerException at lines 27, 46 and 65. Any help would be greatly appreciated.
EmployeeManager
EmployeeManager |
- employees : Employee[] - employeeMax : final int = 10 -currentEmployees : int |
< + addEmployee( type : int, fn : String, ln : String, m : char, g : char, en : int, ft : boolean, amount : double) + removeEmployee( index : int) + listAll() + listHourly() + listSalary() + listCommision() + resetWeek() + calculatePayout() : double + getIndex( empNum : int ) : int + annualRaises() + holidayBonuses() : double + increaseHours( index : int, amount : double) + increaseSales( index : int, amount : double) |
Data Members
- Employee[] employees Collection of Employee objects
- final int employeeMax = 10 Maximum number of Employees allowed
- int currentEmployees Current number of Employees in collection
Methods
public EmployeeManager()
Constructor, creates the Employee array, sets currentEmployees to 0.
public void addEmployee(int, String, String, char, char, int, Boolean, double)
Takes an int representing the type of Employee to be added (1 Hourly, 2 Salary, 3 Commission) as well as the required data to create that Employee. If one of these values is not passed output the line, Invalid Employee Type, None Added, and exit the method. If an Employee with the given Employee Number already exists do not add the Employee and output the line, Duplicate Not Added, and exit the method. If the array is at maximum capacity do not add the new Employee, and output the line, "Cannot add more Employees".
public void removeEmployee(int)
Removes an Employee located at the given index from the Employee array.
public void listAll()
Lists all the current Employees. Outputs there are none if there are none.
public void listHourly()
Lists all the current HourlyEmployees. Outputs there are none if there are none.
public void listSalary()
Lists all the current SalaryEmployees. Outputs there are none if there are none.
public void listCommission()
Lists all the current CommissionEmployees. Outputs there are none if there are none.
public void resetWeek()
Resets the week for all Employees.
public double calculatePayout()
Returns the total weekly payout for all Employees.
public int getIndex(int)
Given an Employee Number, returns the index of that Employee in the array, if the Employee doesnt exist retuns -1.
public void annualRaises()
Applies annual raise to all current Employees.
public double holidayBonuses()
Outputs and returns the total holiday bonus of all Employees.
public void increaseHours(int, double)
Increase the hours worked of the Employee at the given index by the given double amount.
public void increaseSales(int, double)
Increase the sales of the Employee at the given index by the given double amount.
import employeeType.employee.Employee; import employeeType.subTypes.CommissionEmployee; import employeeType.subTypes.HourlyEmployee; import employeeType.subTypes.SalaryEmployee; public class EmployeeManager {
private Employee[] employees; private final int employeeMax = 10; private int currentEmployees; public EmployeeManager() { employees = new Employee[employeeMax]; this.currentEmployees = 0; }
public void addEmployee(int type, String fn, String ln, char m, char g, int en, boolean ft, double amount) { switch (type) { case 1: HourlyEmployee x = new HourlyEmployee(fn, ln, m, g, en, ft, amount); for (int i = 0; i < employees.length; i++) { if (employees[i].getEmployeeNumber() == en) { System.out.println("Duplicate Not Added"); return; } } if (currentEmployees == employeeMax) { System.out.println("Cannot add more Employees"); return;
} employees[currentEmployees] = x; currentEmployees++; break; case 2: SalaryEmployee y = new SalaryEmployee(fn, ln, m, g, en, ft, amount); for (int i = 0; i < employees.length; i++) { if (employees[i].getEmployeeNumber() == en) { System.out.println("Duplicate Not Added"); return; } } if (currentEmployees == employeeMax) { System.out.println("Cannot add more Employees"); return; } employees[currentEmployees] = y; currentEmployees++; break;
case 3: CommissionEmployee z = new CommissionEmployee(fn, ln, m, g, en, ft, amount); for (int i = 0; i < employees.length; i++) { if (employees[i].getEmployeeNumber() == en) { System.out.println("Duplicate Not Added"); return; } } if (currentEmployees == employeeMax) { System.out.println("Cannot add more Employees"); return; } employees[currentEmployees] = z; currentEmployees++; break;
default: System.out.println("Invalid Employee Type, None Added"); break; } }
public void removeEmployee(int index) { employees[index] = null; } public void listAll() { if (currentEmployees == 0) System.out.println("none"); else { for (int i = 0; i < currentEmployees; i++) { if (employees[i] != null) System.out.println(employees[i].toString()); } } } public void listHourly() { if (currentEmployees == 0) System.out.println("none"); else { for (int i = 0; i < currentEmployees; i++) { if (employees[i] != null) { if (employees[i] instanceof HourlyEmployee) System.out.println(employees[i].toString()); } } } } public void listSalary() { if (currentEmployees == 0) System.out.println("none"); else { for (int i = 0; i < currentEmployees; i++) { if (employees[i] != null) { if (employees[i] instanceof SalaryEmployee) System.out.println(employees[i].toString()); } } } } public void listCommission() { if (currentEmployees == 0) System.out.println("none"); else { for (int i = 0; i < currentEmployees; i++) { if (employees[i] != null) { if (employees[i] instanceof CommissionEmployee) System.out.println(employees[i].toString()); } } } } public void resetWeek() { for (int i = 0; i < currentEmployees; i++) { if (employees[i] != null) { employees[i].resetWeek(); } } } public double calculatePayout() { double payOut = 0; for (int i = 0; i < currentEmployees; i++) { if (employees[i] != null) { payOut += employees[i].calculateWeeklyPay(); } } return payOut; } public int getIndex(int employeeNum) { for (int i = 0; i < employees.length; i++) { if (employees[i].getEmployeeNumber() == employeeNum) { return i; } } return -1; } public void annualRaises() { for (int i = 0; i < currentEmployees; i++) { if (employees[i] != null) { employees[i].annualRaise(); } } } public double holidayBonuses() { double holiBonus = 0; for (int i = 0; i < currentEmployees; i++) { if (employees[i] != null) { holiBonus += employees[i].holidayBonus(); } } return holiBonus; } public void increaseHours(int index, double amount) { if (employees[index] != null && employees[index] instanceof HourlyEmployee) { HourlyEmployee employee = (HourlyEmployee) employees[index]; employee.increaseHours(amount); } } // Increase the sales of the Employee at the given index by the given double // amount. public void increaseSales(int index, double amount) { if (employees[index] != null && employees[index] instanceof CommissionEmployee) { CommissionEmployee employee = (CommissionEmployee) employees[index]; employee.increaseSales(amount); } } }
Edit: Here are the other classes
Objectives: The focus of this assignment is modifying and utilizing the inheritance hierarchy from the previous assignment. You will need to understand and implement the ideas of basic inheritance, polymorphism, and abstraction.
Program Description:
Building upon the examples shown in class, this project will be a basic record of employees for a company. Many options for changing the list as a whole and updating the individual elements will be provided by the project.
A total of six classes are required. The first four are the same from the previous assignment with potential minor changes.
Employee An abstract class which the other employee types inherit from
HourlyEmployee An employee whose pay is based upon an hourly wage and hours worked
SalaryEmployee An employee whose pay is based upon a yearly salary
CommissionEmployee An employee whose pay is based upon a commission rate and sales amount
EmployeeManager A class that contains an array of Employees and provides utilities to manage the array and the Employees stored within
EmployeeDriver Contains main method. Creates a single EmployeeManager as well as the menu system in order to give the user the ability to use it. This is provided to you. DO NOT MAKE CHANGES
UML DIAGRAM FOR AND DISCUSSION FOR Employee
Employee {abstract} |
- String firstName - String lastName - char middleInitial - boolean fulltime - char gender - int employeeNum |
< + getEmployeeNumber() : int + setEmployeeNumber(empNum : int) + getFirstName() : String + getLastName() : String + setFirstName(fn: String) + setLastName(ln : String) + setMiddleI(m : char) + setGender(g : char) + equals(e2 : Object) : Boolean + toString() : String + calculateWeeklyPay() : double {abstract} + annualRaise() {abstract} + holidayBonus() : double {abstract} + resetWeek() {abstract} |
Notes on Data Members
The employeeNum member must be between 10000 and 99999, inclusive. If an invalid value is passed, the Employee class should immediately ask for another number until an acceptable one is given. This should be handled in setEmployeeNumber.
If an invalid value for gender is given (not M or F) it should default to F. This should be handled in setGender.
Notes on Methods
equals() Overrides Object equals(). Returns true if the employeeNum of the two instances are equal, false otherwise.
toString() Overrides Object toString(). Returns as String of the Employee in the following format:
12345
Doe, John M.
Gender: M
Status: Full Time
(Note that Status doesnt say true or false, rather Full Time or Part Time)
calculateWeeklyPay() Abstract method to be implemented by subclass. Calculates pay for the week.
annualRaise() Abstract method to be implemented by subclass. Gives Employee a raise.
holidayBonus() Abstract method to be implemented by subclass. Calculates bonus for Employee.
resetWeek() Abstract method to be implemented by subclass. Resets the weekly values for Employee.
DISSCUSSION ON Employee SUBCLASSES
HourlyEmployee
HourlyEmployee extends Employee |
- double wage - double hoursWorked |
< + increaseHours(hours : double) + toString() : String + calculateWeeklyPay() : double + annualRaise() + holidayBonus() : double + resetWeek() |
Additional Data Members:
- double wage
- double hoursWorked
Methods
Constructor accepts all that an Employee requires as well as a double for wage, hoursWorked set at 0.0.
Override toString(), returns a String of the HourlyEmployee in the following format:
12345
Doe, John M.
Gender: M
Status: Full Time
Wage: 3.40
Hours Worked: 0.00
Abstract method implementation:
calculateWeeklyPay() Return amount earned in the week using wage and hoursWorked, any hours worked over 40 give double pay
annualRaise() Wage is increased by 5%
holidayBonus() Return amount of 40 hours worked (40*wage)
resetWeek() Resets hours worked to 0
increaseHours() - This class also needs the ability to increase the hours worked. Requesting to increase by a negative value should give no change, and report an error to the user.
SalaryEmployee
SalaryEmployee extends Employee |
- salary : double |
< + toString() : String + calculateWeeklyPay() : double + annualRaise() + holidayBonus() : double + resetWeek() |
Additional Data Members
- double salary
Methods
Constructor accepts all that an Employee requires as well as a double for salary.
Override toString(), returns a String of the SalaryEmployee in the following format:
12345
Doe, John M.
Gender: M
Status: Full Time
Salary: 50000.00
Abstract method implementation:
calculateWeeklyPay() Return amount earned in the week by dividing salary by 52
annualRaise() Salary is increased by 6%
holidayBonus() Return 3% of salary
resetWeek() No change
CommissionEmployee
CommissionEmployee extends Employee |
- sales : double - rate : double |
< + increaseSales(sales : double) + toString() : String + calculateWeeklyPay() : double + annualRaise() + holidayBonus() : double + resetWeek() |
Additional Data Members
- double sales
- double rate (stored as a percent, eg. 3.5% would be stored as 3.5)
Methods
Constructor accepts all that an Employee requires as well as a double for rate, sales set to 0.0.
Override toString(), returns a String of the CommissionEmployee in the following format:
12345
Doe, John M.
Gender: M
Status: Full Time
Rate: 3.50
Sales: 0.00
Abstract method implementation:
calculateWeeklyPay() Return rate percentage of sales
annualRaise() Rate percentage increased .2% example, if rate was 2.5, it becomes 2.7
holidayBonus() No bonus
resetWeek() Reset sales to 0.0
Additional functionality:
increaseSales() - This class also needs the ability to increase the sales. Requesting to increase by a negative value should give no change and report the error.
EmployeeDriver
The EmployeeDriver will create a single object of EmployeeManager as well as any other variables you deem necessary. This is the interface to using the EmployeeManager through a Menu system. The Main Menu should look like this:
Main Menu
No Employees.
1. Employee Submenu
2. Add Employee
3. Remove Employee
4. Calculate Weekly Payout
5. Calculate Bonus
6. Annual Raises
7. Reset Week
8. Quit
Enter Choice:
Where No Employees is listed, ALL of the current Employees must be listed if there are any, No Employees if there are none. Some of the options lead to a submenu which will allow the user to return back to the previous menu. Whenever the main menu is displayed the employees will be listed as they were added. This is provided for you. You must use the version provided and cannot make any changes.
Menu Option Details
1. Employee Submenu
This leads the user to a submenu where they can choose a specific type of Employee. Once one is selected ONLY THE EMPLOYEES OF THAT TYPE are listed. Employees of type HourlyEmployee and CommissionEmployee are offered the option to add hours/sales as well as return to the previous menu. SalaryEmployees can only return to the previous menu.
Main Menu
12345
Parker, Peter B.
Gender: M
Status: Full Time
Wage: 8.50
Hours Worked: 0.00
54321
Spector, Marc J.
Gender: M
Status: Full Time
Wage: 9.80
Hours Worked: 0.00
98765
Pryde, Kitty T.
Gender: F
Status: Part Time
Salary: 25000.00
45678
Curry, Arthur J.
Gender: M
Status: Full Time
Salary: 65000.00
87654
Kent, Clark J.
Gender: M
Status: Part Time
Rate: 3.40
Sales: 0.00
1. Employee Submenu
2. Add Employee
3. Remove Employee
4. Calculate Weekly Payout
5. Calculate Bonus
6. Annual Raises
7. Reset Week
8. Quit
Enter Choice: 1
1. Hourly Employees
2. Salary Employees
3. Commission Employees
4. Back
Enter Choice: 1
12345
Parker, Peter B.
Gender: M
Status: Full Time
Wage: 8.50
Hours Worked: 0.00
54321
Spector, Marc J.
Gender: M
Status: Full Time
Wage: 9.80
Hours Worked: 0.00
1. Add Hours
2. Back
Enter Choice: 2
1. Hourly Employees
2. Salary Employees
3. Commission Employees
4. Back
Enter Choice:
2. Add Employee
Gives a similar submenu, asking which type of Employee to add, prompting for the appropriate information. Returns to main menu after added.
3. Remove Employee
Asks for an employee number and removes the Employee from the array if it exists. If not states that no such Employee exists and none have been removed. Returns to main menu after.
4. Calculate Payout
Reports how much the company will have to pay out given the Employees current sales, hours worked, and salaries
5. Calculate Bonus
Lists all Employees as well as their specific holiday bonus amount after their information. Also reports the total holiday bonus payout.
Enter Choice: 6
12345
Parker, Peter B.
Gender: M
Status: Full Time
Wage: 7.80
Hours Worked: 0.00
Bonus Amount: 312.00
54321
Wayne, Bruce J.
Gender: M
Status: Full Time
Wage: 9.50
Hours Worked: 0.00
Bonus Amount: 380.00
98765
Richards, Reed L.
Gender: M
Status: Part Time
Salary: 20000.00
Bonus Amount: 600.00
65432
Prince, Diana R.
Gender: F
Status: Full Time
Rate: 55000.00
Sales: 0.00
Bonus Amount: 0.00
Total holiday bonus payout is 1292.00
6. Annual Raises
Applies the annual raises for all Employees and notifies that annual raises have been applied.
7. Reset Week
Resets the weekly values for all Employees.
8. Quit
Exits the program giving an exit message.
EmployeeDriver.java
import java.util.Scanner; import employeeType.subTypes.HourlyEmployee; import employeeType.subTypes.SalaryEmployee; import employeeType.subTypes.CommissionEmployee; public class EmployeeDriver { static Scanner in = new Scanner(System.in); public static int menu(String... options) { int choice; for(int line = 0; line < options.length; line++) System.out.printf("%d. %s ", line+1,options[line]); do { System.out.print("Enter Choice: "); choice = in.nextInt(); }while(!(choice > 0 && choice <= options.length)); return choice; } public static void main(String args[]) { int mainInput;//Input for main menu int subInput1;//Input for submenu int subInput2;//Input for sub-submenu int en; //Inputting an employee number int index; double amount; EmployeeManager em = new EmployeeManager(); //The EmployeManager object //Main control loop, keep coming back to the //Main menu after each selection is finished do { //This is the main menu. Displays menu //and asks for a choice, validaties that //what is entered is a valid choice System.out.println(" Main Menu "); em.listAll(); mainInput = menu("Employee Submenu", "Add Employee", "Remove Employee", "Calculate Weekly Payout", "Calculate Bonus", "Annual Raises", "Reset Week", "Quit"); //Perform the correct action based upon Main menu input switch(mainInput) { //Employee Submenu case 1: do { subInput1 = menu("Hourly Employees", "Salary Employee", "Comission Employees", "Back"); switch(subInput1) { case 1: em.listHourly(); do { subInput2 = menu("Add Hours", "Back"); if( subInput2 == 1) { System.out.println("Employee Number: "); en = in.nextInt(); index = em.getIndex(en); if(index != -1) { System.out.print("Enter Hours: "); amount = in.nextDouble(); em.increaseHours(index, amount); } else { System.out.println("Employee not found!"); } } }while(subInput2 != 2); break; case 2: em.listSalary(); subInput2 = menu("Back"); break; case 3: em.listCommission(); do { subInput2 = menu("Add Sales", "Back"); if( subInput2 == 1) { System.out.println("Employee Number: "); en = in.nextInt(); index = em.getIndex(en); if(index != -1) { System.out.print("Enter Sales: "); amount = in.nextDouble(); em.increaseSales(index, amount); } else { System.out.println("Employee not found!"); } } }while(subInput2 != 2); break; } }while(subInput1 != 4); break; //Add Employee case 2: String fn, ln; char mi, g, f; boolean ft = true; subInput1 = menu("Hourly", "Salary", "Commission"); System.out.print("Enter Last Name: "); ln = in.next(); System.out.print("Enter First Name: "); fn = in.next(); System.out.print("Enter Middle Initial: "); mi = in.next().charAt(0); System.out.print("Enter Gender: "); g = in.next().charAt(0); System.out.print("Enter Employee Number: "); en = in.nextInt(); System.out.print("Full Time? (y/n): "); f = in.next().charAt(0); if(f == 'n' || f == 'N') { ft = false; } if(subInput1 == 1) { System.out.print("Enter wage: "); } else if(subInput1 == 2) { System.out.print("Enter salary: "); } else { System.out.print("Enter rate: "); } amount = in.nextDouble(); em.addEmployee(subInput1, fn, ln , mi, g, en, ft, amount); break; //Remove Employee case 3: System.out.print("Enter Employee Number to Remove: "); en = in.nextInt(); index = em.getIndex(en); if(index != -1) { em.removeEmployee(index); } break; //Calculate Weekly Payout case 4: System.out.printf("Total weekly payout is %.2f ", em.calculatePayout()); break; //Calculate Bonus case 5: amount = em.holidayBonuses(); System.out.printf("Total holiday bonus payout is %.2f ", amount); break; //Apply Annual Raises case 6: em.annualRaises(); System.out.println("Annual Raises applied."); break; //Reset the weeks values case 7: em.resetWeek(); System.out.println("Weekly values reset."); break; //Exit case 8: System.out.println(" Thank you for using the Employee Manager! "); } }while(mainInput != 8); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started