The objectives of this question are

(a)to practice arrays in Java.
(b)to practice methods in Java.

Develop a Java program using the control structures of structured programming on an array data structure for a Java staff leave management system.

Use several arrays to record the details of staff:

  • an array StaffNames to record name of staff whose leave is being managed,
  • an array staffPositionCode to record which type of position they are in (T top management, M- management, S supervisory and J junior level)
  • an array staffNumleaves to record the current leave balance of staff.

Use several arrays to record the details of positions and the annual leave entitlement.

  • an array positionCodeto record 4 types of positions (T top management, M- management, S supervisory and J junior level)
  • an array numleaves to record the annual leave entitlement according to Table Q3.
Position Code Leave (days)
T 52
M 28
S 21
J 14
Table Q3

Write a complete Java program with the following methods:

(a)A method search to locate the staff in the array given the staff name.

The method accepts the name of the staff to be searched. In addition, it accepts the StaffNames array and an integer count which is the number of staff already recorded in the array.

If the name exists in StaffNames, the method returns the array index where the name is located. Otherwise, the method returns -1.

(b)A method addStaff to record details of a staff.

The method accepts the name and positionCode of the staff to add. In addition, it accepts all the arrays and an integer count which is the number of staff already recorded in the arrays for staff details.

The method checks whether provided name already exists. If name exists, the method prints the error message: Xxx is already a staff! where Xxx is the provided name.

If name does not exist, the method records name and positionCode in the arrays StaffNames and staffPositionCode respectively, set the leave of the staff using both the positionCode and numLeaves arrays, increments count as one more staff is added and prints the message: Successfully added Xxx with yy days of leaves! where Xxx is the provided name and yy is the number of days of leave the staff has, based on his position code.

This method returns the value of count.

(c)A method removeStaff to remove the staff given the staff name.

The method accepts the name of the staff to be removed. In addition, it accepts the arrays for the staff details and an integer count which is the number of staff already recorded in these arrays.

The method checks whether provided name exists. If name does not exist, the method prints the error message: Cant remove Xxx: Reason: Not a staff where Xxx is the provided name.

If name exists, the method removes the staff by shifting the details of other staff to pack the array, so that only array positions 0 to count-2 store existing staff details. The method then prints the message: Successfully removed: Xxx where Xxx is the provided name.

This method returns true if the remove is successful and false otherwise.

(d)A method takeLeave to allow a staff to take leave.

The method accepts the name of the staff taking leave, the number of days of leave to take (numDays), the arrays staffNames and staffNumleaves, and an integer count which is the number of staff already recorded in the arrays.

The method checks whether provided name exists. If name does not exist, the method prints the error message: Cant apply leave Xxx Reason: Not a staff where Xxx is the provided name. The method then returns an integer which is the largest number in the Integer class.

If name exists, the method checks whether the staff has sufficient number of leave. If there is insufficient leave, the method prints the error message: Cant apply leave Xxx Reason: Insufficient leave where Xxx is the provided name. The method then returns a negative number which is the number of days exceeded had the leave been taken.

If there is sufficient leave, the method updates the number of days of leave left for the staff and prints the message: Successful leave application for Xxx where Xxx is the provided name. The method then returns a number which is the number of days of leave left after the leave is taken.

(e)A method listStaffWithLeave to display staff with non-zero days of leave. The method accepts the necessary arrays staffNames and staffNumleaves, and an integer count which is the number of staff already recorded in the arrays.

The method displays the header List of Staff who can Take Leave followed by 0 or more staff detail lines (name and days of leave left) and then the trailer End of List.

A sample output is shown below:

List of Staff who can Take Leave Dawn Ho 14 daysAlan Tan 21 days End of List
List of Staff who can Take Leave
Dawn Ho 14 days
Alan Tan 21 days
End of List

(f)The method displayMenu which displays this menu:

Menu
1. Add Staff
2. Remove Staff
3. Take Leave
4. List Staff Leave Details
0. Exit

(g)The method main which does the following actions:

  • Declare and create the arrays StaffNames, staffPositionCode, staffNumleaves, positionCode and numleaves.
    Fortestingpurposes,letStaffNames,staffPositionCodeand staffNumleaves record a maximum of 2 staff data each.
    Initialise positionCode and numleaves according to Table Q(3).
  • Declare and initialise count, the number of data already recorded in the staff detail arrays to zero,
  • Repeatedly display a menu to allow the user to select an option and call the appropriate method to perform the task according to the selected option until the user chooses to exit the program.
    • For option 1, the method checks whether the count of data is already the maximum number of data that can be stored in the staff detail arrays. If so, print the error message: No more staff can be added. Otherwise, the method prompts and reads the name and position code of the staff to add.
    • For option 2, the method prompts and reads the name of the staff to be removed. If the remove is successful, decrement count.
    • For option 3, the method prompts and reads the name of the staff and the number of days of leave to take.
      If the leave is successfully taken, print the message: Xxx has yy day(s) of leave left where Xxx is the provided name and yy is the number of days of leave the staff has left.
      If the leave is not successfully taken, print two messages:
      -Applying yy days will result in -zz days leave! where yy is the number of leave the staff wishes to take and zz is the number of days exceeded had the leave been taken
      -Xxx can apply only yy day(s) of leave where Xxx is the provided name and yy is the number of days of leave the staff has, and so can take.

An example run is shown here with user input underlined for readability:

Menu
1. Add Staff
2. Remove Staff
3. Take Leave
4. List Staff Leave Details
0. Exit
Enter option: 1
Enter name of staff to add: Dawn Ho
Enter position code of staff to add (T, M, S or J): J
Successfully added Dawn Ho with 14 days of leave

Menu
1. Add Staff
2. Remove Staff
3. Take Leave
4. List Staff Leave Details
0. Exit
Enter option: 1
Enter name of staff to add: Alan Tan
Enter position code of staff to add (T, M, S or J): S
Successfully added Alan Tan with 21 days of leave

Menu
1. Add Staff
2. Remove Staff
3. Take Leave
4. List Staff Leave Details
0. Exit
Enter option: 4
List of Staff who can Take Leave Dawn Ho 14 days
Alan Tan 21 days End of List

Menu
1. Add Staff
2. Remove Staff
3. Take Leave
4. List Staff Leave Details
0. Exit
Enter option: 2
Enter name of staff to remove: Peter
Can't remove Peter: Reason: Not a staff

Menu
1. Add Staff
2. Remove Staff
3. Take Leave
4. List Staff Leave Details
0. Exit
Enter option: 2
Enter name of staff to remove: Dawn Ho Successfully removed: Dawn Ho

Menu
1. Add Staff
2. Remove Staff
3. Take Leave
4. List Staff Leave Details
0. Exit
Enter option: 4
List of Staff who can Take Leave
Alan Tan 21 days
End of List

Menu
1. Add Staff
2. Remove Staff
3. Take Leave
4. List Staff Leave Details
0. Exit
Enter option: 3
Enter name of staff taking leave: Alan Tan
Enter number of days to apply leave for: 10
Successful leave application for Alan Tan
Alan Tan has 11 day(s) of leave left

Menu
1. Add Staff
2. Remove Staff
3. Take Leave
4. List Staff Leave Details
0. Exit
Enter option: 3
Enter name of staff taking leave: Alan Tan
Enter number of days to apply leave for: 14
Can't apply leave Alan Tan: Reason: Insufficient leave
Applying 14 days will result in -3 days leave!
Alan Tan can apply only 11 day(s) of leave

Menu
1. Add Staff
2. Remove Staff
3. Take Leave
4. List Staff Leave Details
0. Exit
Enter option: 3
Enter name of staff taking leave: Alan Tan
Enter number of days to apply leave for: 11
Successful leave application for Alan Tan
Alan Tan has 0 day(s) of leave left

Menu
1. Add Staff
2. Remove Staff
3. Take Leave
4. List Staff Leave Details
0. Exit
Enter option: 4
List of Staff who can Take Leave
End of List

Menu
1. Add Staff
2. Remove Staff
3. Take Leave
4. List Staff Leave Details
0. Exit
Enter option: 0
Closing application

The objectives of this question are

(a)to write a basic Java class
(b)to write a simple tester to test your Java class

You have been engaged to develop a prototype using the object-oriented programming approach for the leave application and to implement classes for this object-oriented application.

(a)Write programs using object oriented programming approach to implement a Java class to model a Staff. This class has the following:

  • Suitable instance variables for
    • Name of staff
    • Position code (T top management, M- management, S supervisory and J junior level)
    • Current leave balance
    • Whether the staff is allowed to exchange leave for money
  • Two constructors, one with the input values for all the instance variables and the other with values for all the instance variables except for: whether the staff is allowed to exchange leave for money (set to set to the default value false) and position code is set to J. Avoid repeating assignment statements by making one of the constructors invoke the other constructor.
  • Get and set methods for current leave balance and the position code.
  • The get method for whether the staff is allowed to exchange leave for money.
  • A changeIsAllowedMoneyExchange() method that does not have any parameter and does not return any value. This method changes a staff who is not allowed to exchange leave to money to allowed, and changes a staff who is allowed to exchange leave to money to not allowed.
  • A hasLeave() method which returns true if the current leave balance is more than 0. Returns false otherwise.
  • A computeLeaveToMoney(double) method which returns amount of money the staff would receive should his current leave balance be converted to money. The method returns the amount by computing the product of the current leave balance and the input parameter which is the rate per day, only if the staff is allowed to make the exchange. Otherwise, the method returns -1.
  • A compareTo() method that takes as parameter another staff, compares the current leave balance of this staff and the parameter, and returns 1 of this staff has more days of leave, 0 if their current leave balances are the same, and -1 if this staff has fewer days of leave.
  • A toString() method that returns with descriptions, the values of the instance variables and the amount of money when current leave balance is converted to money, if the staff is allowed to exchange leave for money. Fix the rate per day to 112.50.

(b)Write a test program to exercise the class written in part (a) above. The test program should perform the following:

  • Create the following 2 staff using the appropriate constructors, hold the objects in a single suitable data structure.
Name Position Code Current Leave Balance Allowed to Exhange
Dawn Ho J 14 False
Alan Tan S 21 True
Table Q4(g)

  • Apply the changeIsAllowedMoneyExchange () method to the first staff.
  • Take away 21 days of leave from the second staff.
  • Use a loop to print details of staff only if they have leave.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.