如何从不同的类调用比较两个值的方法



我正试图找出如何在我的Bill类中调用我的Date类中的boolean isAfter(Date compareTo)。我一直得到一个错误消息说"找不到符号-方法isAfter(日期)。我知道我所拥有的是不对的。我必须在我的setPaid方法中创建一个新的Date对象吗?

这里是我的Date类的方法-运行良好,整个Bill类-不。

public boolean isAFter (Date compareTo) {
    return compareTo(compareTo) > 0;
}
public int compareTo (Date x) {
    if (year != x.year) return year - x.year;
    if (month != x.month) return month - x.month;
    return day - x.day;
}
public class Bill
{
private Money amount;
private Date dueDate;
private Date paidDate;
private String originator;
//paidDate set to null
public Bill (Money amount, Date dueDate, String originator) {
    this.amount = amount;
    this.dueDate = dueDate;
    this.originator = originator;
    paidDate = null;
}
public Bill (Bill toCopy) {
    /*this.amount = toCopy.amount;
    this.dueDate = toCopy.dueDate;
    this.paidDate = toCopy.paidDate;
    this.originator = toCopy.originator;*/
}
public Money getAmount () {
    return amount;
}
public Date getDueDate () {
    return dueDate;
}
public String getOriginator () {
    return originator;
}
//returns true if bill is paid, else false
public boolean isPaid () {
    if (paidDate != null) { 
        return true;
    }
    return false;
}
//if datePaid is after the dueDate, the call does not update anything and returns false.
//Else updates the paidDate and returns true
//If already paid, we will attempt to change the paid date.
public boolean setPaid (Date datePaid) {
    Date after = new Date(datePaid);
    if (after.isAfter(datePaid) == true) {
        return false;
    }
    else {
        paidDate = datePaid;
        return true;
    }
}
//Resets the due date – If the bill is already paid, this call fails and returns false. 
//Else it resets the due date and returns true.
public boolean setDueDate (Date newDueDate) {
    if (isPaid() == false) {
        dueDate = newDueDate;
        return true;
    }
    else {
        return false;
    }
}
//Change the amount owed.
//If already paid returns false and does not change the amount owed else changes 
//the amount and returns true.
public boolean setAmount (Money amount) {
}
public void setOriginator () {
}
//Build a string that reports the amount, when due, to whom, if paid, and if paid 
//the date paid
public String toString () {
    return "Amount: " + amount + " Due date: " + dueDate + " To: " + "originator" + " Paid?" + isPaid() + "Paid date: " + paidDate; 
}
//Equality is defined as each field having the same value.
public boolean equals (Object toCompare) {
}

}

你的方法名有一个错别字。你的方法被调用(注意大写的F):

public boolean isAFter (Date compareTo) {}

当你调用它时(注意小写的f):

if (after.isAfter(datePaid) == true) {}

顺便说一句,我不知道你为什么要在这里创建一个新的Date:

Date after = new Date(datePaid);

逻辑似乎是错误的,你本质上只是比较日期本身。也许你想比较dueDate ?

最新更新