0

I have a csv file which contains doctor details: name, surname, address, city, preferredContact, contact id and specialism.

I have created a doctor list in which I want to store my doctor objects. I decided to split the scanner.nextLine() and parse each value into a Doctor instance.

That worked for me, all values were read in correctly and assigned to the correct variable associated with the doctor, i.e name, surname etc, but the only problem is that if a doctors specialism is surgery, I am required to store an additional value which is the certification date.

To do so, I created a extended Surgeon class and tried using inheritance but got lost at this stage and run out of ideas on how to go about this problem.

Is anyone able to tell me what I can change to make this work?

public class Doctor 
{

    protected String name;
    protected String surname;
    protected String address;
    protected String city;
    protected String preferredContact;
    protected String contactID;
    protected String specialism;

    public Doctor()
    {
        this.name="";
        this.surname="";
        this.address="";
        this.city="";
        this.preferredContact="";
        this.contactID="";
        this.specialism="";
    }

    public Doctor(String name,String surname, String address, String city, String preferredContact, String contactID, String specialism)
    {
        this.name=name;
        this.surname=surname;
        this.address=address;
        this.city=city;
        this.preferredContact=preferredContact;
        this.contactID=contactID;
        this.specialism=specialism;
    }

    public void setName(String name)
    {
        this.name=name;
    }

    public String getName()
    {
        return name;
    }

    public void setSurname(String surname)
    {
        this.surname=surname;
    }

    public String getSurname()
    {
        return surname;
    }

    public void setAddress(String address)
    {
        this.address=address;
    }

    public String getAddress()
    {
        return address;
    }

    public void setCity(String city)
    {
        this.city=city;
    }

    public String getCity()
    {
        return city;
    }

    public void setPreferredContact(String preferredContact)
    {
        this.preferredContact=preferredContact;
    }

    public String getPreferredContact()
    {
        return preferredContact;
    }

    public void setContactID(String contactID)
    {
        this.contactID=contactID;
    }

    public String getContactID()
    {
        return contactID;
    }

    public void setSpecialism(String specialism)
    {
        this.specialism=specialism;
    }

    public String getSpecialism()
    {
        return specialism;
    }

    @Override
    public String toString()
    {
        return "\nName:"+getName()
            +"\nSurname: "+getSurname()
            +"\nAddress: "+getAddress()
            +"\nCity: "+getCity()
            +"\nPreferred Means of Contact: "+getPreferredContact()
            +"\nContact ID: "+getContactID()
            +"\nSpecialism: "+getSpecialism()
            +"\n";

    }

}

public class Surgeon extends Doctor
{

    protected String certificationDate;

    public Surgeon()
    {
        super();
        certificationDate="";
    }

    public Surgeon(String name,String surname, String address, String city, String preferredContact, String contactID, String specialism, String certificationDate)
    {
        super(name,surname,address,city,preferredContact,contactID,specialism);
        this.certificationDate=certificationDate;
    }

    public String getCertificationDate()
    {
        return certificationDate;
    }

    @Override
    public String toString()
    {
        return "\nCertificationDate: "+certificationDate +"\n";
    }
}

public class DoctorImport 
{

    public static void main (String[]args) 
    {   
        int index = 0;

        List<Doctor> doctorsList = new ArrayList<>();

        try
        {
            Scanner scanner=new Scanner(new File("DoctorsFile.csv"));
            Scanner dataScanner;

            while (scanner.hasNextLine())
            {
                dataScanner=new Scanner(scanner.nextLine());
                dataScanner.useDelimiter(",");

                Doctor myDoctor=new Doctor();
                Surgeon mySurgeon=new Surgeon();

                while(dataScanner.hasNext())
                {
                    String data= dataScanner.next();

                    switch (index) 
                    {
                        case 0:
                            myDoctor.setName(data);
                            break;

                        case 1:
                            myDoctor.setSurname(data);
                            break;

                        case 2:
                            myDoctor.setAddress(data);
                            break;

                        case 3:
                            myDoctor.setCity(data);
                            break;

                        case 4:
                            myDoctor.setPreferredContact(data);
                            break;

                        case 5:
                            myDoctor.setContactID(data);
                            break;

                        case 6:
                            myDoctor.setSpecialism(data);
                            break;

                         case 7:
                            mySurgeon.certificationDate=data;
                            break;

                     }
                    index++;
                }
                doctorsList.add(myDoctor);

                if((myDoctor.specialism).equals("Surgery"))
                {
                    doctorsList.add(mySurgeon);
                }

                index=0;  
            }
            System.out.print(doctorsList);
        } 

        catch (FileNotFoundException ex) 
        {
            System.out.print("Error, unable to locate the CSV File!");
        }

    }

}
2
  • Are you getting some error or what is the problem that you are facing? Commented Nov 29, 2017 at 21:21
  • So, your problem is that the Surgeon class does not contain all the correct data when the doctor's specialism is surgery. Commented Nov 29, 2017 at 21:31

3 Answers 3

0

One approach would be to collect all the data first, then create the right type of object later. I recommend reading up on the Factory Pattern, as it encapsulates the decision making about which type of object to construct.

For example:

    ...
    String name = "";
    String surname = "";
    String address = "";
    String city = "";
    String preferredContact = "";
    String contactID = "";
    String specialism = "";
    String certificationDate = "";
    while(dataScanner.hasNext())
    {
        switch (index) 
        {
            case 0:
                name = dataScanner.next();
                break;

            case 1:
                surname = dataScanner.next();
                break;

            case 2:
                address = dataScanner.next();
                break;

            case 3:
                city = dataScanner.next();
                break;

            case 4:
                preferredContact = dataScanner.next();
                break;

            case 5:
                contactID = dataScanner.next();
                break;

            case 6:
                specialism = dataScanner.next();
                break;

            case 7:
                certificationDate = dataScanner.next();
                break;

            default:
                dataScanner.next();
                break;
        }
        index++;
    }

    if (specialism.equals("surgery")) {
        doctorsList.add(new Surgeon(name, surname, address, city, preferredContact, contactID, specialism, certificationDate));
    } else {
        doctorsList.add(new Doctor(name, surname, address, city, preferredContact, contactID, specialism));
    }

The factory pattern could be used to implement the last part where the decision of which object to create is performed:

    doctorsList.add(buildDoctor(name, surname, address, city, preferredContact, contactID, specialism, certificationDate));

... which calls the factory method:

private Doctor buildDoctor(String name, String surname, String address, String city, String preferredContact, String contactID, String specialism, String certificationDate) {
    if (specialism.equals("surgery")) {
        return new Surgeon(name, surname, address, city, preferredContact, contactID, specialism, certificationDate);
    } else {
        return new Doctor(name, surname, address, city, preferredContact, contactID, specialism);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

I guess, you have some errors on your code.

One of them is:

You have initialized a Doctor object while you read your file, but if it has a specialty, then you read the data and Add the Surgen object to your list. BUT your surgeon object has no info and is not linked to a Doctor object.

Do your code worked at some point? have you tested it? If so, we can work from that point.

Regards.

Comments

0

The problem is that you are creating both a Doctor and a Surgeon instance. You then populate the Doctor instance; if you have a certification date, you put that in the (otherwise empty) Surgeon instance.

You need to create one instance or the other, based on some criteria. Just using whether a certification date was passed is poor practice. Perhaps the specialism field would work.

If you have a Surgeon instance, assign it to the Doctor field as well. Then the one instance will be populated with all information.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.