Answer:
The program that allows the user to enter set of employees in stack and calculate total salary of all employee is given below.
Step-by-step explanation:
Below is an example implementation in Java:
import java.util.Scan ner;
class Employee {
private String id;
private String name;
private double salary;
// Constructor
public Employee(String id, String name, double salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
// Accessors and Mutators
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
// toString method
Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", salary=" + salary + "]";
}
}
class Stack {
private Employee[] stackArray;
private int top;
private int maxSize;
// Constructor
public Stack(int size) {
maxSize = size;
stackArray = new Employee[maxSize];
top = -1;
}
// Method to push an employee onto the stack
public void push(Employee employee) {
if (top < maxSize - 1) {
stackArray[++top] = employee;
System.out.println("Employee added to the stack.");
} else {
System.out.println("Stack is full. Cannot add more employees.");
}
}
// Method to pop an employee from the stack
public Employee pop() {
if (top >= 0) {
System.out.println("Employee removed from the stack.");
return stackArray[top--];
} else {
System.out.println("Stack is empty. Cannot pop an employee.");
return null;
}
}
// Method to calculate total salary of all employees in the stack
public double calculateTotalSalary() {
double totalSalary = 0;
for (int i = 0; i <= top; i++) {
totalSalary += stackArray[i].getSalary();
}
return totalSalary;
}
}
public class EmployeeStackDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the stack: ");
int stackSize = scanner.nextInt();
Stack employeeStack = new Stack(stackSize);
// Input employees into the stack
for (int i = 0; i < stackSize; i++) {
System.out.println("Enter details for Employee " + (i + 1) + ":");
System.out.print("ID: ");
String id = scanner.next();
System.out.print("Name: ");
String name = scanner.next();
System.out.print("Salary: ");
double salary = scanner.nextDouble();
Employee employee = new Employee(id, name, salary);
employeeStack.push(employee);
}
// Calculate total salary
double totalSalary = employeeStack.calculateTotalSalary();
System.out.println("Total Salary of all employees: $" + totalSalary);
scanner.close();
}
}
This program defines an Employee class and a Stack class.
It then allows the user to input details for a set of employees, stores them in a stack, and calculates the total salary of all employees in the stack.
Thus,
The program that allows the user to enter set of employees in stack and calculate total salary of all employee is given above.