Home > Net >  How can I Calculate the maximum and minimum salary of the employees?
How can I Calculate the maximum and minimum salary of the employees?

Time:02-10

import java.util.*;

class EmployeesDetails {
    // creating instance variables
    private String empName;
    private String empID;
    private String empPhoneNo;
    private Float empSalary;

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

    @Override
    public String toString() {
        return "EmployeesDetails{"  
                "empName='"   empName   '\''  
                ", empID='"   empID   '\''  
                ", empPhoneNo='"   empPhoneNo   '\''  
                '}';
    }
}
public class Main {
  public static void main(String[] args) {
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.

CodePudding user response:

  •  Tags:  
  • Related