【问题标题】:Having trouble with a library book class java assignment图书馆书籍类java作业遇到问题
【发布时间】:2014-10-09 21:31:17
【问题描述】:

我有一个任务给我一个预先编写的用于 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;
}

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

【问题讨论】:

    标签: java if-statement methods nested


    【解决方案1】:

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

    1. System.out.println (..) 语句在可疑条件之前和之后确定 if 语句是否按计划运行以及输入它们的值是否符合预期。
    2. 日志文件将代码中关键点的指定信息写入文件,以便分析程序的整个执行流程。看看 Log4J。
    3. 在 Eclipse 等 IDE 中执行调试器。

    提示:您是否按预期将日期传递给方法?

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

    【讨论】:

      【解决方案2】:

      让我们仔细看看对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;
      }
      

      【讨论】:

        【解决方案3】:

        您的闰年算法向后看。

        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)”语句中强制转换为双精度,这样您就不会从双精度转换为整数并冒着舍入错误的风险。

        【讨论】:

          【解决方案4】:

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

          【讨论】:

          • 欢迎来到 *!如果您添加一些关于您的代码如何工作的解释,您的答案可能会得到很大改进。
          最近更新 更多