0

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;
}
}
Will Ricard
  • 21
  • 1
  • 4
  • 4
    You cant have multiple `key`(s) with the same value. `map.put` is *replacing* the singular value at `"2"` with each `empMap.put("2",` - after your chain you have one `"2", emp20` pair in your map. Similarly for your `"5"` and `emp18`. And so on... – Elliott Frisch Mar 17 '17 at 01:19
  • 3
    You probably want a `Map>` or a multimap. – shmosel Mar 17 '17 at 01:20
  • @Elliot Frisch How would I go about making it so that each one is independent and wont be over written? – Will Ricard Mar 17 '17 at 01:20
  • @shmosel what would map> do? – Will Ricard Mar 17 '17 at 01:22
  • What do you think? – shmosel Mar 17 '17 at 01:22
  • It isn't clear why you want a `Map` in the first place. Please post `Employee`,I think you really just need a `List` (or maybe a `Set`) - because it appears your "key" is the second parameter to the `Employee` constructor (only converted from an `int` to a `String` for some reason). – Elliott Frisch Mar 17 '17 at 01:24
  • @shmosel I'm guessing change it so it is an array? – Will Ricard Mar 17 '17 at 01:24
  • 1
    Well, a list is similar to an array. But you'll need to insert data differently. See [here](http://stackoverflow.com/questions/3019376/shortcut-for-adding-to-list-in-a-hashmap) for details. – shmosel Mar 17 '17 at 01:26
  • @ElliottFrisch I added the Employee class. – Will Ricard Mar 17 '17 at 01:26
  • 2
    Oh dear, all of your `setters` suffer from naming conflicts. You should be getting warnings, because you're setting the parameter to itself (the fields are **shadowed**). – Elliott Frisch Mar 17 '17 at 01:29
  • @ElliottFrisch I feel so stupid for that, didn't even realize ill upload the fixed one. – Will Ricard Mar 17 '17 at 01:33
  • `Department` could have a `Set` as a member, which is the standard way to implement something like this. – Lew Bloch Mar 17 '17 at 01:42

3 Answers3

2

Part One

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;
    }
}

Part Two

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);
    }
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

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,

  • Fetch the value from hashmap based on key.
  • Now you will get arrayList
  • Loop through the result arraylist and get all your employee object

.

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) {
        .....
    }
}
GrabNewTech
  • 631
  • 6
  • 14
0

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 ?

Community
  • 1
  • 1
Joe Lin
  • 56
  • 2