有图书馆书类java任务的麻烦

时间:2014-10-09 21:31:18

标签: java if-statement methods nested

我有一个任务,我给了一个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;
}

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

4 个答案:

答案 0 :(得分:1)

如何调试代码?这是真正的问题,这就是应该如何回答的问题。调试方法各不相同,应根据用例而有所不同。在您的情况下,我会考虑以下内容:

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

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

答案 1 :(得分:1)

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

// if <Feb. 10 2012>.isOnOrBefore(<Mar. 2 2012>))

public boolean isOnOrBefore(Date other) {
    boolean onOrBefore;
    Scanner sc = new Scanner(other.toString()).useDelimiter("-");
    int otherDay = sc.nextInt(); // 2
    int otherMonth = sc.nextInt(); // 3, assuming 1-indexed months
    int otherYear = sc.nextInt(); // 2012
    if(otherYear >= this.year) { // 2012 >= 2012 == true
        if(otherMonth >= this.month) { // 3 >= 2 == true
            if(otherDay >= this.day) { // 2 >= 10 == false
                onOrBefore = true;
            }
            else {
                onOrBefore = false; // onOrBefore == false
            }
        }
        else {
            onOrBefore = false;
        }
    }
    else {
        onOrBefore = false;
    }
    return onOrBefore; // return false
}

问题是您应该只检查这个日期和另一个日期是否在同一个月。 (同样地,你应该只检查他们在同一年的月份。)

尝试此操作(请注意,即使日/月/年字段为私有字段,您也可以other访问它们,因为您还在Date类中):

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;
}

答案 2 :(得分:0)

您的闰年算法向后看。

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&gt; X)”语句中强制转换为double,这样您就不会从double转换为int并冒险完成错误。 < / p>

答案 3 :(得分:0)

我希望这可以帮助你,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"); }