QuestionQuestion

Transcribed TextTranscribed Text

Question 3 I 35 marks ] 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 staff sick leave management system for staff having 14 days of non hospitalisation leave and 60 days of hospitalisation leave per year, after a 6-month probation period. Refer to Figure Q3 for an explanation on the limit on the number of sick leave, reproduced from the webpage http://www.mom.gov.sg/employment-practices/leave/sick-leave/eligibility-and-entitlement. Limits on paid sick leave days The amount of paid outpatient and hospitalisation sick leave you can take is capped at your sick leave entitlement. Example If you have already taken 14 days of outpatient sick leave in a year, the number of days of hospitalisation sick leave that you can take is 46 days (60 14 46). Figure Q3 Use several arrays to record the details of staff: an array empNos to record employee number of staff whose sick leave is being managed, an array hospitalisation to record current hospitalisation leave balance an array nonHospitalisation to record the current non hospitalisation leave balance Write a complete Java program with the following methods: (a) A method search to locate the staff in the array given the employee number (a string). The method accepts the employee number to be searched, the array empNos and an integer count which is the count of employee numbers already recorded in the array. If the employee number exists in empNos, the method returns the array index where the employee number is located. Otherwise, the method returns -1. (2 marks) (b) A method addStaff to record details of a staff. The method accepts all the arrays and an integer count which is the number of staff data recorded in the arrays. The method first checks whether the arrays are already full. If so, the method prints the error message: Capacity Limit. No more data can be added! Otherwise, the method prompts for an employee number and then checks whether the provided employee number already exists. If employee number exists, the method prints the error message: Xxx: Existing employee number where Xxx is the provided employee number. Otherwise, if the employee number does not exist, the method adds the employee number to the array empNos, sets the non hospitalisation and hospitalisation leave of the staff to 14 and 60 respectively, increments count as one more staff is added and prints the message: Xxx added: Non hospitalisation: 14 days Hospitalisation: 60 days where Xxx is the provided employee number. This method returns the value of count. (5 marks) (c) A method removeStaff to remove the staff given the staff employee number. The method accepts the arrays for the staff details and an integer count which is the number of staff data recorded in the arrays. If the method finds that there is no staff data stored in the array, the method prints the error message: No data for remove! Otherwise, the method prompts for an employee number and then checks whether provided employee number exists. If employee number does not exist, the method prints the error message: Xxx: Not existing employee number where Xxx is the provided employee number. If the employee number exists, the method removes the staff data for the staff with the provided employee number by shifting the data of other staff to pack the array and then at the position where the last valid staff data was, setting the employee number to null and the both non hospitalisation and hospitalisation of sick leave to 0 so that only array positions 0 to count- store existing staff data. The method then decrements count and prints the message: Xxx: Successfully removed where Xxx is the provided employee number. This method returns the value of count. (5 marks) (d) A method takeSickLeave to allow a staff to take leave. The method accepts the arrays for the staff details and an integer count which is the number of staff data recorded in the arrays. If the method finds that there is no staff data stored in the array, the method prints the error message: No data for take leave! Otherwise, the method prompts for the employee number of the staff taking leave, and then checks whether provided employee number exists. If employee number does not exist, the method prints the error message: Xxx: Not existing employee number where Xxx is the provided employee number. If the input employee number belongs to a staff, the method prints the number of days of sick leave left by type: Xxx: Non hospitalisation: yy days Hospitalisation: ZZ days where Xxx is the provided employee number, yy is the number of days of non hospitalisation leave left and ZZ is the number of days of hospitalisation leave the staff has currently. If the staff has no more sick leave, the method prints the message: Xxx: No more sick leave. Please see HR. where Xxx is the provided employee number. If the staff has sick leave only hospitalisation leave is left The method prints this message: Xxx: Can take only hospitalisation sick leave both non hospitalisation and hospitalisation sick leave is left the method prompts for the type of sick leave to take (N for non hospitalisation and H for hospitalisation). The method prompts for the number of days of sick leave to take. The user may enter -1 at this point to cancel the take sick leave operation in which case, the method prints the message: Take leave terminated. If there is insufficient leave, the method prints the error message: Xxx: ZZ leave: y1 but requires y2 days. Please see HR. where Xxx is the provided employee number, y1 is the number of days of sick leave of type ZZ left and y2 is the required (input) days for that type of sick leave. If there is sufficient sick leave of the requested type, the method deducts the number of days of sick leave according to Figure 3: - if non hospitalisation is requested, deduct the sick leave from both the non hospitalisation leave and the hospitalisation leave and - if hospitalisation leave is requested, deduct the sick leave from hospitalisation leave only. If the remaining hospitalisation leave is fewer than non hospitalisation leave, then set the non hospitalisation leave to be the same value as hospitalisation leave. The method then prints the leave balance: Xxx: Non hospitalisation: yy days Hospitalisation: ZZ days where Xxx is the provided employee number, yy is the number of days of non hospitalisation leave left and ZZ is the number of days of hospitalisation leave left, both after deduction of the sick leave. (9 marks) (e) A method listSickLeave to display leave balance of staff. The method accepts the necessary arrays, and an integer count which is the number of staff data recorded in the arrays. The method prints the a list of employee numbers together with sick leave details balance of each type, and adds at the end of a staff detail for staff who has consumed at least 14 days of sick leave. The list has a header and a trailer. (3 mark) (f) The method displayMenu which displays this menu: Menu 1. Add Staff 2. Remove Staff 3. Take Sick Leave 4. Staff Sick Leave Details 0. Exit (1 mark) (g) The method main which does the following actions: Declare and create arrays empNos, nonHospitalisation and hospitalisation. For testing purposes, let the capacity of the arrays empNos, nonHospitalisation and hospitalisatic be 3. Declare and initialise count to zero as there is no valid staff data recorded in the arrays yet, 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. (3 marks) An example run is shown here: Menu Menu 1. Add Staff 1. Add Staff 2. Remove Staff 2. Remove Staff 3. Take Sick Leave 3. Take Sick Leave 4. Staff Sick Leave Details 4. Staff Sick Leave Details 0. Exit o. Exit Enter option: 2 Enter option: 1 No data for remove! Enter employee number to add: P127 P127 added: Non hospitalisation: 14 days Hospitalisation: 60 days Menu Menu 1. Add Staff 1. Add Staff 2. Remove Staff 2. Remove Staff 3. Take Sick Leave 3. Take Sick Leave 4. Staff Sick Leave Details 4. Staff Sick Leave Details 0. Exit 0. Exit Enter option: 3 Enter option: 1 No data for take leave! Capacity LAMAE, No more data can be added! Menu Menu 1. Add Staff 1. Add Staff 2. Remove Staff 2. Remove Staff 3. Take Sick Leave 3. Take Sick Leave 4. Staff Sick Leave Details 4. Staff Sick Leave Details 0. Exit Enter option: 4 0. Exit Emp No Non Hospitalisation Hospitalisation Enter option: 4 P123 14 60 Emp No Non Hospitelisation Rospitalisation P125 14 60 End of List P127 14 60 Menu End of List 1. Add Staff 2. Remove Staff Menu 3. Take Sick Leave 1. Add Staff 4. Staff Sick Leave Details 2. Remove Staff 0. Exit 3. Take Sick Leave Enter option: 1 4. Staff Sick Leave Details Enter employee number to add: P123 0. Exit P123 added: Non bompitalisation: 14 days Hospitalisation: 60 days Enter option: 3 Enter employee number of staff taking leave: P122 Menu P122: Not existing employee number 1. Add Staff 2. Remove Staff Menu 3. Take Sick Leave 1. Add Staff 4. Staff Sick Leave Details 2. Remove Staff 3. Take Sick Leave 0. Exit f. Staff Sick Leave Details Enter option: 1 0. Exit Enter employee number to add: P125 P125 added: Non hospitalisation: 14 days Hospitalisation: 60 days Enter option: 3 Enter employee number of staff taking leave: P125 Menu P125: Non hospitalization: 14 days Hospitalisation: 60 days Enter type of leave N (non hompitalization) - M (hompitalization) N 1. Add Staff 2. Remove Staff Enter number of days of sick leave or -1 to cancel take leave: 15 3. Take Sick Leave Non Hospitalisation leave: 14 but requires 15 days. Please see HR. 4. Staff Sick Leave Details Menu 0. Exit 1. Add Staff Enter option: 1 2. Remove Staff Enter employee number to add: P123 T.kake Sick Leave P123: Existing employee number! 4. Staff Sick Leave Details 0. Exit Enter option: 3 Enter employee number of staff taking leave: P125 P125: Non hompitalisation: 14 days Hospitalisation: 60 days Enter type of leave N (non hospitalization) - B (hospitalisation) N Enter number of days of sick leave or -2 to cancel take leave: 10 P125: Non hospitalisation: 4 days Hospitalisstion: 50 days Menu Menu 1. Add Staff 1. Add Staff 2. Remove Staff 2. Remove Staff 3. Take Sick Leave 3. Take Sick Leave 4. Staff Sick Leave Details 4. Staff Sick Leave Details 0. Exit 0. Exit Enter option: 3 Enter option: 2 Enter employee number of staff taking leave: P125 Enter employee number to remove: P122 P125: Non hospitalisation: 4 days Hospitalisation: 50 days P122: Not existing employee number Enter type of leave N (non hospitalisation) H H Enter number of days of sick leave or -1 to cancel take leave: 47 Menu P125: Non hospitalisation: 3 days Nospitalisation: 3 days 1. Add Staff 2. Remove Staff Menu 3. Take Sick Leave 1. Add Staff 4. Staff Sick Leave Details 2. Remove Staff 0. Exit 3. Take Sick Leave 4. Seaff Sick Leave Details Enter option: 2 0. Exit Enter employee number to remove: P123 Enter option: 3 P123: Successfully removed Enter employee number of staff taking leave: P125 P125: Non hompitalization: 3 days Hompitalization: 3 days Menu Enter type of leave N (non hospitalization) - H N 1. Add Staff Inter number of daya of sick leave or -1 to cancel take leave: 3 2. Remove Staff P12S: Non hospitalisation: 0 day Hospitalisation: 0 day 3. Take Sick Leave 4. Staff Sick Leave Details Menu 0. Exit 1. Add Staff Enter option: 4 2. Remove Staff Imp No Non Hospitalisation Mospitalisation 3. Take Sick Leave P125 0 *** 4. Staff Sick Leave Details P127 14 60 0. Exit End of List Enter option: 3 Menu Enter employee number of staff taking leave: P125 1. Add Staff P125: Non hospitalisation: 0 day o day 2. Remove Staff P125: No more sick leave. Please see HR. 3. Take Sick Leave Menu 4. Staff Sick Leave Details 1. Add Staff 0. Exit 2. Remove Staff Enter option: 1 3. Take Sick Leave Enter employee number to add: P272 4. Staff Sick Leave Details P272 added: Non hospitalisation: 14 days Hospitalisation: 60 days 0. Exit Enter option: 3 Menu Enter employee number of staff taking leave: P123 1. Add Staff P123: Non bospitalisation: 14 days Hospitalisation: 60 days 2. Remove Staff Enter type of leave N (non hospitalization) - H (hospitalization) N 3. Take Sick Leave Enter number of days of sick leave or -1 to cancel take leave: 14 4. Staff Sick Leave Details P123: Non hospitalisation: 0 day Hospitalisation: 46 days 0. Exit Enter option: 4 Menu Emp No Non Hospitalisation Hospitalisation 1. Add Staff P125 0 *** 2. Remove Staff P127 14 60 3. Take Sick Leave P272 14 60 4. Staff Sick Leave Details End of List 0. Exit Enter option: 3 Menu Enter employee number of staff taking leave: P123 1. Add Staff P123: Non hospitalization: 14 days Rospitalisation: 60 days 2. Remove Staff Enter type of leave - N (non hompitalization) - H (hospitalization) : N Enter number of days of sick leave or -1 to cancel take leave: 14 3. Take Sick Leave P123: Non hospitalisation: o day Mospitalization: 46 days 4. Staff Sick Leave Details 0. Exit Menu Enter option: 0 1. Add Staff Closing application 2. Remove Staff 3. Take Sick Leave 4. Staff Sick Leave Details 0. Exit Enter option: 3 Enter employee number of staff taking leave: P123 P123: Nom hompitalization: o day 46 daya P123: Can take only hompitalization sick leave Enter number of days of sick leave or -1 to cancel take leave: 15 P123: Non hompitelization: 0 day Hospitalisation: 31 daya Submit the program listing together with a screenshot showing the output. In your run, you should demonstrate that each of the options is working. Include your name and student number in your program output at the start and completion of processing an option.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.util.Scanner;

/**
*
* @author
*/
public class Question3 {

    private static Scanner keyboard = new Scanner(System.in);

    public static void main(String[] args) {
       /**
         * Declare and create arrays empNos, nonHospitalisation and
         * hospitalisation. For testing purposes, let the capacity of the arrays
         * empNos, nonHospitalisation and hospitalisation be 3.
         */
       int capacity = 3;
       String[] empNos = new String[capacity];
       int[] nonHospitalisation = new int[capacity];
       int[] hospitalisation = new int[capacity];

       /**
         * Declare and initialise count to zero as there is no valid staff data
         * recorded in the arrays yet,
         */
       int count = 0;

       /**
         * 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.
         */
       int choice;
       do {
            displayMenu();
            choice = keyboard.nextInt();
            switch (choice) {
                case 1:
                   count = addStaff(count, empNos, nonHospitalisation,
                            hospitalisation);
                   break;
                case 2:
                   count = removeStaff(count, empNos, nonHospitalisation,
                            hospitalisation);
                   break;
                case 3:
                   takeSickLeave(count, empNos, nonHospitalisation,
                            hospitalisation);
                   break;
                case 4:
                   listSickLeave(count, empNos, nonHospitalisation,
                            hospitalisation);
                   break;
            }
            System.out.println("");
       } while (choice != 0);

    }

    private static void listSickLeave(int count,
            String[] empNos, int[] nonHospitalisation,
            int[] hospitalisation) {
       System.out.printf("%-6s%26s%22s\n", "Emp No",
                "Non Hospitalisation", "Hospitalisation");
       for (int i = 0; i < count; i++) {
            System.out.printf("%-6s%16d%24d", empNos[i],
                   nonHospitalisation[i], hospitalisation[i]);
            if (60 - hospitalisation[i] >= 14) {
                System.out.print("***");
            }
            System.out.println("");
       }
       System.out.println("End of List");
    }

    /**
    * A method takeSickLeave to allow a staff to take leave.
    *
    * The method accepts the arrays for the staff details and an integer count
    * which is the number of staff data recorded in the arrays.
    *
    * @param count
    * @param empNos
    * @param nonHospitalisation
    * @param hospitalisation
    */
    private static void takeSickLeave(int count,
            String[] empNos, int[] nonHospitalisation,
            int[] hospitalisation) {
       /**
         * If the method finds that there is no staff data stored in the array,
         * the method prints the error message: No data for take leave!
         */
       if (count <= 0) {
            System.out.println("No data for take leave!");
       } else {
            /**
             * Otherwise, the method prompts for the employee number of the
             * staff taking leave, and then checks whether provided employee
             * number exists. If employee number does not exist, the method
             * prints the error message: Xxx: Not existing employee number where
             * Xxx is the provided employee number.
             */
            System.out.print("Enter employee number to remove: ");
            String number = keyboard.next();
            int index = search(number, empNos, count);
            if (index == -1) {
                System.out.println(number + ": Not existing employee number");
            } else {...

By purchasing this solution you'll be able to access the following files:
Solution.java.

$30.00
for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats