My HashMap which contains new Doctor keeps overriding the previous one whenever I use addDoctor method

I use HashMap to store String followed by object Doctor, where the String is the copy of code in Doctor.

At first I thought it just some problem with checking condition and throw Exception but it not.
Secondly, I printed all the doctors that had been added but all the previous doctor was the same as the new doctor

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

This the part where the problem occurs:

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
}

These parts are just INPUT but maybe you will need it

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

New contributor

Duong Nguyen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

4

Your DoctorInputter doesn’t create a new Doctor instance when it inputs a doctor’s details, but instead has just one Doctor stored as a data member it keeps overwriting. You should create a new instance every time you input a new set of details:

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

Note that after changing the DoctorInputter like that, you can see it has no state, and getDoctorDetails could be static, although that may not fit your design.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật