Employee Management with MyArrayList
Employee Management with MyArrayList
The main class orchestrates the collection of user input to create Employee objects and manages them through a MyArrayList instance (empList). It also performs tasks such as filtering based on salary, calculating totals and averages, and identifying salary extremes, all while utilizing the MyArrayList functionality to display this data effectively .
The program uses the display method from the MyArrayList class to iterate over the empList and empHigh lists, printing out string representations of each employee, which include their name, ID, and salary, using the toString method of the Employee class .
The program iterates over all Employee objects in the empList, checking if their salary exceeds 5000. Each time an employee meets this criterion, they are inserted into the empHigh list using the insertAtBack method of the MyArrayList class .
If an attempt is made to access an index that is less than 0 or greater than or equal to the current size of the list, the get method in the MyArrayList class throws an ArrayIndexOutOfBoundsException, effectively preventing out-of-bounds access .
The average salary is calculated by summing all the salaries from the Employee objects stored in the empList and then dividing this total by the number of employees present in the list. This is expressed by the formula avg = total / empList.size().
The program differentiates these calculations through separate for-loops: the total salary is computed by summing each Employee's salary, maximum salary is found by comparing each to the current max and updating as needed, and minimum salary is similarly calculated by updating the current min. These are performed after all Employee objects have been instantiated and inserted into empList .
The MyArrayList class implements a check in its insertAtFront and insertAtBack methods to determine if the list is already full. If the current size is equal to or greater than the capacity, attempting to insert an element will not proceed, and the message 'Cannot insert in a full list.' is printed .
The mainEmployee class uses a Scanner object to accept user input from the console. For each Employee, it prompts the user to enter a name, ID, and salary, which are then parsed and used to instantiate a new Employee object. This object is subsequently added to the empList via the insertAtBack method .
The Employee class encapsulates its data using private fields for employee name, ID, and salary. These fields can be accessed through public accessor methods (getName, getID, and getSalary) and modified using mutator methods (setEmpName, setID, setSalary). This provides controlled access and modification of the class's private data .
The program initializes both max and min values to the salary of the first employee. It then iterates through the remaining Employee objects in the empList, updating max if it encounters a salary greater than the current max and updating min if it encounters a salary less than the current min .