0

Can anyone help on how to get the employee with the highest net pay using Java?

import java.util.*; public class LabExercise { public static void main (String[] args) { double grossPay; int hoursWorked; int ratePerHour; double totalDeduction; double amountTax; double amountSSS; double amountPhilhealth; double netPay; String nameOfEmployee; int numberOfEmployees; int initialNumberOfEmployees = 1; Scanner input = new Scanner (System.in); System.out.print("Enter number of employees: "); numberOfEmployees = input.nextInt(); while (initialNumberOfEmployees <= numberOfEmployees) { System.out.println("\nEmployee No. " + initialNumberOfEmployees); input.nextLine(); System.out.print("Enter Employee Name: "); nameOfEmployee = input.nextLine(); System.out.print("Enter Hours Worked: "); hoursWorked = input.nextInt(); System.out.print("Enter Rate per Hour: "); ratePerHour = input.nextInt(); grossPay = hoursWorked * ratePerHour; System.out.println("Gross Pay: " + grossPay); amountTax = grossPay * 0.12; System.out.println("Tax: " + amountTax); amountSSS = grossPay * 0.05; System.out.println("SSS: " + amountSSS); amountPhilhealth = grossPay * 0.02; System.out.println("Philhealth: " + amountPhilhealth); totalDeduction = amountTax + amountSSS + amountPhilhealth; System.out.println("Total Deduction: " + totalDeduction); System.out.println("Employee Name: " + nameOfEmployee); netPay = grossPay - totalDeduction; System.out.println("Net Pay: " + netPay); initialNumberOfEmployees++; } input.close(); } }

4th Dec 2021, 5:14 AM
Emreich John Carumay
Emreich John Carumay - avatar
3 Réponses
+ 2
Store the net pays for all the employees in a List. Then use the method Collections.max() on netPayList to find the highest net pay.
4th Dec 2021, 5:37 AM
Anannya
+ 2
Can it also display the name of the employee with the highest net pay?
4th Dec 2021, 5:40 AM
Emreich John Carumay
Emreich John Carumay - avatar
+ 2
No, for that a Map would be better. Since you can have Employee name as key and netPay as value. Then you can just find the key associate with max value. ---- Why don't you finish the java course? The last project "Bowling game" is similar to what you want to achieve. i.e. Find the key with max value.
4th Dec 2021, 5:44 AM
Anannya