在图书馆图书类java作业中遇到问题

在图书馆图书类java作业中遇到问题,java,if-statement,methods,nested,Java,If Statement,Methods,Nested,我有一个作业,给我一个Date类和LibraryBook类的预先编写的驱动程序,我必须编写这些类,以便它们通过驱动程序中的一系列检查。我的问题出在一张支票上,它看我是否会因为按时归还一本书而被收取费用。显然,我应该收取0美元,但出于某种原因,我的程序显示为15美元。 以下是计算罚款的方法: public double getFineAmount(Date dateReturned){ double fine; Date dueDate = this.get

我有一个作业,给我一个Date类和LibraryBook类的预先编写的驱动程序,我必须编写这些类,以便它们通过驱动程序中的一系列检查。我的问题出在一张支票上,它看我是否会因为按时归还一本书而被收取费用。显然,我应该收取0美元,但出于某种原因,我的程序显示为15美元。 以下是计算罚款的方法:

    public double getFineAmount(Date dateReturned){
        double fine;
        Date dueDate = this.getDueDate();
        if(dateReturned.isOnOrBefore(dueDate)){
            fine = 0.00;
        }
        else{
            if(this.isFiction){
                fine = 1.2 * dueDate.getDaysUntil(dateReturned);
                if(fine > 15){
                    fine = 15.00;
                }
            }
            else{
                fine = 1.5 * dueDate.getDaysUntil(dateReturned);
                if(fine > 20){
                    fine = 20.00;
                }
            }
        }
        return fine;
    }
当图书的借阅日期为2012年2月10日,到期日期为2012年3月2日时,出于某种原因,isOnOrBefore方法返回false。以下是isOnOrBefore方法:

public boolean isOnOrBefore(Date other){
boolean onOrBefore;
Scanner sc = new Scanner(other.toString()).useDelimiter("-");
int otherDay = sc.nextInt();
int otherMonth = sc.nextInt();
int otherYear = sc.nextInt();
if(otherYear >= this.year){
    if(otherMonth >= this.month){
        if(otherDay >= this.day){
            onOrBefore = true;
        }
        else{
            onOrBefore = false;
        }
    }
    else{
        onOrBefore = false;
    }
}
else{
    onOrBefore = false;
}
return onOrBefore;
}
我相信这个问题是由闰年引起的,但我不知道是什么导致了这个错误。下面是检测闰年的代码,以防有所帮助

public boolean isLeapYear(){
boolean leapYear;
if(this.year % 100 == 0){
    if(this.year % 400 == 0){
    leapYear = true;
    }
    else{
    leapYear = false;
    }
}
else{
    if(this.year % 4 == 0){
    leapYear = true;
    }
    else{
    leapYear = false;
    }
}
return leapYear;
}

如果需要,我可以发布更多的代码。

您的闰年算法会向后看

if (year is not divisible by 4) then (it is a common year)    
else if (year is not divisible by 100) then (it is a leap year)    
else if (year is not divisible by 400) then (it is a common year)    
else (it is a leap year)

您可能还希望在if fine>X语句中强制转换为double,这样您就不会从double转换为int,并冒着舍入错误的风险。

如何调试代码?这才是真正的问题,应该如何回答。调试的方法是多种多样的,并且应该根据用例的不同而有所不同。在你的情况下,我会考虑如下:

System.out.println。。在可疑条件之前和之后的语句,以确定if语句的行为是否符合计划,以及进入它们的值是否符合预期。 将代码关键点的指定信息写入文件的日志文件,以便分析程序的整个执行流。看看Log4J。 在类似Eclipse的IDE中执行调试器。 提示:您是否按预期将日期传递到方法中

注意:您可以通过将boolean onOrBefore设置为false并简单地测试肯定条件(将其设置为true)来减少代码的长度(即代码块的数量)

让我们仔细看一下对isOnOrBefore的调用:


我希望这可能会帮助你,工程100%计算你的罚款

public static void main(String[] args) throws ParseException{

    Scanner date_1 = new Scanner ( System.in);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

  // lend Date- This is not required 
    System.out.println("Enter in the format: YYYY-MM-DD");
    String inputDate = date_1.next();
    Date lendDate = sdf.parse(inputDate);
    System.out.println(lendDate);
 // duedate   
    System.out.println("Enter DueDate in the format:");
    String inputDate1 = date_1.next();
    Date dueDate = sdf.parse(inputDate1);
    System.out.println(dueDate);
 //returnDate    
    System.out.println("Enter return Date");
    String inputDate2 = date_1.next();
    Date returnDate = sdf.parse(inputDate2);
    System.out.println(returnDate);

    long diff = returnDate.getTime() -  dueDate.getTime();

        long diffSeconds = diff / 1000 % 60;
        long diffMinutes = diff / (60 * 1000) % 60;
        long diffHours = diff / (60 * 60 * 1000) % 24;
        long diffDays = diff / (24 * 60 * 60 * 1000);
        // Fine amount should be declared or can be added as a user input(optional).    
        long fine = (diffDays)*10 ;
    // if the returnDate has exceeded the duedate                   
if (returnDate.after(dueDate)){
    System.out.print(diffDays + " days, ");
    System.out.print(diffHours + " hours, ");
    System.out.print(diffMinutes + " minutes, ");
    System.out.print(diffSeconds + " seconds." );
    System.out.println("");
    System.out.println("The Fine is" +fine);   }                    
else{
    System.out.println("Checked out"); }

欢迎来到StackOverflow!如果您添加一些代码工作原理的解释,您的答案可能会有很大改进。
public boolean isOnOrBefore(Date other) {
    if (other.year > this.year) { // 2012 > 2012 == false
        return true;
    }
    if (other.year < this.year) { // 2012 < 2012 == false
        return false;
    }

    // At this point, we know the two dates are in the same year.

    if (other.month > this.month) { // 3 > 2 == true
        return true; // return true
    }
    if (other.month < this.month) {
        return false;
    }

    // At this point, we know the two dates are in the same month *of the same year*

    return other.day >= this.day;
}
public static void main(String[] args) throws ParseException{

    Scanner date_1 = new Scanner ( System.in);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

  // lend Date- This is not required 
    System.out.println("Enter in the format: YYYY-MM-DD");
    String inputDate = date_1.next();
    Date lendDate = sdf.parse(inputDate);
    System.out.println(lendDate);
 // duedate   
    System.out.println("Enter DueDate in the format:");
    String inputDate1 = date_1.next();
    Date dueDate = sdf.parse(inputDate1);
    System.out.println(dueDate);
 //returnDate    
    System.out.println("Enter return Date");
    String inputDate2 = date_1.next();
    Date returnDate = sdf.parse(inputDate2);
    System.out.println(returnDate);

    long diff = returnDate.getTime() -  dueDate.getTime();

        long diffSeconds = diff / 1000 % 60;
        long diffMinutes = diff / (60 * 1000) % 60;
        long diffHours = diff / (60 * 60 * 1000) % 24;
        long diffDays = diff / (24 * 60 * 60 * 1000);
        // Fine amount should be declared or can be added as a user input(optional).    
        long fine = (diffDays)*10 ;
    // if the returnDate has exceeded the duedate                   
if (returnDate.after(dueDate)){
    System.out.print(diffDays + " days, ");
    System.out.print(diffHours + " hours, ");
    System.out.print(diffMinutes + " minutes, ");
    System.out.print(diffSeconds + " seconds." );
    System.out.println("");
    System.out.println("The Fine is" +fine);   }                    
else{
    System.out.println("Checked out"); }