I am new to Java and even new to generics in Java. I was looking for similar questions, but could not find a direct answer for my specific problem.
I am developing a project for managing patients, doctors, consultations, medical events and everything related to medical clinics. What I'm trying to do right now is to create a list of medical events related to each patient. To this list of medical events at the moment, only admission to exams and prescriptions is allowed, but it must be extensible: I want to be able to add other types of medical events in the future, if I need it, such as information about operations.
So, I started by creating an ArrayList from generic ArrayLists in the Patient class, with its type limited by the extension of the MedicalEvent class (so at the moment it's an ArrayList from ArrayLists of type Prescription or Exam). I also created an ArrayList of type Prescription and another type of Exam.
List<ArrayList<? extends MedicalEvent>> medicalevents;
private ArrayList<Prescription> prescriptions;
private ArrayList<Exam> exams;
Then in the constructor I added ArrayLists recipes and exams to ArrayList medicines.
medicalevents.add(prescriptions);
medicalevents.add(exams);
To add medical events to one of two valid types, I defined the following method:
public void addMedicalEvent(E element){ if(element instanceof Prescription){ medicalevents.get(0).add((Prescription)element); } if(element instanceof Exam){ medicalevents.get(1).add((Exam)element); } }
The problem is that I get the error message "The add (capture # 1-of? Extends MedicalEvent) method in the ArrayList type is not applicable for (Prescription) arguments" and I don't know what that means. Can someone tell me what I'm doing wrong, or suggest a better way to solve this problem?
Thanks!