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:
Use several arrays to record the details of positions and the annual leave entitlement.
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:
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:
(b)Write a test program to exercise the class written in part (a) above. The test program should perform the following:
Name Position Code Current Leave Balance Allowed to Exhange
Dawn Ho J 14 False
Alan Tan S 21 True
Table Q4(g)