2014-10-09 70 views
-1

我有一个任务,我给了一个Date类和LibraryBook类的预先编写的驱动程序,我必须编写这些类,以便他们通过一堆检查在驱动程序中。我的问题出现在其中一张支票上,看看我是否因按时还书而被起诉。显然我应该收取0美元,但由于某种原因,我的程序说15美元。 下面是计算罚款的方法:与图书馆书籍类Java分配有困难

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

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

回答

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) 

您也可能要转换为双在“如果(精> X)”语句,这样,你是不是从double转换成int和冒着舍入误差。

1

如何调试代码?这是真正的问题,这就是应该如何回答的。调试的方法各不相同,应根据使用情况而有所不同。在你的情况,我会考虑以下几点:之前和犯罪嫌疑人后,条件语句

  1. 的System.out.println(..)语句,以确定if语句表现为计划和值是否进入他们如预期。
  2. 将代码中关键点的指定信息写入文件的日志文件,以便可以分析程序的整个执行流程。看看Log4J。
  3. 调试器在像Eclipse这样的IDE中执行。

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

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

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; 
} 
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"); } 
+0

欢迎来到StackOverflow!如果您添加了关于代码如何工作的解释,您的答案可能会有所改进。 – 2016-02-02 19:27:38