2015-11-18 14 views
1

我有2個課程:截止日期和任務。在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){ 

} 

}

回答

0

它看起來就像你在你的最後期限類有setNewDeadline(int numDays)方法。 (這可能應該重新命名爲extendDeadlineByDays,因爲你沒有重置截止日期,只是擴展它)看起來你實際上想要做的就是調用該方法。所以,在任務類,方法應該是這樣的:

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

在這種情況下,你要更新的最後期限,而不是試圖將其重新分配。你試圖做的是採取一個截止日期,並添加一個int,這是不可能的。

+0

謝謝,幫助很多 –