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