0% found this document useful (0 votes)
192 views9 pages

Employee Management with MyArrayList

This document contains code for an Employee class that stores employee name, ID, and salary. It also contains a MyArrayList class that implements basic array list functionality like insertion, size checking, and retrieval. The main class prompts the user for employee data, stores employees in lists based on salary, calculates total salary, average salary, minimum salary, and maximum salary, and displays the results.

Uploaded by

SushyYaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
192 views9 pages

Employee Management with MyArrayList

This document contains code for an Employee class that stores employee name, ID, and salary. It also contains a MyArrayList class that implements basic array list functionality like insertion, size checking, and retrieval. The main class prompts the user for employee data, stores employees in lists based on salary, calculates total salary, average salary, minimum salary, and maximum salary, and displays the results.

Uploaded by

SushyYaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

/*

Name : NUR DIYANA BINTI AB AZIZ

Sid# : 2020844414

Course: CSC508

Group#: CS2304C

Assign#: #1

Due Date: 23 April 2021 (Friday before 12 AM)

Program Description: Lab Assignment #1 ( Arraylist)

*/

EMPLOYEE CLASS

public class Employee{

public String empName;

public String id;

public double salary;

public Employee(){}

public Employee(String name, String id, double salary)

[Link]= name;

[Link] = id;

[Link] = salary;

//accessor

public String getName()

return empName;

}
public String getID()

return id;

public double getSalary()

return salary;

//mutator

public void setEmpName(String name)

[Link] = name;

public void setID(String id)

[Link] = id;

public void setSalary(double salary)

[Link] = salary;

public String toString()

return "Name: "+ empName + " ID: "+id + " Salary(RM): " +salary;

}
ARRAYLIST CLASS

public class MyArrayList

// default initial capacity

private static final int INITIAL_CAPACITY = 50;

private Object[] theData; // the array to hold the list elements

private int size = 0; // the current size

private int capacity = 0; // the current capacity

//Default constructor

public MyArrayList()

theData = new Object[INITIAL_CAPACITY];

capacity = INITIAL_CAPACITY;

public boolean isEmpty() {

return size == 0;

//exercise 3

//method isFull()

public boolean isFull()

if(size >= capacity)

return true;

else return false;

//Return the number of elements in this list

public int size() {


return size;

//insert front

public void insertAtFront(Object theValue)

if(size >= capacity) //the list is full

[Link]("Cannot insert in a full list.");

else

for (int i = size; i > 0; i--)

theData[i] = theData[i-1]; // move elements down

theData[0] = theValue; //insert the item at front

size++; //increment the size

} //end add

//insert at back

public void insertAtBack(Object theValue)

if(size >= capacity) //the list is full

[Link]("Cannot insert in a full list.");

else

theData[size] = theValue; //insert the item at front

size++; //increment the size

}
//GET OBJECT DATA

public Object get(int index)

if(index < 0 || index >= size)

throw new ArrayIndexOutOfBoundsException(index);

else

return theData[index];

} //end get

// display the elements of the list

public void display()

for ( int i = 0; i < size; i++)

[Link](theData[i]);

[Link]();

}
MAIN CLASS

import [Link];

public class mainEmployee{

public static void main(String[] args)

MyArrayList empList = new MyArrayList();

MyArrayList empHigh = new MyArrayList();

double avg=0,total=0;

Employee [] emp = new Employee[5];

//input data

for(int i =0; i<[Link];i++){

Scanner sc = new Scanner([Link]);

[Link]("Enter Your Name:");

String name =[Link]();

[Link]("Enter Your ID:");

String id =[Link]();

[Link]("Enter Your Salary:");

double salary =[Link]([Link]());

emp[i] = new Employee(name,id,salary);

[Link](emp[i]);

//Employee that has salary more than 5000 will store in empHigh list

for(int j =0; j<[Link]();j++)

if(emp[j].getSalary()>5000)

[Link]([Link](j));

}
for(int t =0; t<[Link]();t++)

//total salary

total += emp[t].getSalary();

double max=emp[0].getSalary(),min=emp[0].getSalary();

for(int m=1;m<[Link]();m++)

if(emp[m].getSalary()>max)

max = emp[m].getSalary();

if(emp[m].getSalary()<min)

min = emp[m].getSalary();

//[Link] average salary

avg = total/[Link]();

//display all employee detail

[Link]("Detail in empList:");

[Link]();

//display total salary

[Link]("Detail in empHigh:");

[Link]();

[Link]("Total salary(RM):"+total);

[Link]("Average salary of "+[Link]()+" workers (RM):"+avg);

[Link]("Minimum salary of (RM):"+min);

[Link]("Maximum salary of (RM):"+max);

}
OUTPUT

Common questions

Powered by AI

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 .

You might also like