Reputation: 21
So the issue I'm having is for the program to print out every value that holds a certain key. The program asks the user what department they want to see the employee roster for. The user then picks and what department they want to see and enters the value printed next to the department, lets say they chose "4. accounting". The program then needs to print out all the values that are associated with that key. in the format of "Employees in accounting: employee 1, employee 2, etc. . It's also supposed to use the method countEmps. What I specifically need help with is the syntax to make the program take the values that have the desired key and print them out. Any help is appreciated!
import java.util.ArrayList;
import java.util.HashMap;
public class Department{
ArrayList<Employee> countEmps(HashMap <String, Employee> e , int I, ArrayList<Employee> l){
return l;
}
}
import java.util.Scanner;
import java.util.*;
public class empCountDriver {
public static void main(String[] args) {
Map <String, Employee> empMap = new HashMap<>();
int menuselect = 0;
Scanner key = new Scanner(System.in);
Employee emp1 = new Employee (101,2 ,3 , "John", "Joe");
Employee emp2 = new Employee (102,5 ,6 , "Phil", "Jognson");
Employee emp3 = new Employee (103,4 ,7 , "Billy", "Schmitz");
Employee emp4 = new Employee (104,3 ,3 , "Hank", "Dugart");
Employee emp5 = new Employee (105,1 ,2 , "Able", "Philstein");
Employee emp6 = new Employee (106,3 ,10 , "Adam", "Renolds");
Employee emp7 = new Employee (107,1 ,4 , "Larry", "Cableguy");
Employee emp8 = new Employee (108,5 ,2 , "Doug", "Dougster");
Employee emp9 = new Employee (109,2 ,3 , "Mylan", "Ester");
Employee emp10 = new Employee (110,5 ,6 , "Cherry", "Jakesn");
Employee emp11 = new Employee (111,2 ,3 , "Ethel", "Manfred");
Employee emp12 = new Employee (112,3 ,11 , "Lindsey", "Joel");
Employee emp13 = new Employee (113,1,1 , "Ellie", "Winston");
Employee emp14 = new Employee (114,4 ,6 , "Blake", "Wiotell");
Employee emp15 = new Employee (115,1 ,3 , "Elton", "John");
Employee emp16 = new Employee (116,2 ,7 , "David", "Flint");
Employee emp17 = new Employee (117,4 ,600000 , "Igor", "TheElder");
Employee emp18 = new Employee (118,5 ,9 , "Hanz", "Joe");
Employee emp19 = new Employee (119,2 ,3 , "Jordan", "Friedel");
Employee emp20 = new Employee (120,2 ,3 , "Dyaln", "Rogers");
empMap.put("2", emp1);
empMap.put("5", emp2);
empMap.put("4", emp3);
empMap.put("3", emp4);
empMap.put("1", emp5);
empMap.put("3", emp6);
empMap.put("1", emp7);
empMap.put("5", emp8);
empMap.put("2", emp9);
empMap.put("5", emp10);
empMap.put("2", emp11);
empMap.put("3", emp12);
empMap.put("1", emp13);
empMap.put("4", emp14);
empMap.put("1", emp15);
empMap.put("2", emp16);
empMap.put("4", emp17);
empMap.put("5", emp18);
empMap.put("2", emp19);
empMap.put("2", emp20);
menus();
menuselect = key.nextInt();
while (menuselect!= 6){
//if the user enters 1 it will print out all the employees with the key of 1, if the user enters 2
//it will print out all the employees with the key of 2 and so on.
}
}
public static void menus(){
System.out.println("Company Departments:");
System.out.println("1. IS");
System.out.println("2. Accounting");
System.out.println("3. Purchasing");
System.out.println("4. Sales");
System.out.println("5. Advertising");
System.out.println("6. EXIT");
}
}
The Employee class:
public class Employee {
private int employeeID;
private int departmentID;
private int employedYears;
private String firstName;
private String lastName;
public Employee ( int eid, int d, int y, String fn, String ln){
employeeID = eid;
departmentID = d;
employedYears = y;
firstName = fn;
lastName = ln;
}
public void setEmployeeID (int EmployeeID){
employeeID = EmployeeID;
}
public int getEmployeeID(){
return employeeID;
}
public void setDepartmentID (int DepartmentID){
departmentID = DepartmentID;
}
public int getDepartmentID(){
return departmentID;
}
public void setEmployedYears(int EmployedYears){
employedYears = EmployedYears;
}
public int getEmployedyears(){
return employedYears;
}
public void setFirstName (String FirstName){
firstName = FirstName;
}
public String getFirstName (){
return firstName;
}
public void setLastName (String LastName){
lastName = LastName;
}
public String getLastName (){
return lastName;
}
}
Upvotes: 0
Views: 620
Reputation: 56
this seems duplicate with many previous questions. like this one : HashMap with multiple values under the same key
But I can give you a simple solution. That is "reverse your key,value pair" and use it like below:
HashMap<String,Integer> data = new HashMap();
data.put("Joe", 1);
data.put("Lin",1);
data.put("Haha",2);
//select apartment like below
for(Entry<String,Integer> i :data.entrySet()){
if(i.getValue()==2){
System.out.println(i.getKey());
}
}
The most important thing is your design, is this solution matches your whole design ?
Upvotes: 0
Reputation: 641
Some code snippet. Please try this
Explanation
Since in hashmap, you cannot add same keys multiple times, the value object will get override. So add all your employee object to a array List and put the result arraylist to the hashmap with specified key.
While retrieving,
.
Map <String, List<Employee)> empMap = new HashMap <String, ArrayList<Employee)>();
.....
Employee emp2 = new Employee (102,5 ,6 , "Phil", "Jognson");
.
.
Employee emp9 = new Employee (109,2 ,3 , "Mylan", "Ester");
List<Employee> emp2List = new ArrayList<Employee>();
emp2List.add(emp2);
emp2List.add(emp9);
empMap.put("2", emp2List );
.....
while (menuselect!= 6){
//if the user enters 1 it will print out all the employees with the key of 1, if the user enters 2
//it will print out all the employees with the key of 2 and so on.
List<Employee) empList = empMap.get(menuselect);
for (Employee employee : empList) {
.....
}
}
Upvotes: 0
Reputation: 201497
You need to fix your Employee
. Since your setters were broken, I removed them - you aren't using them anyway and there are advantages to making a class immutable. You also need to implement hashCode
and override equals
for part two (where I assume employeeId
(s) are unique).
class Employee {
private final int employeeID;
private final int departmentID;
private final int employedYears;
private final String firstName;
private final String lastName;
public Employee(int eid, int d, int y, String fn, String ln) {
employeeID = eid;
departmentID = d;
employedYears = y;
firstName = fn;
lastName = ln;
}
@Override
public int hashCode() {
return Integer.hashCode(employeeID);
}
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (o instanceof Employee) {
return employeeID == ((Employee) o).employeeID;
}
return false;
}
@Override
public String toString() {
return String.format("%s, %s %d %d %d", lastName, firstName, //
employeeID, departmentID, employedYears);
}
public int getEmployeeID() {
return employeeID;
}
public int getDepartmentID() {
return departmentID;
}
public int getEmployedyears() {
return employedYears;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
Now you can create a HashSet
of Employee
(s) and filter by department (and because toString
is implemented you can just print), like
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
Set<Employee> empSet = new HashSet<>(Arrays.asList(//
new Employee(101, 2, 3, "John", "Joe"), //
new Employee(102, 5, 6, "Phil", "Jognson"), //
new Employee(103, 4, 7, "Billy", "Schmitz"), //
new Employee(104, 3, 3, "Hank", "Dugart"), //
new Employee(105, 1, 2, "Able", "Philstein"), //
new Employee(106, 3, 10, "Adam", "Renolds"), //
new Employee(107, 1, 4, "Larry", "Cableguy"), //
new Employee(108, 5, 2, "Doug", "Dougster"), //
new Employee(109, 2, 3, "Mylan", "Ester"), //
new Employee(110, 5, 6, "Cherry", "Jakesn"), //
new Employee(111, 2, 3, "Ethel", "Manfred"), //
new Employee(112, 3, 11, "Lindsey", "Joel"), //
new Employee(113, 1, 1, "Ellie", "Winston"), //
new Employee(114, 4, 6, "Blake", "Wiotell"), //
new Employee(115, 1, 3, "Elton", "John"), //
new Employee(116, 2, 7, "David", "Flint"), //
new Employee(117, 4, 600000, "Igor", "TheElder"), //
new Employee(118, 5, 9, "Hanz", "Joe"), //
new Employee(119, 2, 3, "Jordan", "Friedel"), //
new Employee(120, 2, 3, "Dyaln", "Rogers")));
while (true) {
menus();
int choice = key.hasNextInt() ? key.nextInt() : 6;
if (choice == 6) {
break;
}
empSet.stream().filter(emp -> emp.getDepartmentID() == choice)
.forEach(System.out::println);
}
}
Upvotes: 2