0
import java.util.*;

class EmployeesDetails {
    System.out.println("Welcome Aboard!");
    Scanner emp = new Scanner(System.in);
    List<EmployeesDetails> employeesDetails = new ArrayList<>();
    Boolean empQuestion = true;

    while (empQuestion == true) {
        System.out.println("Do you want to enter the employee details? Yes or No");
        String inputString = emp.next();
        empQuestion = inputString.equalsIgnoreCase("Yes") ? true : false;

        if (empQuestion == false) {
            System.out.println("Come again Later!");
            break;
        }

        System.out.println("Enter the employee name: ");
        String empName = emp.next();

        System.out.println("Enter the employee ID");
        String empID = emp.next();

        System.out.println("Enter the employee Phone number");
        String empPhoneNumber = emp.next();

        System.out.println("Enter the employee Salary");
        Float empSalary = emp.nextFloat();

        EmployeesDetails employee = new EmployeesDetails(empName, empID, empPhoneNumber, empSalary);
        employeesDetails.add(employee);
        System.out.println(employeesDetails);
    }

    emp.close();
}
  1. Hi everyone, So here I've written a program for adding the employee details.
  2. I want to add the minimum and maximum wage for the employees, for example let's say if I have added 10 employees then I want to display the minimum salary of the employee who earns the least and display the salary of the employee who earns the highest.
  3. My initial approach was to use Math.min() but then I realized that it is used for comparing between two values. Then my second approach was to Collections.min(); but I realized it's used the min value of the list.
  4. Thank you and please go easy on me I've just started to learn coding.
deHaar
  • 17,687
  • 10
  • 38
  • 51
Mritunjay
  • 21
  • 7
  • 1
    Iterate over `employeesDetails` and keep the employee with the lowest `empSalary`. Alternatively use stream. – c0der Feb 10 '22 at 05:19
  • Thank you for posting what you've already tried. It really helps us figure out where you're at. You can still use [`Collections.min()`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Collections.html#min(java.util.Collection,java.util.Comparator)) if you want, you just need to provide your own [`Comparator`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Comparator.html) to order the list by salary. – Charlie Armstrong Feb 10 '22 at 05:46
  • 1
    There is handy utility in Java 8+ to collect some statistics in one iteration: `DoubleSummaryStatistics stats = employeesDetails.stream().mapToDouble(EmployeesDetails::getEmpSalary).summaryStatistics();` and then you can use `stats.getMin()` and `stats.getMax()` converting them to float if needed. – Nikolai Shevchenko Feb 10 '22 at 06:04

2 Answers2

0

The following demonstrates 4 possible solutions. Note the comments:

import java.util.*;

public class Main{

    public static void main(String[] args) {

        //Hardcoded data to make it more of an mre
        List<EmployeesDetails> employeesDetails = new ArrayList<>();
        employeesDetails.add(new EmployeesDetails("Sara", 1000f));
        employeesDetails.add(new EmployeesDetails("Deby", 1400f));
        employeesDetails.add(new EmployeesDetails("Dan", 1200f));
     
        //solution 1
        //Iterate over the collection and keep the employee with the highest (you can also keep the lowest)
        EmployeesDetails highestSalary = employeesDetails.get(0);
        for(EmployeesDetails e :employeesDetails ){
            if(e.getEmpSalary() > highestSalary.getEmpSalary()){
                highestSalary = e;
            }
        }

        //solution 2 contributed by Charlie Armstrong
        //Use Collections.max ( Collections.min )
        EmployeesDetails empD = Collections.max(employeesDetails, (e1, e2) -> Float.compare(e1.getEmpSalary(), e2.getEmpSalary()));
        System.out.println("employee with highest salary is "+empD);

        //solution 3
        //Use a stream, sort by salary find first to get the lowest
        //sort by salary in reverse and find first to get the highest
        Optional<EmployeesDetails> opEmpD = employeesDetails.stream().sorted((e1, e2) -> Float.compare(e2.getEmpSalary(), e1.getEmpSalary())).findFirst();
        System.out.println( opEmpD == null ? "No solution found" : "employee with highest salary is "+opEmpD.get());
        System.out.println("employee with highest salary is "+ highestSalary);

        //solution 4
        //Sort the collection by salary the last one is the highest
        //(get the first one for the lowest)
        Collections.sort(employeesDetails, (e1, e2) -> Float.compare(e1.getEmpSalary(), e2.getEmpSalary()));
        System.out.println("employee with highest salary is "+ employeesDetails.get(employeesDetails.size()-1));

    }
}

//simplified for to make it more of an mre
class EmployeesDetails {

    private final String empName;
    private final Float empSalary;

    public EmployeesDetails(String empName, Float empSalary) {
        this.empName = empName;
        this.empSalary = empSalary;
    }

    @Override
    public String toString() {
        return "EmployeesDetails{" +
                "Name='" + empName + '\'' +
                ", Salary='" + empSalary+ '\'' +
                '}';
    }

    public String getEmpName() {
        return empName;
    }

    public Float getEmpSalary() {
        return empSalary;
    }
}

See: https://stackoverflow.com/help/minimal-reproducible-example
c0der
  • 18,467
  • 6
  • 33
  • 65
  • 1
    Since you've got a list of solutions there, I thought I might throw another one in. I noticed the OP said they were already looking at [`Collections.min()`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Collections.html#min(java.util.Collection)), which means they were pretty close to something like this: `Collections.min(employeesDetails, Comparator.comparing(EmployeesDetails::getEmpSalary));` Basically the same as your sorted solutions, but without mutating the list. – Charlie Armstrong Feb 10 '22 at 06:29
0

Calculate maximum and minimum salary at the time of input, because you are taking input for only one time, so I think this solution will help you.

 import java.util.*;

class EmployeesDetails {
    System.out.println("Welcome Aboard!");
    Scanner emp = new Scanner(System.in);
    // add these parameters to hold min and max values for salary and employees
    float maxSalary = Float.MIN_VALUE;
    float minSalary = Float.MAX_VALUE;
    EmployeesDetails maxSalaryEmployee = null;
    EmployeesDetails minSalaryEmployee = null;
    List<EmployeesDetails> employeesDetails = new ArrayList<>();
    Boolean empQuestion = true;

    while (empQuestion == true) {
        System.out.println("Do you want to enter the employee details? Yes or No");
        String inputString = emp.next();
        empQuestion = inputString.equalsIgnoreCase("Yes") ? true : false;

        if (empQuestion == false) {
            System.out.println("Come again Later!");
            break;
        }

        System.out.println("Enter the employee name: ");
        String empName = emp.next();

        System.out.println("Enter the employee ID");
        String empID = emp.next();

        System.out.println("Enter the employee Phone number");
        String empPhoneNumber = emp.next();

        System.out.println("Enter the employee Salary");
        Float empSalary = emp.nextFloat();

        EmployeesDetails employee = new EmployeesDetails(empName, empID, empPhoneNumber, empSalary);
        employeesDetails.add(employee);
        // for max salary calculation
        if(employee.getEmpSalary() > maxSalary){
         maxSalary = employee.getEmpSalary();
         maxSalaryEmployee = employee;
        }
        // for min salary calculation
        if(employee.getEmpSalary() < minSalary){
         minSalary = employee.getEmpSalary();
         minSalaryEmployee = employee;
        }
        System.out.println(employeesDetails);
    }

        System.out.println("Max Salary Employee: " + maxSalaryEmployee);
        System.out.println("Min Salary Employee: " + minSalaryEmployee);
    emp.close();
}
DeePanShu
  • 1,236
  • 10
  • 23