2014-10-09 68 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