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

时间:2016-10-26 04:25:29

标签: java bluej currency

我正在尝试弄清楚如何在我的boolean isAfter(Date compareTo)课程中调用Date课程中的Bill。我一直收到一条错误消息“无法找到符号 - 方法isAfter(Date)。我知道我的错误是什么。我是否必须在Date方法中创建一个新的setPaid对象?< / p>

以下是我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) {

}

}

1 个答案:

答案 0 :(得分:1)

您的方法名称中存在拼写错误。调用您的方法(注意大写 F ):

public boolean isAFter (Date compareTo) {}

当你打电话时(注意小写 f ):

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

旁注,不知道为什么你在这里创建一个新的Date

Date after = new Date(datePaid);

逻辑似乎是错误的,你基本上只是将日期与自身进行比较。您可能希望与dueDate进行比较?