簡體   English   中英

面向對象設計中日期驗證的最佳方法

[英]Best approach for a date validation in object oriented design

我正在嘗試找出驗證日期的最佳方法。 我正在通過使用對象構造此日期。

public Appointment(String description , AppointmentDate appointmentDate)
{
    this.description = description; 
    this.appointmentDate = appointmentDate;
}

這只是一個簡單的構造函數,它使用約會數據中的信息來創建約會。

public AppointmentDate(Date startTime,Date endTime,Date appDate){
    this.startTime = startTime;
    this.endTime = endTime;
    this.appDate = appDate; 

}

然后是約會約會構造函數,該構造函數在約會構造函數的參數中傳遞。

我傾向於使用isLenient()方法來檢查用戶輸入的日期是否有效,但我很好奇這樣做可能會更簡單

public void add(Appointment a) 
{
    try
    {
        a.setLenient(false);
        appointmentCalender.add(a);
    }
    catch(Exception ex)
    {
        System.out.println("Invalid Date");
    }
}

我會這樣做,這是我的邏輯,但是您可以從中得到一個想法

public void add(Appointment a) 
{
    if (dateValidate(a)){
        appointmentCalender.add(a);
    }else{
    System.out.println("Invalid Date");
    }
}

public boolean dateValidate(Appointment a){

    if(a.getAppointmentDate().getStartTime().after(a.getAppointmentDate().getAppDate())){
        if(a.getAppointmentDate().getEndTime().after(a.getAppointmentDate().getStartTime())){
            return true;
        }
    }
    return false;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM