簡體   English   中英

如何在 ArrayList 中的 Object 中創建日歷 object

[英]How to create a calendar object within an Object within an ArrayList

剛剛完成我的 java 任務,我遇到了障礙,我似乎無法找到解決方案 - 我不一定要尋找答案,甚至是一些關於哪些工具可能對我有幫助的想法:) 不管怎樣,這里是:

正如標題所說,我希望在 Arraylist 內的 object 內創建一個日歷 object。基本上,按照下面的代碼 - 我認為當創建約會 object 的實例時,日歷 object 和約會 85828229 之間的鏈接將被切斷,我可以將日歷 object 重新用於下一個約會 object。不幸的是,每個 object 都保留其對日歷 object 的引用,而不是創建其自己的日歷 object =/ 實例。

一些工作背景:

基本上這段 java 代碼,掃描文件並從中提取信息,確保其有效,然后在兩個數組列表之一中創建適當的 object 的實例。 我在我的導師的限制下工作,他指定我必須使用 arraylist。任何幫助將不勝感激。

約會 class 的構造函數: public Appointment (Patient patient, Provider provider, GregorianCalendar date, boolean standard, boolean attended)

示例約會數據約會#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");
        }
    }   

好吧,您要向每個約會發送相同的 object。 如果我理解正確,您希望每個約會都有不同的日歷 object。如果是這樣,只需在每次創建約會時在約會構造函數或您的方法中重新實例化日歷...

編輯:哦,我忘了,Calendar 是 singleton。然后我建議只保留 java.util.Date object 在約會中 - Calendar.getTime() 創建 Date 的新實例。

然后你可以在 getter 中把它裝扮成 Calendar -

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

問題是每次都將相同的日歷實例傳遞給構造函數。 在要將約會添加到列表的 for 循環內實例化一個新的 Calendar 實例。 傳遞一個新實例將解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM