Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.util.ArrayList; import java.util.Scanner; public class Salary { publ

ID: 3919764 • Letter: I

Question

import java.util.ArrayList; import java.util.Scanner; public class Salary { public String name; public Double hour; public Double rate; public Double calculate() { return hour * rate; } } public class TestSalary { ArrayList employeeArray = new ArrayList(); public void main(String[] args) { Scanner in = new Scanner(System.in); // public void sethighest(double max) // return max; int i = 0; while (true) { Salary s = new Salary(); System.out.print("Enter the name of Employee or Q to exit: "); String n = in.nextLine(); if (n.equalsIgnoreCase("Q")) { return; } else if(n == "R") { // remove name System.out.print("Enter the name of Employee"); String removedEmployee = in.nextLine(); for (Salary sal : employeeArray ) { if(sal.name == removedEmployee) { employeeArray.remove(sal); System.out.print("Its removed !!"); } } } else { // Adding name s.name = n; System.out.print("Enter the number hours worked: "); s.hour = in.nextDouble(); System.out.print("Enter the pay rate per hour: "); s.rate = in.nextDouble(); employeeArray.add(s); } for (Salary salary : employeeArray) { System.out.println("name of Emp" + salary.name); System.out.println("Salary" + salary.calculate()); System.out.println(name + " " + hour + " " + salary); } } } } this is my code for the following question how can i fix the errors: Write a program to process employee salaries. Store the full name (foo bar), the number of hours worked, and pay rate of employees using ArrayList objects. You can define an ArrayList for employee names, an ArrayList for hours, etc. We also need to be able to store the employee pay by calculating pay (hours * rate). MUST use MRETHODS!!! They’re ideal for this project. a) Prompt the user to enter some data for a list of potential employees. Of course, you need a loop to keep asking the user to enter data. The user should be able to enter as many data records as she wishes. b) Create a new array list to hold the pay for each employee. c) Display the size of the array lists. Make sure to be very descriptive in your output. d) Ask the user to enter the name of an employee and check if the person exists in the array list. Your search must be case insensitive. Display the message “EMPLOYEE found”, if the name is found in the list and display the employee’s corresponding employee information (name, hours, rate, and pay). Otherwise, display “EMPLOYEE NOT found”. EMPLOYEE is the name of the person. So, if the input for name is Foo Bar, we’ll display something like foo Bar, 10 20 200. If the input for name is Bill Bob, we’ll display something like Bill Bob NOT found, if we don’t have an employee named Bill Bob. e) Allow the user to add more names. f) Allow the user to remove names. If the name to be removed cannot be found, display an error message. g) Display the name of the employee with the highest salary. h) Display the data in a tabular format (name, hours, rate, and pay)

Explanation / Answer


Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you


ProcessSalary.java
----------------
import java.util.ArrayList;
import java.util.Scanner;

public class ProcessSalary {

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int choice = 0;

ArrayList<String> names = new ArrayList<String>();
ArrayList<Double> hours = new ArrayList<Double>();
ArrayList<Double> rate = new ArrayList<Double>();
ArrayList<Double> pay = new ArrayList<Double>();

String n;
double h, r, p;
int index;

while(choice != 6){
System.out.println("1. Add Employee");
System.out.println("2. Remove Employee");
System.out.println("3. Search Employee");
System.out.println("4. Display highest paid");
System.out.println("5. Display all");
System.out.println("6. Quit");
System.out.print("Enter your choice: ");
choice = keyboard.nextInt();
keyboard.nextLine(); //get rid of newline
switch(choice){
case 1:
System.out.println("Enter new details");
System.out.print("Enter name: ");
n = keyboard.nextLine();
System.out.print("Enter no. of hours: ");
h = keyboard.nextDouble();
System.out.print("Enter rate: ");
r = keyboard.nextDouble();
names.add(n);
hours.add(h);
rate.add(r);
p = calculatePay(h, r);
pay.add(p);
System.out.println("Added details for " + n);
break;

case 2:
System.out.print("Enter name of employee to be removed: ");
n = keyboard.nextLine();
index = indexOf(n, names);
if(index == -1)
System.out.println(n + " NOT found");
else {
names.remove(index);
hours.remove(index);
rate.remove(index);
pay.remove(index);
System.out.println("Removed details for " + n);
}
break;
case 3:
System.out.print("Enter name of employee to search: ");
n = keyboard.nextLine();
index = indexOf(n, names);
if(index == -1)
System.out.println(n + " NOT found");
else {
System.out.println(n + " found");
System.out.printf("%s, %.2f %.2f %.2f ", names.get(index), hours.get(index), rate.get(index), pay.get(index));
}
break;

case 4:
index = indexOfHighestPaid(pay);
if(index == -1)
System.out.println("Please enter employee data first");
else
System.out.printf("Highest paid is %s, %.2f %.2f %.2f ", names.get(index), hours.get(index), rate.get(index), pay.get(index));

break;
case 5:
System.out.printf("%25s %10s %10s %10s ", "Name", "Hours","Rate", "Pay");
for(int i = 0; i < names.size(); i++){
System.out.printf("%25s %10.2f %10.2f %10.2f ", names.get(i), hours.get(i), rate.get(i), pay.get(i));
}
break;
case 6:
break;
default:
System.out.println("Invalid menu choice");
}
}

}


static int indexOf(String name, ArrayList<String> names){
for(int i = 0; i < names.size(); i++){
if(names.get(i).equalsIgnoreCase(name))
return i;
}
return -1;
}
static double calculatePay(double hours, double rate){
return hours * rate;
}

static int indexOfHighestPaid(ArrayList<Double> pay){

if(pay.isEmpty())
return -1;
int highestIndex= 0;
for(int i = 1 ; i < pay.size(); i++)
{
if(pay.get(i) > pay.get(highestIndex))
highestIndex = i;
}

return highestIndex;
}
}
--------
output

1. Add Employee
2. Remove Employee
3. Search Employee
4. Display highest paid
5. Display all
6. Quit
Enter your choice: 1
Enter new details
Enter name: John Smith
Enter no. of hours: 40
Enter rate: 10
Added details for John Smith
1. Add Employee
2. Remove Employee
3. Search Employee
4. Display highest paid
5. Display all
6. Quit
Enter your choice: 1
Enter new details
Enter name: Alice
Enter no. of hours: 35
Enter rate: 8
Added details for Alice
1. Add Employee
2. Remove Employee
3. Search Employee
4. Display highest paid
5. Display all
6. Quit
Enter your choice: 3
Enter name of employee to search: Alice
Alice found
Alice, 35.00 8.00 280.00
1. Add Employee
2. Remove Employee
3. Search Employee
4. Display highest paid
5. Display all
6. Quit
Enter your choice: 3
Enter name of employee to search: Robert
Robert NOT found
1. Add Employee
2. Remove Employee
3. Search Employee
4. Display highest paid
5. Display all
6. Quit
Enter your choice: 4
Highest paid is John Smith, 40.00 10.00 400.00
1. Add Employee
2. Remove Employee
3. Search Employee
4. Display highest paid
5. Display all
6. Quit
Enter your choice: 5
Name Hours Rate Pay
John Smith 40.00 10.00 400.00
Alice 35.00 8.00 280.00