'Is there a way to read information from a saved file to check if there is an already exisiting doctorID saved?

So I am trying to create a Medical Clinic System Management program for a school project. This is my first project using NetBeans to create a GUI and unfortunately, my class spent about two weeks on the topic so I am not as well versed in this as I wish I was. So please don't be too harsh on my code. When I create a doctor object, the data gets saved to a file. When I go back and attempt to create another doctor with the same ID, my boolean isKeyPresent does not catch it because (I'm assuming) my doctor map is not associated with any of the doctors created in the save file. How can I fix this, so my system does not make a doctor if the ID is already in use?

private void btnAddDoctorActionPerformed(java.awt.event.ActionEvent evt) {                                             
        
        try {
            
            String id = txtDocID.getText();
            String name = txtDocName.getText();
            String specialty = txtSpecialty.getText();
        
            boolean isKeyPresent = administrative.doctors.containsKey(id);
            if(isKeyPresent) {
            
                JOptionPane.showMessageDialog(this, "Doctor ID Already Exists");
            }
            else {
            
                addDoctor(name, specialty, id);
                String s = name + ",";
                s += specialty + ",";
                s += id;
            
                FileWriter w = new FileWriter(fileLocation);
                BufferedWriter writer = new BufferedWriter(w);
                writer.write(s);
                writer.close();
                JOptionPane.showMessageDialog(this, "Doctor Added");
            }        
            txtDocID.setText("");
            txtDocName.setText("");
            txtSpecialty.setText("");
        }
        catch(IOException e) {
            
            JOptionPane.showMessageDialog(this, "File Error");
        }
    }


Solution 1:[1]

Firstly, I'd recommend converting the doctor id in the txtDocID field to lowercase (String#toLowerCase) when you access it, as that'd prevent the user from inputting a duplicate ID just with different casing.

I'm not sure what your addDoctor method does, I'm guessing it just adds it to the GUI. It'd be useful if you included the code for that. If that's the case, then you are correct in assuming the problem is that the administrative.doctors Map is not linked to your save file.

One way you could fix this is by also adding the doctor to the administrative.doctors Map after saving it to the save file.

However, I'd advice that you make a separate class for handling the saving and storage of doctors. This way you could simply call something like DoctorManager#saveDoctor and it would write to the file and save to the internal Map. It'd also be worth encapsulating the Map.

Finally, regarding the comment on a database, it sounds like you haven't been taught about them yet and its probably not worth the hassle for your use-case anyway. Abstracting your code to use a separate class for managing storage of doctors is likely ideal.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ben Anderson