每当我使用 addDoctor 方法时,包含新 Doctor 的 hashMap 都会覆盖前一个,我不知道问题是什么?

问题描述 投票:0回答:1

我使用HashMap来存储String,然后是Object Doctor,该String是Doctor中Code的副本

起初我以为这只是检查条件并抛出异常的一些问题,但事实并非如此。 其次,我打印了所有已添加的医生,但所有以前的医生都与新医生相同

如果我缺少任何信息来对这个问题进行分类,请告诉我,您的帮助对我来说意义重大

public class Main {
    public static void main(String[] args) {
        DoctorController controler = new DoctorController();
        try {
            controler.addWorker();
            controler.addWorker();
            controler.displayAll();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
  • 这是出现问题的部分
public class DoctorController {
    private final DoctorInputer inputer;
    private final DoctorManagement management;

    public DoctorController() {
        management = new DoctorManagement();
        inputer = new DoctorInputer();
    }

    public void addWorker() throws Exception {
        Doctor doctor = inputer.getDoctorDetails();
        if (management.addDoctor(doctor)) {
            System.out.println("Add successful");
        } else {
            System.out.println("Can not add doctor!");
        }
    }

    public void displayAll() {
        management.displayAll();
    }
}
import java.util.HashMap;
public class DoctorManagement {
    
    private final HashMap<String, Doctor> doctors;

    public DoctorManagement() {
        doctors = new HashMap<>();
    }
    
    public boolean addDoctor(Doctor d) throws Exception {
        if (doctors.containsKey(d.getCode())) {
            throw new Exception("Doctor code " + d.getCode() + " is duplicate");
        }
        if (d == null) {
            throw new Exception("Data does not exist");
        }
        doctors.put(d.getCode(), d);
        return true;
    }

    //Method: Displat All the doctor
}
  • 这些部分只是输入但也许你会需要它
public class DoctorInputer {
    private final Doctor doctor;

    public DoctorInputer() {
        doctor = new Doctor();
    }

    public Doctor getDoctorDetails() {
        doctor.setCode(Validation.getString("Enter code: ",
                "Code should follow format 'DOC_number'",
                "^DOC\\d+$"));
        doctor.setName(Validation.getString("Enter name: ",
                "Invalid",
                "[a-zA-Z]+"));
        doctor.setSpecialization(Validation.getString("Enter specialization: ",
                "Invalid",
                "[a-zA-Z]+"));
        doctor.setAvaiability(Validation.getInt("Enter avaiability: ",
                "Avaiability >= 0",
                "Invalid", 0, Integer.MAX_VALUE));
        return doctor;
    }
}
public class Doctor {
    private String code;
    private String name;
    private String specialization;
    private int avaiability;

// Getter and Setters
    
    @Override
    public String toString(){
        return String.format("%-6s %-10s %-12s %-2d", code, name, specialization, avaiability);
    }
}
java database hashmap
1个回答
0
投票

您的

DoctorInputter
在输入医生的详细信息时不会创建新的
Doctor
实例,而是仅将一个
Doctor
存储为数据成员,并不断覆盖。每次输入一组新的详细信息时,您都应该创建一个新实例:

public class DoctorInputer {
    public Doctor getDoctorDetails() {
        Doctor doctor = new Doctor(); // Here!
        doctor.setCode(Validation.getString("Enter code: ",
                "Code should follow format 'DOC_number'",
                "^DOC\\d+$"));
        doctor.setName(Validation.getString("Enter name: ",
                "Invalid",
                "[a-zA-Z]+"));
        doctor.setSpecialization(Validation.getString("Enter specialization: ",
                "Invalid",
                "[a-zA-Z]+"));
        doctor.setAvaiability(Validation.getInt("Enter avaiability: ",
                "Avaiability >= 0",
                "Invalid", 0, Integer.MAX_VALUE));
        return doctor;
    }
}

请注意,像这样更改

DoctorInputter
后,您可以看到它没有状态,并且
getDoctorDetails
可能是
static
,尽管这可能不适合您的设计。

© www.soinside.com 2019 - 2024. All rights reserved.