如何在ArrayList中的Object内创建日历对象

时间:2012-04-10 10:08:35

标签: java arraylist gregorian-calendar

刚刚完成我的java任务并且我遇到了一堵砖墙我似乎无法找到解决方案 - 我不一定在寻找答案,甚至一些想法可以帮助我什么工具:)无论如何这里云:

正如标题所说,我希望在Arraylist中的对象中创建一个日历对象。基本上,根据下面的代码 - 我认为当创建一个Appointment对象的实例时,日历对象和约会对象之间的链接将被切断,我可以重用Calendar对象作为下一个约会对象。不幸的是,每个对象都保留对日历对象的引用,而不是创建自己的Calendar对象实例= /。

有关工作的背景知识:

基本上这段java代码扫描文件并从中提取信息,确保其有效然后在两个arraylists之一中创建适当对象的实例。我在我的导师的约束下工作,他指定我必须使用arraylist。任何帮助都会非常感激。

约会类的构造函数: 公共预约(患者患者,提供者提供者,GregorianCalendar日期,布尔标准,布尔参加者)

示例约会数据 预约#84736254193#123456AF#22.30第20 /2012分之12#假#真

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

2 个答案:

答案 0 :(得分:1)

你正在向每个约会发送相同的对象。如果我理解正确,您希望每个约会都有不同的日历对象。如果是这样,只需在每次创建约会时重新实例化日历,无论是在约会构造函数还是方法中......

编辑: 哦,我忘了,日历是单身人士。然后我建议只在约会中保留java.util.Date对象 - Calendar.getTime()创建Date的新实例。

然后你可以把它装扮成吸气的日历 -

public Calendar getAppointmentCalendar()
{
    Calendar cal = Calendar.getInstance();
    cal.setTime(this.appDate);
    return cal;
}

答案 1 :(得分:1)

问题是每次都会将相同的日历实例传递给构造函数。在for循环中实例化一个新的Calendar实例,您将约会添加到列表中。传递新实例可以解决您的问题。