1

键入 ADD 时,我检查了代码并将数据保存到 HashMap 是正确的。然后在选择 FIND 选项后,我可以使用专用功能,但即使 100% 正确,该方法也无法显示找到的对象。请检查此代码并告诉我为什么它没有在“public void showInfo(String name, String secondName)”中找到由在 CompanyApp 类中键入“FIND”引发的 Company 类的正确对象


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


4

2 回答 2

1

问题出在方法static void options(int choose)上。您需要传递Company-Object 并在那里使用它,如下所示:

从 main 方法调用(refCompany您在 main 方法中创建的 -Object)

options(choose, ref);

options带有Companyas 第二个参数的- 方法:

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

解释代码中发生了什么

如前所述,问题出在方法static void options(int choose)上。

在这里,您创建一个新的Company-Object,它不会以任何方式传递给 main 方法。

当您使用 ADD 和之后的 FIND 时,会发生这种情况:

  1. options从主方法调用ADD
  2. Company对象创建于options
  3. new Employee-ObjectCompany从上一点添加到
  4. 方法结束 -> 创建的Company-Object 被“丢弃”(符合垃圾收集条件)
  5. options从主方法调用FIND
  6. new Company-Object 是在其中创建的options(因此其中没有员工)
  7. noEmployee找不到,因为新创建的map中没有条目Company
于 2021-11-12T18:32:32.747 回答
0

当您尝试使用 FIND 选项从中获取数据时,该地图为空。原因是您在 options 方法中重新创建 Company 对象:
Company ref = new Company();
同时,地图也被重新创建,所以里面没有记录。

此外,没有使用 main 方法中的 Company 对象。

于 2021-11-12T18:41:43.783 回答