我有一個任務,我給了一個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;
}
如果需要,我可以發佈更多的代碼。
歡迎來到StackOverflow!如果您添加了關於代碼如何工作的解釋,您的答案可能會有所改進。 – 2016-02-02 19:27:38