Java Program To Print Average and Total Salary of the Organization

1. Introduction

This tutorial demonstrates how to calculate both the average and total salary within an organization using Java. This type of calculation is crucial for financial planning and analysis, helping businesses to manage payroll budgets and understand employee compensation trends.

Key Points

- Use of the Employee class with a salary attribute.

- Utilization of Java Streams for concise and efficient salary computations.

- Printing both the total and average salary for the entire organization.

2. Program Steps

1. Define the Employee class with the necessary attributes.

2. Populate a list of Employee objects.

3. Use Java Streams to compute the total and average salary.

4. Print the results.

3. Code Program

// File: Employee.java
public class Employee {
    private int id;
    private String name;
    private int age;
    private long salary;
    private String gender;
    private String deptName;
    private String city;
    private int yearOfJoining;

    public Employee(int id, String name, int age, long salary, String gender, String deptName, String city, int yearOfJoining) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.salary = salary;
        this.gender = gender;
        this.deptName = deptName;
        this.city = city;
        this.yearOfJoining = yearOfJoining;
    }

    public long getSalary() {
        return salary;
    }
}

// File: SalaryStatistics.java
import java.util.*;

public class SalaryStatistics {
    public static void main(String[] args) {
        List<Employee> employees = Arrays.asList(
            new Employee(1, "Aditi", 30, 100000, "F", "HR", "Mumbai", 1995),
            new Employee(2, "Rahul", 25, 130000, "M", "Engineering", "Bangalore", 2000),
            new Employee(3, "Vishal", 34, 110000, "M", "Engineering", "Mumbai", 1998),
            new Employee(4, "Lakshmi", 28, 150000, "F", "HR", "Bangalore", 1992),
            new Employee(5, "Priya", 24, 90000, "F", "Marketing", "Delhi", 2005)
        );

        long totalSalary = employees.stream()
                                    .mapToLong(Employee::getSalary)
                                    .sum();

        double averageSalary = employees.stream()
                                        .mapToLong(Employee::getSalary)
                                        .average()
                                        .orElse(0);

        System.out.println("Total Salary: " + totalSalary);
        System.out.println("Average Salary: " + averageSalary);
    }
}

Output:

Total Salary: 540000
Average Salary: 108000.0

Explanation:

1. The Employee class is utilized with a salary attribute, which is critical for the computations.

2. SalaryStatistics initializes a list of employees and processes them using Java Streams to compute salary metrics.

3. The mapToLong method maps each employee to their salary, which is then aggregated using sum for the total and average for the mean salary.

4. Both the total and average salaries are printed to give a comprehensive view of the organization's salary distribution.

Comments