java - 我如何建模这样的东西? (科室与老师的关系)

标签 java oop model

我有这份作业需要完成,但是其中的一些事情确实困扰着我。我一直在思考这个问题,但我认为我的方法是有缺陷的。嗯,确实如此,因为我尝试过,它只是给了我一个 stackoverflow 异常。不管怎样,这里是导师给我的规范片段:

  • A section is defined as a combination of a teacher, a subject and a schedule.
  • A section can accommodate a maximum of forty (40) students.
  • Each subject is worth three (3) units. A teacher is identified by his/her Faculty ID.
  • A teacher cannot teach two sections with the same schedule.

我的老师类(class)有他/她所教的部分的列表。我计划如果我创建一个部分,构造函数必须首先检查该部分的时间表是否与列表中的时间表冲突。如果不存在冲突,则该部分已成功创建,并且该部分必须添加到教师拥有的部分列表中。我想我的逻辑是好的,但我的实现却不行!

这是我根据上面的描述创建的类:

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

我的老师类(class):

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

最佳答案

您正确地判断了哪一行导致了 StackoverflowException。该行:

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

会导致无限循环,因为每次传递相同的参数时都会发生相同的事情。根据呆伯特的说法,在特定的情况下重复同样的事情并期望不同的结果是精神错乱的表现:)

替换为:

teacher.addSection(this);

关于java - 我如何建模这样的东西? (科室与老师的关系),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8805734/

相关文章:

java - 使用java获取一条记录的多个连接VS获取Mysql中所有记录的一个连接

java - 我正在尝试根据图表值获取图表网页。但

qt - QML 中 ItemSelectionModel 的目的和用法

java - Servlet 不刷新 html 页面

java - 在opengl es 2.0中绘制纹理

oop - 面向对象编程背后的理论

java - 如何更新 if block 中的实例变量?

c - C中的动态方法调度

java - swing组件的模型如何请求重绘?

node.js - SLC环回: Using model instance from within a model hook