0

只是在处理我的 Java 作业,但我遇到了一堵砖墙,我似乎无法找到解决方案 - 我不一定要寻找答案,甚至对哪些工具可能对我有帮助的一些想法:) 无论如何,这里有:

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

一些工作背景:

基本上,这段 java 代码扫描文件并从中提取信息,确保其有效,然后在两个数组列表之一中创建适当对象的实例。我在我的导师的限制下工作,他指定我必须使用数组列表。任何帮助将不胜感激。

预约类的构造函数: public Appointment(Patient patient, Provider provider, GregorianCalendar date, boolean standard, boolean Attended)

约会数据示例 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");
        }
    }   
4

2 回答 2

1

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

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

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

public Calendar getAppointmentCalendar()
{
    Calendar cal = Calendar.getInstance();
    cal.setTime(this.appDate);
    return cal;
}
于 2012-04-10T10:18:24.730 回答
1

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

于 2012-04-10T10:25:44.820 回答