2012-01-10 5 views
0

나는이 숙제를 끝내야한다. 그러나 그것에 관해 무엇인가 정말로 나를 괴롭 히고있다. 나는 이것에 대해 생각 해왔다. 그러나 나는 나의 접근법에 결함이 있다고 생각한다. 글쎄, 정말, 내가 그것을 시도하고, 그것은 단지 stackoverflow 예외 thingy 준. 섹션은 교사, 제목과 일정의 조합으로 정의된다어떻게 이런 모델을 만들 수 있습니까? (섹션 - 교사 관계)

  • : 어쨌든, 여기에 멘토 나에게 주어진 사양의 조각이다.
  • 섹션에는 최대 40 명의 학생을 수용 할 수 있습니다.
  • 각 과목의 가치는 3 과목입니다. 교사는 교직원 ID로 식별됩니다.
  • 교사는 동일한 일정으로 두 섹션을 가르 칠 수 없습니다.

내 선생님 수업에 저에게 가르치는 부분 목록이 있습니다. 섹션을 만들면 해당 섹션의 일정이 목록의 일정과 충돌하는지 먼저 확인해야합니다. 충돌이 없으면 섹션이 성공적으로 만들어지고 해당 섹션이 교사가 가지고있는 섹션 목록에 추가되어야합니다. 내 논리는 괜찮은 것 같지만 내 구현은 그렇지 않습니다!

import java.util.List; 

import org.apache.commons.lang3.Validate; 

public class Section { 

    //A section is defined as a combination of a teacher, a subject and a schedule. 
    private Teacher teacher; 
    private Subject subject; 
    private Schedule schedule; 

    private String sectionName; 

    //A section can accommodate a maximum of forty (40) students. 
    private int numOfStudentsItCanAccomodate = 40; 

    public Section(Teacher teacher, Subject subject, Schedule schedule, 
      String sectionName){ 

      this.teacher = teacher; 
      this.subject = subject; 
      this.schedule = schedule; 
      this.sectionName = sectionName; 

      Validate.notNull(teacher,"Teacher field"+Prompts.FAILED_PROMPT_NULL.getMessage()); 
      Validate.notNull(subject,"Subject field"+Prompts.FAILED_PROMPT_NULL.getMessage()); 
      Validate.notNull(schedule,"Schedule field"+Prompts.FAILED_PROMPT_NULL.getMessage()); 
      Validate.notNull(sectionName,"Section name"+Prompts.FAILED_PROMPT_NULL.getMessage()); 

      Validate.notBlank(sectionName,"Section name"+Prompts.FAILED_PROMPT_BLANK.getMessage()); 

      if(sectionConflictsWithTeachersOtherSections(teacher,schedule)==true){ 
       throw new IllegalArgumentException(Prompts.FAILED_PROMPT_TEACHER.getMessage()); 
      }else{ 
       //**I believe this line faulty** No! It is faulty and stupid of me. T_T 
       teacher.addSection(new Section (teacher,subject,schedule,sectionName)); 

      } 
    } 

    public Teacher getTeacher() { 
     return teacher; 
    } 

    public Subject getSubject() { 
     return subject; 
    } 

    public Schedule getSchedule() { 
     return schedule; 
    } 

    public String getSectionName() { 
     return sectionName; 
    } 

    //A teacher cannot teach two sections with the same schedule. 
    private boolean sectionConflictsWithTeachersOtherSections(Teacher teacher,Schedule newSchedule){ 
     boolean conflict = false; 
     List<Section> teachersListOfSections = teacher.getListOfSections(); 

     if(teacher.getListOfSections().size()>0){ 
     for(Section takenSection:teachersListOfSections){ 
      Schedule scheduleOfTakenSection = takenSection.getSchedule(); 

      if(scheduleOfTakenSection.getDay().equals(newSchedule.getDay()) && 
       scheduleOfTakenSection.getTime().equals(newSchedule.getTime())){ 
       conflict = true; 
       } 
      } 
     } 

     return conflict; 
    } 

    public void subtractNumOfStudentsItCanAccomodate(){ 
     this.numOfStudentsItCanAccomodate--; 
    } 

    public int getNumOfStudentsItCanAccomodate() { 
     return numOfStudentsItCanAccomodate; 
    } 

    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + numOfStudentsItCanAccomodate; 
     result = prime * result 
       + ((schedule == null) ? 0 : schedule.hashCode()); 
     result = prime * result 
       + ((sectionName == null) ? 0 : sectionName.hashCode()); 
     result = prime * result + ((subject == null) ? 0 : subject.hashCode()); 
     result = prime * result + ((teacher == null) ? 0 : teacher.hashCode()); 
     return result; 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) 
      return true; 
     if (obj == null) 
      return false; 
     if (getClass() != obj.getClass()) 
      return false; 
     Section other = (Section) obj; 
     if (numOfStudentsItCanAccomodate != other.numOfStudentsItCanAccomodate) 
      return false; 
     if (schedule == null) { 
      if (other.schedule != null) 
       return false; 
     } else if (!schedule.equals(other.schedule)) 
      return false; 
     if (sectionName == null) { 
      if (other.sectionName != null) 
       return false; 
     } else if (!sectionName.equals(other.sectionName)) 
      return false; 
     if (subject == null) { 
      if (other.subject != null) 
       return false; 
     } else if (!subject.equals(other.subject)) 
      return false; 
     if (teacher == null) { 
      if (other.teacher != null) 
       return false; 
     } else if (!teacher.equals(other.teacher)) 
      return false; 
     return true; 
    } 

    public boolean conflictsDayWith(Section newSection){ 
     return (this.schedule.getDay().equals(newSection.schedule.getDay())); 
    } 

    public boolean conflictsTimeWith(Section newSection){ 
     return (this.schedule.getTime().equals(newSection.schedule.getTime())); 
    } 
} 

선생님 클래스 :

public class Teacher extends Person{ 

    private List<Section> listOfSections; 

    public Teacher(String firstName,String lastName, String middleName) 
        throws IllegalArgumentException{ 
     this.firstName = firstName; 
     this.lastName = lastName; 
     this.middleName = middleName; 
     this.listOfSections = new ArrayList<Section>(); 

     Validate.notNull(firstName, "First name "+Prompts.FAILED_PROMPT_NULL.getMessage()); 
     Validate.notNull(lastName, "Last name"+Prompts.FAILED_PROMPT_NULL.getMessage()); 
     Validate.notNull(middleName, "Middle name"+Prompts.FAILED_PROMPT_NULL.getMessage()); 
    } 

    public void addSectionToList(Section newSection){ 
     listOfSections.add(newSection); 
    } 

    public void teachesSection(Section newSection){ 
     this.listOfSections.add(newSection); 
    } 

    public List<Section> getListOfSections() { 
     return listOfSections; 
    } 

    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = super.hashCode(); 
     result = prime * result 
       + ((listOfSections == null) ? 0 : listOfSections.hashCode()); 
     return result; 
    } 

    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) 
      return true; 
     if (!super.equals(obj)) 
      return false; 
     if (getClass() != obj.getClass()) 
      return false; 
     Teacher other = (Teacher) obj; 
     if (listOfSections == null) { 
      if (other.listOfSections != null) 
       return false; 
     } else if (!listOfSections.equals(other.listOfSections)) 
      return false; 
     return true; 
    } 

    @Override 
    public String toString(){ 
     return firstName + " " + middleName + " " + lastName ; 
    } 
} 
+1

이 UML을위한 것입니다. 또한, [정확한 복제] (http://stackoverflow.com/questions/8730313/how-do-i-model-something-like-this). – mre

+1

예외가 발생하면 스택 추적을 게시하는 것이 좋습니다. –

+0

@ 그들이 실제로 관련되어 있습니다. : D 나는 그 질문을 몇 주 전에 게시 한 사람입니다. 그러나 그 질문은 완전히 다른 문제입니다. :) –

답변

1

당신은있는 라인이있는 StackOverflowException의 원인이 올바른지 여기

내가 위의 설명에 따라 만든 클래스입니다. 줄이 :

teacher.addSection(new Section (teacher,subject,schedule,sectionName)); 

같은 매개 변수를 전달할 때마다 동일한 일이 발생하므로 infinte 루프가 발생합니다. 정확한 상황에서 같은 일을 반복하고 다른 결과를 기대하는 것은 딜버트에 따라 광기의 표시입니다 :)

이로 교체 :

teacher.addSection(this); 
+0

고마워! :디 –

관련 문제