Monday 4 October 2010

ItemDaoImpl

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
 */
package mumbay.dao.item;

//~--- non-JDK imports --------------------------------------------------------

import mumbay.data.Item;

/**
 *
 * @author HP
 */
public class ItemDaoImpl implements ItemDao {
    @Override
    public Item getItemById(int itemId) {
        assert itemId != 0;
        String itemName = ItemInfo.getItemNamebyId(itemId);
        String username = ItemInfo.getUsernameById(itemId);
        return new Item(itemId,itemName,username);
    }
}

User.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mumbay.data;

/**
 *
 * @author HP
 */
public class User {
    private String fname_,lname_,username_;
    private String password_;
    private String address_,webAddress_;
    private String mobile_,phone_;
    private String email_;

    public User() {
    }

    public User(String fname) {
        this.fname_ = fname;
    }

    public User(String fname, String lname) {
        this.fname_ = fname;
        this.lname_ = lname;
    }

    public User(String username, String password_,  String fname, String email_) {
        username_=username;
        this.fname_ = fname;
        this.password_ = password_;
        this.email_ = email_;
    }

    public String getUsername() {
        return username_;
    }

    public void setUsername(String username_) {
        this.username_ = username_;
    }

    public String getAddress() {
        return address_;
    }

    public void setAddress(String address_) {
        this.address_ = address_;
    }

    public String getEmail() {
        return email_;
    }

    public void setEmail(String email_) {
        this.email_ = email_;
    }

    public String getFname() {
        return fname_;
    }

    public void setFname(String fname_) {
        this.fname_ = fname_;
    }

    public String getLname() {
        return lname_;
    }

    public void setLname(String lname_) {
        this.lname_ = lname_;
    }

    public String getMobile() {
        return mobile_;
    }

    public void setMobile(String mobile_) {
        this.mobile_ = mobile_;
    }

    public String getPassword() {
        return password_;
    }

    public void setPassword(String password_) {
        this.password_ = password_;
    }

    public String getPhone() {
        return phone_;
    }

    public void setPhone(String phone_) {
        this.phone_ = phone_;
    }

    public String getWebAddress() {
        return webAddress_;
    }

    public void setWebAddress(String webAddress_) {
        this.webAddress_ = webAddress_;
    }

    @Override
    public String toString() {
        return "User{" + "fname_=" + fname_ + "lname_=" + lname_ + "password_=" + password_ + "address_=" + address_ + "webAddress_=" + webAddress_ + "mobile_=" + mobile_ + "phone_=" + phone_ + "email_=" + email_ + '}';
    }

  




}

ItemDao interface

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
 */
package mumbay.dao.item;

//~--- non-JDK imports --------------------------------------------------------

import mumbay.data.Item;

/**
 *
 * @author HP
 */
public interface ItemDao {

//  public void addItem(Item item);
    public Item  getItemById(int itemId);    // throws CustomerNotFoundException;

//  public void deleteUser(int itemId);// throws CustomerNotFoundException;
//  public void updateUser(Item item);// throws CustomerNotFoundException;
}

Item.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mumbay.data;

import java.util.Date;

/**
 *
 * @author HP
 */
public class Item {
    private int itemId_;
    private String itemName_,itemDescription_,itemType_;
    String userName_;
    float price_;
    Date bidDate_;
    boolean isSailable_;

    public Item() {
        isSailable_=true;
    }

    public Item(int itemId_) {
        this.itemId_ = itemId_;
    }

    public Item(int itemId,String itemName_,String username) {
        itemId_=itemId;
        this.itemName_ = itemName_;
        userName_ = username;
        isSailable_ = true;
    }

   
    public Date getBidDate() {
        return bidDate_;
    }

    public void setBidDate(Date bidDate) {
        this.bidDate_ = bidDate;
    }

    public String getItemDescription() {
        return itemDescription_;
    }

    public void setItemDescription(String itemDescription) {
        this.itemDescription_ = itemDescription;
    }

    public int getItemId() {
        return itemId_;
    }

    public void setItemId(int itemId) {
        this.itemId_ = itemId;
    }

    public String getItemName() {
        return itemName_;
    }

    public void setItemName(String itemName_) {
        this.itemName_ = itemName_;
    }

    public String getItemType() {
        return itemType_;
    }

    public void setItemType(String itemType_) {
        this.itemType_ = itemType_;
    }

    public float getPrice() {
        return price_;
    }

    public void setPrice(float price_) {
        this.price_ = price_;
    }

    public String getUserName() {
        return userName_;
    }

    public void setUserName(String userName_) {
        this.userName_ = userName_;
    }

    @Override
    public String toString() {
        return "Item{" + "itemId_=" + itemId_ + "itemName_=" + itemName_ + "itemDescription_=" + itemDescription_ + "itemType_=" + itemType_ + "userName_=" + userName_ + "price_=" + price_ + "bidDate_=" + bidDate_ + "isSailable_=" + isSailable_ + '}';
    }



}

createTable.sql

create table authorisation(
    username varchar(20) not null primary key,
    password varchar(20) not null
)


create table users(

username varchar(20) not null primary key,
fname varchar(20) not null,
lname varchar(20),
address varchar(20),
webaddress varchar(20),
email varchar(20),
phone varchar(20),
mobile varchar(20)
)

create table sailItems
(
itemId int identity primary key ,
itemName varchar(20),
username varchar(20),
price float(6),
bidDate smalldatetime, //not smalldate
isSailable boolean default true//added for history purpose
)

create table itemDescription
(
itemId int primary key,
type varchar(20),
description varchar(50)
)

create table bids
(
bidId int identity primary key,
itemId int not null,
username varchar(20) not null,
bidPrice float(10),
bidDate smalldatetime
)



//ADDING constraints
alter table users
add foreign key(username) references users(username)

alter table sailItems
add foreign key (username) references users(username);

alter table itemDescription
add foreign key (itemId) references sailItems(itemId);

alter table bids
add foreign key(itemId) references sailItems(itemId);

alter table bids
add foreign key(username) references users(username)


//Dropping tables
drop table users
drop table sailItems
drop table itemDescription


//VIEWing tables
select * from authorisation
select * from users



//POPULATIng tables

insert into users( username ,fname )
value('ballg','George') //Note the single quotes , this cause my so much time to waiste

insert into sailitems(itemname , price , biddate ,username )
values('fridge',111.12,'2010-09-21','ballg')

select * from sailitems;
ITEMID      ITEMNAME      USERNAME      PRICE      BIDDATE      ISSAILABLE
2    fridge    ballg    111.12    2010-09-21 00:00:00.0    TRUE
3    cooler    kinshukc    59.12    2010-09-20 00:00:00.0    TRUE
4    tv    richard    75.12    2010-09-22 00:00:00.0    TRUE
5    washing machine    rutuja    100.12    2010-09-23 00:00:00.0    TRUE
6    ipod    ballg    34.12    2010-09-24 00:00:00.0    TRUE
7    fridge    ballg    112.12    2010-09-24 00:00:00.0    TRUE
(6 rows, 4 ms)

insert into bids(itemId , bidPrice , biddate ,username )
values(2,112.12,'2010-09-24','kinshukc')

insert into bids(itemId , bidPrice , biddate ,username )
values(3,67.12,'2010-09-24','kinshukc')

insert into bids(itemId , bidPrice , biddate ,username )
values(4,76.12,'2010-09-25','rutuja')

insert into bids(itemId , bidPrice , biddate ,username )
values(6,33.12,'2010-09-25','rutuja')

insert into bids(itemId , bidPrice , biddate ,username )
values(6,35.12,'2010-09-25','rutuja')

insert into bids(itemId , bidPrice , biddate ,username )
values(6,34.13,'2010-09-25','ballg')

Sunday 19 September 2010

Member again

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();
    }

}

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;
    }
}

getting the loanable interface

package book;

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);
       
    }
}

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();
    }

}

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_;
    }


}

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;
    }
}

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_;
}
}