2012-04-10 62 views
0

只是在我的java任務上工作,我碰到了一堵磚牆,我似乎找不到解決方案 - 我不一定在尋找答案,甚至一些想法,以什麼工具可以幫助我:)無論如何這裏去:如何在ArrayList中的對象內創建日曆對象

正如標題所說,我正在尋找在Arraylist內的對象內創建一個日曆對象。基本上,按照下面的代碼 - 我認爲,當約會對象的實例創建時,日曆對象和約會對象之間的鏈接將被切斷,我可以重複使用日曆對象的下一個約會對象。不幸的是,每個對象都保留對日曆對象的引用,而不是創建自己的日曆對象實例= /。

上工作的一些背景:

基本上這一塊的Java代碼,掃描文件,並提取出的信息的話,可以確保其有效的,那麼的一個內創建相應對象的實例兩個陣列表。我在我的導師的約束下工作,他指定我必須使用數組列表。任何幫助將不勝感激。

的委任類的構造函數: 公共預約(患者的患者,提供供應商,GregorianCalendar的日期,布爾標準,布爾出席)

實例約會數據 任命#84736254193#123456AF#22.30# 2012年12月20日#false#真

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

回答

1

好,你發送同樣的對象到每個約會。如果我理解正確,您希望每個約會都有不同的日曆對象。如果是這樣,只要重新創建日曆,每次約會創建,無論是在約會的構造函數或你的方法...

編輯: 哦,我忘了,日曆是單身。然後我建議只保留一個約會中的java.util.Date對象 - Calendar.getTime()創建Date的新實例。

然後你就可以在吸氣穿着它作爲日曆 -

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

這樣一個簡單的修復 - 謝謝。我仍然想方設法操縱如何引用和創建對象。 – Fallz 2012-04-10 10:30:20

+0

不客氣,祝你好運 – 2012-04-10 10:35:03

1

的問題是,同一日曆實例獲得通過每次構造函數。在要將約會添加到列表的for循環中實例化新的Calendar實例。傳遞一個新實例將解決您的問題。