我有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 :(得分:0)
您的截止日期课程中似乎有setNewDeadline(int numDays)
方法。 (这应该可以重命名为extendDeadlineByDays
,因为你没有重置截止日期,只是扩展它。看起来你真正想做的就是调用那个方法。因此,在Task类中,该方法应如下所示:
public void extendDeadline(int extend){
deadline.setNewDeadline(extend);
}
在这种情况下,您要更新截止日期,而不是尝试重新分配截止日期。你想要做的是采取截止日期,并添加一个int,这是不可能的。