Skip to content

Instantly share code, notes, and snippets.

@Yuhtin
Created September 1, 2022 23:52
Show Gist options
  • Save Yuhtin/d6f85b402448179b5ad59c611a6545b9 to your computer and use it in GitHub Desktop.
Save Yuhtin/d6f85b402448179b5ad59c611a6545b9 to your computer and use it in GitHub Desktop.
Employee Salary System
public class Employee {
private final int id;
private final String name;
private double salary;
public Employee(int id, String name, double salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
public int getId() {
return id;
}
public void increaseSalary(double percentage) {
salary += salary * percentage / 100.0;
}
@Override
public String toString() {
return id + ", " + name + ", " + String.format("%.2f", salary);
}
}
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.function.Predicate;
public class EmployeeApplication {
public static List<Employee> employees = new ArrayList<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
print("Insert the number of workers:");
int workers = scanner.nextInt();
for (int i = 1; i <= workers; i++) {
print("Employee #" + i + ":");
print("Insert the id of the worker:");
int id = scanner.nextInt();
Employee employeeById = findEmployeeById(id);
if (employeeById != null) {
print("The id already exists");
continue;
}
scanner.nextLine();
print("Insert the name of the worker:");
String name = scanner.nextLine();
print("Insert the salary:");
double salary = scanner.nextDouble();
employees.add(new Employee(id, name, salary));
}
print("Insert the id of the worker to increase the salary:");
int id = scanner.nextInt();
Employee employeeById = findEmployeeById(id);
if (employeeById == null) {
print("The id does not exist");
return;
}
print("Insert the percentage to increase the salary:");
double percentage = scanner.nextInt();
employeeById.increaseSalary(percentage);
print("List of employees:");
employees.forEach(System.out::println);
}
public static Employee findEmployeeById(int id) {
return findEmployee(employee -> employee.getId() == id);
}
public static Employee findEmployee(Predicate<Employee> predicate) {
return employees.stream()
.filter(predicate)
.findFirst()
.orElse(null);
}
public static void print(String message) {
System.out.println(message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment