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