Just working on my java assignment and I've hit a brick wall I just cant seem to find a solution for - I'm not necessarily looking for an answer, even some ideas to what tools might help me :) Anyway here goes:
As the title says, I am looking to create a calendar object within an object within an Arraylist. Basically, as per the code below - I thought that when an instance of the Appointment object was created the link between the calendar object and the appointment object would be severed and I could reuse the Calendar object for the next appointment object. Unfortunately each object retains its reference to the calendar object, rather than create its own instance of the Calendar object =/.
Some background on the work:
Basically this piece of java code, scans the file and extracts the information out of it, makes sure its valid then creates an instance of the appropriate object within the one of the two arraylists. I'm working within constraints of my tutor who has specified that I must use an arraylist. Any help would greatly appreciated.
The constructor for the appointment class:
public Appointment (Patient patient, Provider provider, GregorianCalendar date, boolean standard, boolean attended)
Example Appointment Data
Appointment#84736254193#123456AF#22.30#20/12/2012#false#True
public AppointmentsManager (String path) {
this.path = path;
String[] fileLine;
boolean throwError = false;
DateFormat df = new SimpleDateFormat ("HH.mm dd/MM/yyyy");
df.setLenient(false);
GregorianCalendar calendar = new GregorianCalendar();
try {
Scanner input = new Scanner(new File(path));
String line;
while (input.hasNext()) {
line = input.nextLine();
fileLine = line.split("#");
if (fileLine.length < 0)
throw new IllegalArgumentException("Error: the data in the file is has not been delimited correctly. Please review");
if (fileLine[0].matches("Provider")) {
if (fileLine.length < 7)
throw new IllegalArgumentException("Error: the provider data in the file is incomplete. Please review");
persons.add(new Provider(fileLine[1], fileLine[2], fileLine[3], fileLine[4], fileLine[5],
fileLine[6]));
}
else if (fileLine[0].matches("Patient")) {
fileLine = line.split("#");
if (fileLine.length < 11)
throw new IllegalArgumentException("Error: the patient data in the file is incomplete. Please review");
for (int i = 0; i < persons.size(); i++) {
if (persons.get(i).getMedicare().matches(fileLine[10])) {
persons.add(new Patient(fileLine[1], fileLine[2], fileLine[3], fileLine[4], fileLine[5],
fileLine[6], fileLine[7], fileLine[8], Integer.parseInt(fileLine[9]),(Provider)persons.get(i)));
throwError = true;
}
}
if (throwError!=true) {
throw new IllegalArgumentException("Error: the provided Provider does not exist for Patient: " + fileLine[2]+", "+fileLine[1] +". Please review");
}
}
else if (fileLine[0].matches("Appointment")) {
fileLine = line.split("#");
if (fileLine.length < 7)
throw new IllegalArgumentException("Error: the appointment data in the file is incomplete. Please review");
if (!"true".equals(fileLine[5].toLowerCase()) && !"false".equals(fileLine[5].toLowerCase()))
throw new IllegalArgumentException("Error: the appointment data in the file is incorrect. Please review");
if (!"true".equals(fileLine[6].toLowerCase()) && !"false".equals(fileLine[6].toLowerCase()))
throw new IllegalArgumentException("Error: the appointment data in the file is incorrect. Please review");
//parse the fileLine parameters
calendar.setTime(df.parse(fileLine[3] + " " + fileLine[4]));
for (int i = 0; i < persons.size(); i++) {
if (persons.get(i).getMedicare().matches(fileLine[1])) {
for (int j = 0; j < persons.size(); j++) {
if (persons.get(j).getMedicare().matches(fileLine[2])) {
appointments.add(new Appointment((Patient) persons.get(i), (Provider) persons.get(j), calendar,
Boolean.parseBoolean(fileLine[5]), Boolean.parseBoolean(fileLine[6])));
throwError = true;
}
}
}
}
if (throwError!=true) {
throw new IllegalArgumentException("Error: the provided Provider or Patient does not exist in the system. Please review");
}
}
else
throw new IllegalArgumentException("Error: the data provided does not match a person, provider or appointment. Please review");
}
input.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException pe) {
// TODO Auto-generated catch block
throw new IllegalArgumentException("Error: the appointment date and time in the file is incorrect. Please review");
}
}
No comments:
Post a Comment