Java 如何更新特定任务的截止日期

Java 如何更新特定任务的截止日期,java,Java,我有两门课:最后期限和任务。在Task类中,我想创建一个extendDeadline方法,它增加了任务的截止日期。我尝试了以下方法: public void extendDeadline(int extend){ deadline = deadline + extend; } 但这不起作用,因为截止日期的类型是截止日期。有人能帮我吗 截止日期类别代码: import java.text.DateFormat; import java.util.Date; public class

我有两门课:最后期限和任务。在Task类中,我想创建一个extendDeadline方法,它增加了任务的截止日期。我尝试了以下方法:

public void extendDeadline(int extend){

    deadline = deadline + extend;
}
但这不起作用,因为截止日期的类型是截止日期。有人能帮我吗

截止日期类别代码:

import java.text.DateFormat;
import java.util.Date;


public class Deadline {
    // dates are stored as the number of seconds since 01/01/1970
    private long deadline;
    // we have a DateFormat to format the date as text
    private DateFormat dateFormatter;

    // define some constants...
public static final long SECOND = 1000;
public static final long MINUTE = SECOND*60;
public static final long HOUR = MINUTE * 60;
public static final long DAY = HOUR*24;
public static final long WEEK = DAY*7;

/**
 * Construct a new deadline.  
 * By default, deadlines are one week ahead of the time now.
 */
public Deadline() {
    // by default you get a week
    deadline = now() + WEEK;
    dateFormatter = DateFormat.getDateInstance();
}

/**
 * @return the time now as a long
 */
private long now() {
    return new Date().getTime();
}

/**
 * Get the date of this deadline as a Date object.
 * @return the date of this deadline as a Date object.
 */
private Date getDeadlineDate() {
    return new Date(deadline);
}

/**
 * Change the date of this deadline by a specified number of days.
 * @param numDays the number of days to add to the deadline date (may be negative).
 */
public void setNewDeadline(int numDays) {
    deadline = deadline + (DAY*numDays);
}

/**
 * Find out if this deadline has passed.
 * @return true when the time now is later than the deadline.
 */
public boolean hasPassed() {
    return now() > deadline;
}

/**
 * Return this deadline formatted as text to be printed.
 * @return a string representation of this deadline.
 */
@Override
public String toString() {
    return dateFormatter.format(getDeadlineDate());
}

@Override
public boolean equals(Object o) {
    if (o instanceof Deadline) {
        Deadline other = (Deadline) o;
        if (this.toString().equals(other.toString())) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}
}

任务类的代码:

public class Task {
private int id;
private String description;
private Deadline deadline;
private boolean done;
private int estimatedLength;
private int hoursWork;

/**
 * Constructor for objects of class Task
 */
public Task(int id , String description, int estimatedLength) {
    this.description = description;
    this.id = id;
    this.estimatedLength = estimatedLength;
    deadline = new Deadline();
    done = false;
    hoursWork = hoursWork;

}

public int getId(){
    return id;
}

public Deadline getDeadline(){
    return deadline;
}

public String getDescription(){
    return description;
}

public boolean isDone(){
    return done;
}

public double getPercentageComplete(){ //make sure this is right
    double result = 0;
    double hoursWorkDouble = (double) hoursWork;
    double estimatedLengthDouble = (double) estimatedLength;
    result = (double) hoursWork / (double) estimatedLength * 100.0;
    System.out.println("Percentage complete: "+result);
    return result;

}

public boolean isLate(){
    if(done = false) { // dont forget to put AND date greater deadline
        return true;
    } else{
        return false;
    }

}

public void setDone(){
    this.done = true;
}

public void extendEstimate(int extendEstimate){
    estimatedLength = estimatedLength + extendEstimate;

    /*int extendEstimate= 0;
    int estimatedLength= 0;
    int result= extendEstimate + estimatedLength;
    System.out.println(+result);
     */

}

public void recordHoursWorked(int recordHours){
    hoursWork= hoursWork + recordHours;

}

public void extendDeadline(int extend){

}

}

看起来在Deadline类中有一个
setNewDeadline(int numDays)
方法。(这可能应该重命名为类似于
extendedDeadLineByDays
的名称,因为您不是在重置截止日期,而是在延长它)看起来您实际上想要做的是调用该方法。因此,在Task类中,方法应如下所示:

public void extendDeadline(int extend){
    deadline.setNewDeadline(extend);
}

在这种情况下,您正在更新截止日期,而不是尝试重新分配它。您试图做的是设定一个截止日期,并向其添加一个int,这是不可能的。

看起来您在Deadline类中有一个
setNewDeadline(int numDays)
方法。(这可能应该重命名为类似于
extendedDeadLineByDays
的名称,因为您不是在重置截止日期,而是在延长它)看起来您实际上想要做的是调用该方法。因此,在Task类中,方法应如下所示:

public void extendDeadline(int extend){
    deadline.setNewDeadline(extend);
}
在这种情况下,您正在更新截止日期,而不是尝试重新分配它。你要做的是设定一个截止日期,然后在上面加一个int,这是不可能的