Question
Hi! Please help me alter this Java code a little bit. I am working on an Array List but want to keep count of one
Hi! Please help me alter this Java code a little bit. I am working on an Array List but want to keep count of one specific item, in this case, the number of people with the same Department ID. For example, the output will say something like: Department ID 1, 2 (since there are 2 employees with the same department ID no. 1), Department ID 2, 2 (since there are 2 employees with the same department ID no. 2), et cetera...
I have looked up on Google and people kept telling me to change the Array List into a Hash Map, however, this is kind of difficult for two reasons:
1) I have over 4 elements that needed to be stored and Hash Map is not exactly feasible because technically, it only stores one key to one value (and yes, I have tried and failed to store more than one value to a Hash Map key).
2) I will need to sort employees by salary and department in later steps so an Array List seems to be working just fine for that purpose.
Please show me how to work this out. I don't mind changing the Array List into a Hash Map as long as I can still do the same sorting operations with it.
Also, is it possible for me to use another Array List to store the values in Table 2? I am currently using a Hash Map to store it but I also run into the same problem of not being able to store all 3 items (Department ID, Name and Address) within it. People on forums said that it is possible but I have never been successful at implementing their solution.
Thank you so much!
Here is the code:
public class Person { public static void main(String[] args) { // create Scanner object
Scanner scanner = new Scanner(System.in);
// data structures for department and employee data // use HashMap for department data HashMap
ArrayList
// variable to store the current department id int currentDepartmentId = 0; // prompt user to enter department data System.out.println("Please enter the department data (name and address):"); // loop until user enters an empty line
while (true) { System.out.print("Enter a department name (or press enter to end): "); // read the input from user String departmentName = scanner.nextLine(); // if the user enters an empty line, exit the loop
if (departmentName.isEmpty()) { break; } // increment the department id
currentDepartmentId++;
// store the department data in the HashMap
departments.put(currentDepartmentId, departmentName);
// prompt user to enter the department address
System.out.print("Enter the department address: ");
// read the input from user String
String departmentAddress = scanner.nextLine();
// store the department address in the HashMap departments.put(currentDepartmentId, departmentAddress); }
// prompt user to enter employee data
System.out.println("Please enter the employee data (name, department ID and gross salary):");
double grossSalary = 0; // loop until user enters an empty line while (true) { System.out.print("Enter an employee name (or press enter to end): ");
// read the input from user
String employeeName = scanner.nextLine();
// if the user enters an empty line, exit the loop
if (employeeName.isEmpty()) { break;
} // prompt user to enter department ID
System.out.print("Enter the department ID: ");
// read the input from user int
int departmentId = scanner.nextInt(); // check if the department ID entered by the user exists
if (!departments.containsKey(departmentId)) { System.out.println("Error: invalid department ID!"); // exit the program System.exit(1); }
// prompt user to enter gross salary System.out.print("Enter the gross salary: "); // read the input from user double grossSalary = scanner.nextDouble(); // create an array to store the employee data String[] employeeData = {employeeName, Integer.toString(departmentId), Double.toString(grossSalary)}; // add the employee data to the ArrayList employees.add(employeeData);
// consume the new line character scanner.nextLine(); } // NOW HOW DO I COUNT THE NUMBER OF EMPLOYEES WITH THE SAME DEPARTMENT ID?
The Human Resources (HR) department of a superstore needs an application for managing payroll (calculating employees' income tax, the wage bill for each department and producing reports). The superstore has three (3) departments as shown in Table 2, and each department has several employees, as shown in Table 1). The application should calculate the level of income tax based on Table 3 . Table 1: Sample employees' data. Table 2: Somple departments' data. Table 3: Tax Rate (https://www.gov.uk/income-tax-rates)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started