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