我有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){
}
}
最佳答案
看来您的Deadline类中有一个setNewDeadline(int numDays)
方法。 (这可能应该重命名为extendDeadlineByDays
之类的,因为您没有重置截止日期,只是延长了截止日期)看起来您实际上想要做的就是调用该方法。因此,在Task类中,方法应如下所示:
public void extendDeadline(int extend){
deadline.setNewDeadline(extend);
}
在这种情况下,您要更新截止日期,而不是尝试重新分配它。您试图做的是获取截止日期,并向其添加整数,这是不可能的。
关于java - 如何更新特定任务的截止日期,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33790778/