How to increment salary by 2%, 5%, etc. Using java
Categories: Java 9 Java 8(JDK1.8) Java Java Examples
package r4r.co.in;
import java.util.List;
import java.util.stream.Collectors;
public class IncrementSalary {
public static void main(String[] args) {
// How to increment salary by 2%, 5% etc using java - Published
List<EmployeeSalary> list = List.of(new EmployeeSalary(1, "Rajesh", 32, "JUNIOR", 1100),
new EmployeeSalary(2, "Bimal", 30, "JUNIOR", 2100),
new EmployeeSalary(3, "Brijesh", 35, "JUNIOR", 1500),
new EmployeeSalary(4, "Brijesh", 35, "JUNIOR", 1600));
System.out.println("Input List" + list);
//Salary increase by 2
System.out.println("Salary increase by 2 List");
List<EmployeeSalary> employeeSalaries=list.stream().map(f->new EmployeeSalary(f.getId(), f.getName(), f.getAge(), f.getType(), f.getSalary()+(f.getSalary()*.02))).collect(Collectors.toList());
System.out.println(employeeSalaries);
// Salary increase by 5
System.out.println("Salary increase by 5 List");
List<EmployeeSalary> employeeSalaries1=list.stream().map(f->new EmployeeSalary(f.getId(), f.getName(), f.getAge(), f.getType(), f.getSalary()+(f.getSalary()*.05))).collect(Collectors.toList());
System.out.println(employeeSalaries1);
}
}
class EmployeeSalary {
int id;
String name;
int age;
String type;
double salary;
public EmployeeSalary(int id, String name, int age, String type, double salary) {
this.id = id;
this.name = name;
this.age = age;
this.type = type;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", type=" + type + ", age=" + age + ", salary=" + salary + "]";
}
}
Output:-
Input List[Employee [id=1, name=Rajesh, type=JUNIOR, age=32, salary=1100.0], Employee [id=2, name=Bimal, type=JUNIOR, age=30, salary=2100.0], Employee [id=3, name=Brijesh, type=JUNIOR, age=35, salary=1500.0], Employee [id=4, name=Brijesh, type=JUNIOR, age=35, salary=1600.0]]
Salary increase by 2 List
[Employee [id=1, name=Rajesh, type=JUNIOR, age=32, salary=1122.0], Employee [id=2, name=Bimal, type=JUNIOR, age=30, salary=2142.0], Employee [id=3, name=Brijesh, type=JUNIOR, age=35, salary=1530.0], Employee [id=4, name=Brijesh, type=JUNIOR, age=35, salary=1632.0]]
Salary increase by 5 List
[Employee [id=1, name=Rakesh Kumar, type=JUNIOR, age=32, salary=1155.0], Employee [id=2, name=Rajesh Kumar, type=JUNIOR, age=30, salary=2205.0], Employee [id=3, name=Bimal Kumar Patel, type=JUNIOR, age=25, salary=1575.0], Employee [id=4, name=Brijesh Kumar Patel, type=JUNIOR, age=35, salary=1680.0]]