ݺߣ

ݺߣShare a Scribd company logo
BookCollectionHelper.java
package com.cg.helper;
importjava.util.ArrayList;
importjava.util.Iterator;
importcom.cg.bean.BookSchema;
publicclassBookCollectionHelper{
private staticArrayList<BookSchema>bookList=null;
static
{
bookList=new ArrayList<BookSchema>();
BookSchemab1=newBookSchema(111,"Seeintothe Seaof C",350);
BookSchemab2=newBookSchema(222,"LearningJavain21 Days",450.50);
BookSchemab3=newBookSchema(333,"ASP.NetwithC#",250.75);
bookList.add(b1);
bookList.add(b2);
bookList.add(b3);
}
publicBookCollectionHelper(){}
/***Add NewBookinArrayList**/
publicvoidaddNewBookDetails(BookSchemabook)
{
bookList.add(book);
}
publicstaticArrayList<BookSchema>getbookList() {
returnbookList;
}
publicstaticvoidsetbookList(ArrayList<BookSchema>bookList) {
BookCollectionHelper.bookList=bookList;
}
/***Fetch All BookDetails*/
publicstatic voiddisplayBookCount()
{
Iterator<BookSchema>bookIt=bookList.iterator();
BookSchematempBook=null;
inttotalCount=0;
while(bookIt.hasNext())
{
totalCount++;
tempBook=bookIt.next();
System.out.println(tempBook);
}
System.out.println("Total Countof Books"+totalCount);
}
}
/****
/
BookCollectionHelperTest.java
package com.cg.junit;
importjunit.framework.Assert;
importorg.junit.Test;
importorg.junit.AfterClass;
importorg.junit.BeforeClass;
importcom.cg.bean.BookSchema;
importcom.cg.exception.BookException;
importcom.cg.helper.BookCollectionHelper;
publicclassBookCollectionHelperTest{
staticBookCollectionHelpercollectionHelper;
staticBookSchemabook=null;
@BeforeClass
public static voidbeforeClass()
{
collectionHelper=newBookCollectionHelper();
book=newBookSchema(888,"Neelima",670.75);
}
@AfterClass
publicstatic voidafterClass()
{
collectionHelper=null;
book=null;
}
@Test
publicvoidtestAddNewBook() throwsBookException
{
collectionHelper.addNewBookDetails(book);
//Assert.assertEquals(4,collectionHelper.getCustList().size());
Assert.assertNotNull(collectionHelper.toString());
}
}
/****
/
BookDataValidator.java
package com.cg.helper;
importjava.util.regex.Pattern;
importcom.cg.exception.BookException;
publicclassBookDataValidator{
public static booleanvalidateBookName(StringbookName)throwsBookException
{
StringcustPattern="[A-Za-z]{6,20}";
if(Pattern.matches(custPattern,bookName))
{
returntrue;
}
else
{
thrownewBookException("BookName shouldstartwithCAPITAL&Min6 and
Max 20 characters Allowed");
}
}
public static booleanvalidateBookId(StringbookId)throwsBookException
{
StringIdPattern="d{3}";
if(Pattern.matches(IdPattern,bookId))
{
returntrue;
}
else
{
thrownewBookException("Only3-digitBookIDisAllowed");
}
}
public static booleanvalidateBookPrice(StringbookPrice)throwsBookException
{
StringpricePattern="d{2,4}.?[0-9]{2}$";
if(Pattern.matches(pricePattern,bookPrice))
{
returntrue;
}
else
{
thrownewBookException("Onlynumbers(andpaise if any) isAllowed");
}
}
}
/****
**/
BookException.java
package com.cg.exception;
publicclassBookExceptionextendsException{
private staticfinal longserialVersionUID=1L;
publicBookException()
{
super();
}
publicBookException(Stringmessage,Throwablecause,booleanenableSuppression,boolean
writableStackTrace)
{
super(message,cause,enableSuppression,writableStackTrace);
}
publicBookException(Stringmessage,Throwablecause)
{
super(message,cause);
}
publicBookException(Stringmessage)
{
super(message);
}
publicBookException(Throwablecause)
{
super(cause);
}
}
/****
/
BookSchema.java
package com.cg.bean;
publicclassBookSchema{
private intbookId;
private StringbookName;
private double bookPrice;
publicBookSchema(){ }
publicBookSchema(intbookId,StringbookName,double bookPrice) {
this.bookId=bookId;
this.bookName=bookName;
this.bookPrice =bookPrice;
}
publicintgetBookId() {
returnbookId;
}
publicvoidsetBookId(intbookId){
this.bookId=bookId;
}
publicStringgetBookName(){
returnbookName;
}
publicvoidsetBookName(StringbookName){
this.bookName=bookName;
}
publicdouble getBookPrice(){
returnbookPrice;
}
publicvoid setBookPrice(double bookPrice){
this.bookPrice =bookPrice;
}
@Override
publicStringtoString() {
return"Book [bookId="+bookId+ ",bookName="
+ bookName +", bookPrice="+bookPrice + "]";
}
}
/****
/
BookUI.java
package com.cg.ui;
importjava.util.Scanner;
importcom.cg.bean.BookSchema;
importcom.cg.exception.BookException;
importcom.cg.helper.BookCollectionHelper;
importcom.cg.helper.BookDataValidator;
publicclassBookUI{
staticScanner sc=newScanner(System.in);
staticBookCollectionHelpercollectionhelper=null;
@SuppressWarnings("static-access")
publicstaticvoidmain(String[] args) {
// TODO Auto-generated methodstub
intchoice=0;
collectionhelper=new BookCollectionHelper();
while(true)
{
System.out.println("1:AddNew Book n"+
"2:Findtotal count of books n3:Exit");
System.out.println("nEnterURChoice:");
choice=sc.nextInt();
switch(choice)
{
case 1: enterNewBookDetails();break;
case 2: collectionhelper.displayBookCount();break;
default:System.exit(0);
}
}
}
private staticvoidenterNewBookDetails()
{
System.out.println("How manynew Books?");
intbcount=sc.nextInt();
while (bcount!=0) {
System.out.println("EnterBookId:");
StringbookId=sc.next();
try
{
if(BookDataValidator.validateBookId(bookId))
System.out.println("EnterBookname:");
StringbookName=sc.next();
if(BookDataValidator.validateBookName(bookName))
{
System.out.println("EnterPrice ");
StringbookPrice =sc.next();
if(BookDataValidator.validateBookPrice(bookPrice))
{
BookSchemabook=new
BookSchema(Integer.parseInt(bookId),bookName,Double.parseDouble(bookPrice));
collectionhelper.addNewBookDetails(book);
}
}
}
catch (BookExceptione)
{
System.out.println(e.getMessage());
}
bcount--;
}
}
}
/****
**/
helpfile forBook.txt
Note:
1. Include the jarfilesrequiredforjunit.
2. The executable file is:BookUI.java
-------
Create filesinthe followingsequence :
1. BookSchema
2. BookUI
3. BookDataValidator &BookCollectionHelper
4. BookException&bbokCollectionHelperTest

More Related Content

Book integrated assignment