将代码添加到预先存在的方法

时间:2012-01-30 02:15:06

标签: java methods

只是很难添加一定数量的给定代码来为预先存在的方法添加额外的检查。

public boolean makeAppointment(int time,
                               Appointment appointment)
{
    if(validTime(time)) {
        int startTime = time - START_OF_DAY;
        if(appointments[startTime] == null) {
            int duration = appointment.getDuration();
            // Fill in all the slots for the full duration
            // of the appointment.
            for(int i = 0; i < duration; i++) {
                appointments[startTime + i] = appointment;
            }
            return true;
        }
        else {

            return false;
        }
    }
    else {
        return false;
    }
}

是主要的makeAppointment方法。我需要添加一个方法来检查一个指定的持续时间是否与另一个指定的时间重叠。如果我有两个约会:1个是下午5点,持续2个小时,另一个是下午6点,只持续一个小时。我需要在这个预先存在的代码中添加以下方法,以防止这种情况发生。

private boolean checkMultihourAppointment(int startTime, int duration){

这就是它的标题。我试图实现它,但它最终失败了每次Junit测试。

需要更多信息,请询问:)

4 个答案:

答案 0 :(得分:0)

简化的代码更易于使用:代码转向不是一个多么棒的图表。

public boolean makeAppointment(int time, Appointment appointment) {
    if (!validTime(time)) {
        return false;
    }

    int startTime = time - START_OF_DAY;
    if (appointments[startTime] != null) {
        return false;
    }

    int duration = appointment.getDuration();

    // Here's where the new code goes
    if (appointmentOverlaps(time, duration)) {
        return false;
    }

    for (int i = 0; i < duration; i++) {
        appointments[startTime + i] = appointment;
    }

    return true;
}

appointmentOverlaps会检查duration的下一个null预约空档(空):

// Pseudo-code
public boolean appointmentOverlaps(int time, int duration) {
    while (appointment still going) {
        if (next slot filled) return true;
    }

    return false;
}

考虑返回某种状态,以便调用者知道为什么约会被拒绝。

appointmentOverlaps还应该处理现有代码的startTime检查,进一步简化。

答案 1 :(得分:0)

这是你想要的吗?重写现有代码以添加checkmethod。

public boolean makeAppointment(int time,
                               Appointment appointment)
{
    if(validTime(time)) {
        int startTime = time - START_OF_DAY;
        int duration = appointment.getDuration();
        if(checkMultihourAppointment(startTime, duration)) {
            // Fill in all the slots for the full duration
            // of the appointment.
            for(int i = 0; i < duration; i++) {
                appointments[startTime + i] = appointment;
            }
            return true;
        }
        else {

            return false;
        }
    }
    else {
        return false;
    }
}

private boolean checkMultihourAppointment(int startTime, int duration){
    for(int i = 0; i < duration; i++) {
          if(appointments[startTime + i] != null) return false;
    }
    return true;
}

答案 2 :(得分:0)

private boolean checkMultihourAppointment(int startTime, int duration){

    for (int i = startTime; i <= startTime + duration; i++){

        if (appointments[i] != null)
            return false;

    }

    return true;

}

答案 3 :(得分:0)

做得恰当......

第1步

采用良好的面向对象设计并向Appointment添加检查与其他Appointment重叠的方法,因此实现隐藏在 Appointment它所属的类

public class Appointment {
    Date start;
    int minutesDuration; // recommend change this to a Date

    public boolean overlaps(Appointment other) {
        // Check for any overlap by comparing opposite ends of each other
        return start.before(other.getEnd()) && getEnd().after(other.start);
    }

    private Date getEnd() {
        return new Date(start + minutesDuration * 60 * 1000);
    }
}

第2步

在您的代码中使用此方法:

if (appointment.overlaps(other)) {
    // do something
}