package person;
import video.Video;
import book.Book;
import book.Loanable;
import membership.*;
public class Member extends Person {
Membership membertype_;//Member's membership type
private int nBooks_;
private int nVideos_;
public Member(String name,Membership membertype)
{
super(name);
membertype_= membertype;
nBooks_=0;
}
public boolean borrow(Book b)
{
if(b.isLoanable() )
{
Loanable l = (Loanable) b;
if (membertype_.canBorrow(l,nBooks_)){
nBooks_++;
return true;
}
}
return false;
}
public boolean borrow(Video v)
{
if(membertype_.canBorrow(v,nVideos_))
{
nVideos_++;
return true;
}
return false;
}
public String getDetails() {
// TODO Auto-generated method stub
return null;
}
public String toString()
{
return super.toString() + "--Membership type:"+membertype_.toString();
}
}
Sunday, 19 September 2010
Adding video with books to library
Its better to have items...
package item;
public class Item {
String title_;
public Item (String title)
{
title_ = title;
}
public boolean isLoanable()
{
return false;
}
public String details ()
{
return "Title:"+title_;
}
public String toString()
{
return "Title:"+title_;
}
}
changing the book class
package book;
import item.Item;
public abstract class Book extends Item
{
String author_;
double price_;
int category_, subCategory_;
public Book (String title, String author, double price, int category, int subCategory)
{
super(title);
author_ = author;
price_ = price;
category_ = category;
subCategory_ = subCategory;
}
public String getDetails ()
{
return "hello:";
}
public String toString()
{
return super.toString()+"--Author:"+author_+"--price:"+price_+"--Category:"+category_+"--SubCategory:"+subCategory_;
}
public boolean isLoanable()
{
return super.isLoanable(); //by default returns false
}
}
public class LoanBook extends Book implements Loanable
{
public LoanBook (String title, String author, double price, int category, int subCategory)
{
super(title,author,price,category,subCategory);
}
public boolean canLoan()
{
return true;
}
public String details ()
{
return "Loan: "+super.getDetails();
}
public String toString()
{
return super.toString() + "--Loanable Book";
}
public boolean isLoanable()
{
return true;
}
}
public class LoanBook extends Book implements Loanable
{
public LoanBook (String title, String author, double price, int category, int subCategory)
{
super(title,author,price,category,subCategory);
}
public boolean canLoan()
{
return true;
}
public String details ()
{
return "Loan: "+super.getDetails();
}
public String toString()
{
return super.toString() + "--Loanable Book";
}
public boolean isLoanable()
{
return true;
}
}
package book;
public class ReferenceBook extends Book
{
public ReferenceBook (String title, String author, double price, int category, int subCategory)
{
super(title,author,price,category,subCategory);
}
public boolean canLoan()
{
return false;
}
public String details ()
{
return "Reference: "+super.getDetails();
}
public String toString()
{
return super.toString()+"--Reference Book";
}
public boolean isLoanable()
{
return false;
}
}
package item;
public class Item {
String title_;
public Item (String title)
{
title_ = title;
}
public boolean isLoanable()
{
return false;
}
public String details ()
{
return "Title:"+title_;
}
public String toString()
{
return "Title:"+title_;
}
}
changing the book class
package book;
import item.Item;
public abstract class Book extends Item
{
String author_;
double price_;
int category_, subCategory_;
public Book (String title, String author, double price, int category, int subCategory)
{
super(title);
author_ = author;
price_ = price;
category_ = category;
subCategory_ = subCategory;
}
public String getDetails ()
{
return "hello:";
}
public String toString()
{
return super.toString()+"--Author:"+author_+"--price:"+price_+"--Category:"+category_+"--SubCategory:"+subCategory_;
}
public boolean isLoanable()
{
return super.isLoanable(); //by default returns false
}
}
public class LoanBook extends Book implements Loanable
{
public LoanBook (String title, String author, double price, int category, int subCategory)
{
super(title,author,price,category,subCategory);
}
public boolean canLoan()
{
return true;
}
public String details ()
{
return "Loan: "+super.getDetails();
}
public String toString()
{
return super.toString() + "--Loanable Book";
}
public boolean isLoanable()
{
return true;
}
}
public class LoanBook extends Book implements Loanable
{
public LoanBook (String title, String author, double price, int category, int subCategory)
{
super(title,author,price,category,subCategory);
}
public boolean canLoan()
{
return true;
}
public String details ()
{
return "Loan: "+super.getDetails();
}
public String toString()
{
return super.toString() + "--Loanable Book";
}
public boolean isLoanable()
{
return true;
}
}
package book;
public class ReferenceBook extends Book
{
public ReferenceBook (String title, String author, double price, int category, int subCategory)
{
super(title,author,price,category,subCategory);
}
public boolean canLoan()
{
return false;
}
public String details ()
{
return "Reference: "+super.getDetails();
}
public String toString()
{
return super.toString()+"--Reference Book";
}
public boolean isLoanable()
{
return false;
}
}
getting the loanable interface
package book;
public interface Loanable {
boolean isLoanable();
}
public interface Loanable {
boolean isLoanable();
}
Membership.java
package membership;
import video.Video;
import book.Book;
import book.LoanBook;
import book.Loanable;
public abstract class Membership
{
public boolean canBorrow(Loanable item,int nItems)
{
return false;
}
}
package membership;
import book.Loanable;
public class StandardMembership extends Membership
{
private final int nMaxBooks=6;
private final int nMaxVideos = 1;
public String toString()
{
return "Standard";
}
public boolean canBorrow(Loanable item,int nItems) {
return Membership.canBorrow(item,nItems,nMaxBooks,nMaxVideos);
}
}
package membership;
import java.util.Calendar;
import book.LoanBook;
import book.Loanable;
public class OapMembership extends Membership
{
int nMaxVideos = 1;
public boolean canBorrow(Loanable item, int nItems)
{
Calendar presentDay = Calendar.getInstance();
int day = presentDay.get(Calendar.DAY_OF_WEEK);
// if (day == Calendar.SATURDAY || day==Calendar.SUNDAY)
// return false;
if(item instanceof LoanBook)
return true;
if(item instanceof Loanable)
return nItems<nMaxVideos;
return false;
}
public String toString()
{
return "OAP";
}
}
package membership;
import video.Video;
import book.LoanBook;
import book.Loanable;
public class JuniorMembership extends Membership {
private final int nMaxBooks = 1;
private final int nMaxVideos = 0;
public String toString() {
return "Junior";
}
public boolean canBorrow(Loanable item, int nItems) {
return Membership.canBorrow(item,nItems,nMaxBooks,nMaxVideos);
}
}
import video.Video;
import book.Book;
import book.LoanBook;
import book.Loanable;
public abstract class Membership
{
public boolean canBorrow(Loanable item,int nItems)
{
return false;
}
}
package membership;
import book.Loanable;
public class StandardMembership extends Membership
{
private final int nMaxBooks=6;
private final int nMaxVideos = 1;
public String toString()
{
return "Standard";
}
public boolean canBorrow(Loanable item,int nItems) {
return Membership.canBorrow(item,nItems,nMaxBooks,nMaxVideos);
}
}
package membership;
import java.util.Calendar;
import book.LoanBook;
import book.Loanable;
public class OapMembership extends Membership
{
int nMaxVideos = 1;
public boolean canBorrow(Loanable item, int nItems)
{
Calendar presentDay = Calendar.getInstance();
int day = presentDay.get(Calendar.DAY_OF_WEEK);
// if (day == Calendar.SATURDAY || day==Calendar.SUNDAY)
// return false;
if(item instanceof LoanBook)
return true;
if(item instanceof Loanable)
return nItems<nMaxVideos;
return false;
}
public String toString()
{
return "OAP";
}
}
package membership;
import video.Video;
import book.LoanBook;
import book.Loanable;
public class JuniorMembership extends Membership {
private final int nMaxBooks = 1;
private final int nMaxVideos = 0;
public String toString() {
return "Junior";
}
public boolean canBorrow(Loanable item, int nItems) {
return Membership.canBorrow(item,nItems,nMaxBooks,nMaxVideos);
}
}
Member.java
package person;
import video.Video;
import book.Book;
import book.Loanable;
import membership.*;
public class Member extends Person {
Membership membertype_;//Member's membership type
private int nBooks_;
private int nVideos_;
public Member(String name,Membership membertype)
{
super(name);
membertype_= membertype;
nBooks_=0;
}
public boolean borrow(Book b)
{
if(b.isLoanable() )
{
Loanable l = (Loanable) b;
if (membertype_.canBorrow(l,nBooks_)){
nBooks_++;
return true;
}
}
return false;
}
public boolean borrow(Video v)
{
if(membertype_.canBorrow(v,nVideos_))
{
nVideos_++;
return true;
}
return false;
}
public String getDetails() {
// TODO Auto-generated method stub
return null;
}
public String toString()
{
return super.toString() + "--Membership type:"+membertype_.toString();
}
}
import video.Video;
import book.Book;
import book.Loanable;
import membership.*;
public class Member extends Person {
Membership membertype_;//Member's membership type
private int nBooks_;
private int nVideos_;
public Member(String name,Membership membertype)
{
super(name);
membertype_= membertype;
nBooks_=0;
}
public boolean borrow(Book b)
{
if(b.isLoanable() )
{
Loanable l = (Loanable) b;
if (membertype_.canBorrow(l,nBooks_)){
nBooks_++;
return true;
}
}
return false;
}
public boolean borrow(Video v)
{
if(membertype_.canBorrow(v,nVideos_))
{
nVideos_++;
return true;
}
return false;
}
public String getDetails() {
// TODO Auto-generated method stub
return null;
}
public String toString()
{
return super.toString() + "--Membership type:"+membertype_.toString();
}
}
Person class
Now there are persons who borrow book from library...and there are also the person who give the books, i.e librarian.
So we can implement abstract class
package person;
public class Person {
String name_;
public Person (String name)
{
name_ = name;
}
public String toString()
{
return "Name:"+name_;
}
public String getDetails() {
// TODO Auto-generated method stub
return "Name: "+name_;
}
}
So we can implement abstract class
package person;
public class Person {
String name_;
public Person (String name)
{
name_ = name;
}
public String toString()
{
return "Name:"+name_;
}
public String getDetails() {
// TODO Auto-generated method stub
return "Name: "+name_;
}
}
Types of book
Now in a library, book can be loanable and reference book.
So we can extend book to loanable providing the function which tells whether the book is loanable or not.
public class LoanBook extends Book implements Loanable
{
public LoanBook (String title, String author, double price, int category, int subCategory)
{
super(title,author,price,category,subCategory);
}
public boolean canLoan()
{
return true;
}
public String details ()
{
return "Loan: "+super.getDetails();
}
public String toString()
{
return super.toString() + "--Loanable Book";
}
public boolean isLoanable()
{
return true;
}
}
So we can extend book to loanable providing the function which tells whether the book is loanable or not.
public class LoanBook extends Book implements Loanable
{
public LoanBook (String title, String author, double price, int category, int subCategory)
{
super(title,author,price,category,subCategory);
}
public boolean canLoan()
{
return true;
}
public String details ()
{
return "Loan: "+super.getDetails();
}
public String toString()
{
return super.toString() + "--Loanable Book";
}
public boolean isLoanable()
{
return true;
}
}
Building the library in Java
First thing is book...So what does book have:
public abstract class Book
{
String title_;
String author_;
float price_;
int category_, subCategory_;
public Book (String title, String author, double price, int category, int subCategory)
{
title_ = title;
author_ = author;
price_ = price;
category_ = category;
subCategory_ = subCategory;
}
public String toString()
{
return "Title:"title_+"--Author:"+author_+"--price:"+price_+"--Category:"+category_+"--SubCategory:"+subCategory_;
}
}
Subscribe to:
Posts (Atom)