Home > database >  How can I solve with Java's map containsKey() method?
How can I solve with Java's map containsKey() method?

Time:11-13

I checked the code and saving data to the HashMap is correct, when typing ADD. Then after choosing option FIND I can get to the dedicated function but the method is unable to show me found object even if it is correct 100%. Please check this code out and tell me why it does not find right objects in "public void showInfo(String name, String secondName)" for class Company that is sparked by TYPING "FIND" in class CompanyApp


import java.util.InputMismatchException;
import java.util.Objects;
import java.util.Scanner;

public class CompanyApp {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        options[] values = options.values();
        int choose;
        int EXIT_NR = 2;
        Company ref = new Company();
        do {
            System.out.println("Available options: ");
            for (options one : values) {
                System.out.println(one.getDescription()   " - "   one.name());
            }
            System.out.println("Choose one: ");
            try {
                choose = options.valueOf(in.nextLine()).ordinal();
                if (Objects.equals(EXIT_NR, choose)) break;
                if (choose < 0 || choose >= options.values().length) {
                    throw new IllegalArgumentException("Choose 0, 1 or 2!");
                }
                options(choose);
            } catch (InputMismatchException e) {
                System.out.println("Choose a number ");
            }


        } while (1 == 1);
    }

    static void options(int choose){
        Company ref = new Company();
        Scanner info = new Scanner(System.in);

        switch (choose){
            case 0:
                System.out.println("Type in the name of the worker: ");
                String name = info.nextLine();
                System.out.println("Type in the second name of the worker: ");
                String secondName = info.nextLine();
                System.out.println("Type in the salary: ");
                double salary = info.nextDouble();
                info.nextLine();
                ref.add(new Employee(name, secondName, salary));
                break;
            case 1:
                System.out.println("Type in the name of the worker you want to find: ");
                String name2 = info.nextLine();
                System.out.println("Type in the second name of the worker you want to 
                find: ");
                String secondName2 = info.nextLine();
                ref.showInfo(name2, secondName2);
                break;
        }
    }
}

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class Company {
    private Map<String, Employee> map = new HashMap<>();

    public void add(Employee employee){
        String key = employee.getName()   " "   employee.getSecondName();
        if(!map.containsKey(key)){
        map.put(key, employee);
            System.out.println("Added object to map");}

    }

    public void showInfo(String name, String secondName){

        String key = name   " "   secondName;
        System.out.println("in showinfo method");
        if(map.containsKey(key)){
            System.out.println("found an object");
            Employee employee = map.get(key);
            System.out.println(employee.getName());
        }}}  

enum options {

     ADD("Add employee "), FIND("Find employee"), EXIT("Exit program");
    private String description;

     options(String description) {
         this.description = description;
     }

     public String getDescription() {
         return description;
     }

     public void setDescription(String description) {
         this.description = description;
     }

     @Override
     public String toString() {
         return "options{"  
                 "description='"   description   '\''  
                 '}';
     }
 }

    String name;
    String secondName;
    double salary;

    public Employee(String name, String secondName, double salary) {
        this.name = name;
        this.secondName = secondName;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSecondName() {
        return secondName;
    }

    public void setSecondName(String secondName) {
        this.secondName = secondName;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }





    @Override
    public String toString() {
        return "Employee{"  
                "name='"   name   '\''  
                ", secondName='"   secondName   '\''  
                ", salary="   salary  
                '}';
    }
}


CodePudding user response:

The problem is in the method static void options(int choose). You need to pass the Company-Object and use it there like this:

Call from main method (ref is the Company-Object you create in the main method)

options(choose, ref);

The options-method with the Company as second parameter:

  static void options(int choose, Company ref){
    Scanner info = new Scanner(System.in);

    switch (choose){
      case 0:
        System.out.println("Type in the name of the worker: ");
        String name = info.nextLine();
        System.out.println("Type in the second name of the worker: ");
        String secondName = info.nextLine();
        System.out.println("Type in the salary: ");
        double salary = info.nextDouble();
        info.nextLine();

        //use the passed Company here
        ref.add(new Employee(name, secondName, salary));
        break;
      case 1:
        System.out.println("Type in the name of the worker you want to find: ");
        String name2 = info.nextLine();
        System.out.println("Type in the second name of the worker you want to find: ");
        String secondName2 = info.nextLine();
 
        //and here
        ref.showInfo(name2, secondName2);
        break;
    }
  }

Explanation what is happening in your code

As mentioned, the problem is in the method static void options(int choose).

Here you create a new Company-Object which is not passed in any way to the main method.

This is what happens, when you use ADD and a FIND afterwards:

  1. Call options from main method with ADD
  2. new Company-Object is created in options
  3. new Employee-Object is added to the Company from the previous point
  4. the method ends -> the created Company-Object is "thrown away" (eligible for Garbage Collection)
  5. Call options from main method with FIND
  6. new Company-Object is created in options(therefore no Employees in it)
  7. no Employee can be found, because there is no entry in the map of the newly created Company

CodePudding user response:

The map is empty at the time when you're trying to get the data from it using FIND option. The reason for that is you recreate the Company object in the options method:
Company ref = new Company();
At the same time also the map is recreated so there are no records inside.

Also, the Company object in the main method is not used.

  • Related