Introduction
Java 8 introduced several features that make it easier to work with collections of data, such as streams and lambda expressions. In a typical application, you might need to calculate an employee’s salary based on various components such as base salary, bonuses, deductions, and other factors. This guide will show how to use Java 8 features to create a program that calculates an employee’s total salary.
Table of Contents
- Problem Statement
- Solution Steps
- Java Program
- Example: Calculating the Salary of an Employee
- Conclusion
Problem Statement
You need to calculate the total salary of an employee, considering components like base salary, bonuses, and deductions. The program should be able to handle different combinations of these components to arrive at the final salary.
Example:
- Problem: Calculate the total salary of an employee based on various components.
- Goal: Use Java 8’s features like streams and lambda expressions to compute the salary efficiently.
Solution Steps
- Create an Employee Class: Define an
Employeeclass with fields such as base salary, bonuses, and deductions. - Calculate the Total Salary: Use Java 8 features to compute the total salary.
- Print the Result: Display the calculated salary of the employee.
Java Program
Example: Calculating the Salary of an Employee
First, define the Employee class with the required fields.
import java.util.Arrays;
import java.util.List;
/**
* Java 8 Program to Calculate Salary of an Employee
* Author: https://www.rameshfadatare.com/
*/
public class EmployeeSalaryExample {
public static void main(String[] args) {
// Define employee salary components
Employee employee = new Employee("Amit", 50000, Arrays.asList(5000.0, 3000.0), Arrays.asList(2000.0, 1500.0));
// Calculate total salary
double totalSalary = employee.calculateTotalSalary();
// Print the calculated salary
System.out.println("Total Salary of " + employee.getName() + ": " + totalSalary);
}
}
class Employee {
private String name;
private double baseSalary;
private List<Double> bonuses;
private List<Double> deductions;
public Employee(String name, double baseSalary, List<Double> bonuses, List<Double> deductions) {
this.name = name;
this.baseSalary = baseSalary;
this.bonuses = bonuses;
this.deductions = deductions;
}
public String getName() {
return name;
}
public double calculateTotalSalary() {
// Calculate total bonuses
double totalBonuses = bonuses.stream()
.mapToDouble(Double::doubleValue)
.sum();
// Calculate total deductions
double totalDeductions = deductions.stream()
.mapToDouble(Double::doubleValue)
.sum();
// Calculate total salary
return baseSalary + totalBonuses - totalDeductions;
}
}
Output
Total Salary of Amit: 55500.0
Explanation
EmployeeClass: This class includes fieldsname,baseSalary,bonuses, anddeductions. ThegetName()method retrieves the name of the employee, while thecalculateTotalSalary()method computes the total salary.calculateTotalSalary()Method: This method uses streams to calculate the sum of bonuses and deductions and then applies them to the base salary to compute the total salary.mapToDouble(Double::doubleValue): This method converts each value in the list of bonuses or deductions to adouble.sum(): This method sums all the values in the stream to calculate the total bonuses and total deductions.
- Final Calculation: The total salary is computed by adding the base salary and total bonuses and then subtracting the total deductions.
Conclusion
Using Java 8’s Stream API and lambda expressions, you can efficiently calculate an employee’s total salary by considering various components such as bonuses and deductions. This approach simplifies the calculation logic and makes the code more readable and maintainable. The flexibility of the Stream API allows you to easily adapt the program to different salary structures or additional components.