[IBook] #java
import tester.Tester;
interface IBook{
//how many days left?
public int daysOverdue();
public boolean isOverdue();
public int computeFine();
}
abstract class ABook implements IBook{
String title;
ABook(String title){
this.title = title;
}
//
public abstract int daysOverdue();
public abstract boolean isOverdue();
public abstract int computeFine();
}
class Book extends ABook{
String author;
int dayTaken;
Book(String title, int dayTaken,String author){
super(title);
this.dayTaken = dayTaken;
this.author = author;
}
/* tmpl
* fields:
* this.author ... String
* this.dayTaken ... int
* methods:
* this.daysOverdue() ... int
*/
public int daysOverdue(){
return this.dayTaken - 14;
}
public boolean isOverdue(){
return this.daysOverdue() > 0;
}
public int computeFine(){
if (this.isOverdue()){
return (this.daysOverdue() * 10);}
else {return 0; }
}
}
class RefBook extends ABook{
int dayTaken;
RefBook(String title, int dayTaken){
super(title);
this.dayTaken = dayTaken;
}
public int daysOverdue(){
return this.dayTaken - 2;
}
public boolean isOverdue(){
return this.daysOverdue() > 0;
}
public int computeFine(){
if (this.isOverdue()){
return (this.daysOverdue() * 10);}
else {return 0; }
}
}
class AudioBook extends ABook{
String author;
int dayTaken;
AudioBook(String title, int dayTaken,String author){
super(title);
this.dayTaken = dayTaken;
this.author = author;
}
public int daysOverdue(){
return this.dayTaken - 14;
}
public boolean isOverdue(){
return this.daysOverdue() > 0;
}
public int computeFine(){
if (this.isOverdue()){
return (this.daysOverdue() * 20);}
else {return 0; }
}
}
class ExamplesBook{
IBook htdp = new Book("htdp",10,"harry");
boolean testBook(Tester t)
{return t.checkExpect (htdp.daysOverdue(),-4)&&
t.checkExpect (htdp.isOverdue(),false)&&
t.checkExpect (htdp.computeFine(),0);}
}