2012-04-10 3 views
0

그냥 내 Java 할당 작업을하고 벽돌 벽에 맞 혔습니다. 해결책을 찾지 못하는 것 같습니다. 반드시 대답을 찾지는 않습니다. 어떤 도구가 나를 도울 수있는 몇 가지 아이디어 :) 어쨌든 여기에 간다 :ArrayList 내에서 개체 내에 일정 개체를 만드는 방법

제목에서 말하지만, 나는 Arraylist 내의 객체 내에 일정 객체를 생성하려고한다. 기본적으로, 아래 코드에 따라 - 나는 Appointment 객체의 인스턴스가 생성 될 때 calendar 객체와 약속 객체 사이의 링크가 끊어지고 다음 약속 객체에 대한 Calendar 객체를 재사용 할 수 있다고 생각했습니다. 불행하게도 각 객체는 Calendar 객체의 자체 인스턴스를 만들지 않고 캘린더 객체에 대한 참조를 유지합니다.

작업의 일부 배경 :

는 기본적으로 자바 코드의 조각 파일을 스캔하고 그것에서 정보를 추출은 다음의 하나에 해당하는 개체의 인스턴스를 만들고 그것의 유효 확인합니다 두 arraylists. 나는 강사를 사용해야한다는 강사의 제약 속에서 일하고 있습니다. 어떤 도움이라도 대단히 감사하겠습니다.

약속 클래스의 생성자 : 공개 약속 (환자 환자, 제공자 제공, GregorianCalendar를 날짜, 부울 표준, 부울 참석)

예 약속 데이터 약속 # 84736254193 # 123456AF # 22.30 # 2012 년 12 월 20 일 # 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"); 
     } 
    } 

답변

1

잘 각 예약에 같은 개체를 보내고 있습니다. 올바르게 이해하면 각 약속마다 다른 달력 개체가 있어야합니다. 그렇다면 약속을 생성 할 때마다 약속 생성자 또는 메서드에서 일정을 다시 인스턴스화하십시오.

편집 : 오, 잊어 버렸습니다, 캘린더는 싱글 톤입니다. 그런 다음 Calendar에있는 java.util.Date 객체 만 유지하도록 제안합니다. Calendar.getTime()은 Date의 새 인스턴스를 만듭니다.

그럼 당신은 게터의 일정으로 입을 수 -

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

그런 간단한 수정을 부탁드립니다. 나는 아직도 당신이 객체를 참조하고 만드는 방법을 조작하는 방법을 고민하고있다. – Fallz

+0

당신은 환영합니다, 행운을 빈다. –

1

문제는 같은 일정 인스턴스마다 생성자에 전달지고 있다는 점이다. 약속을 목록에 추가 할 for 루프에서 새 Calendar 인스턴스를 인스턴스화합니다. 새 인스턴스를 전달하면 문제가 해결됩니다.

관련 문제