Question & Answer: Book Cataloging System Write a program to manage a Book Catalog. The catalog would cont…..


Book Cataloging System Write a program to manage a Book Catalog. The catalog would contain a collection of books, with following fields of information: book code (unique for each book; must be valid - see ISBN validation rules in this assignment for more info), authors last name, authors first name, book title, year of publication and price. The book catalog should use a file for storing books The information in the file uses tabs to separate each of the field of a book, with one book per line. Refer to sample file: booklist.txt for more details. Usage Requireinents: 1. At the start, the program should load the catalog from file (Books.txt from current directory), if it doesnt exist, create a new catalog 2. The program should allow the user to add a new book to the catalog. The program should ask for the required information and then add the book to the catalog, only if same book code doesnt alreadv exist in the catalog and if the book is a valid. Book is considered valid, if the book code is valid, name and title are valid (name cant start with numbers, name/title cant be empty, price is not negative or zero, year cannot be greater than 2013) 3. The program should allow user to find a book in the catalog, given one of the following: author first name or last name or the book code. If the book is found, all the information about the book should be displayed. If the book is not found, an appropriate message should be displayed 4. The program should allow user to delete an existing book from the catalog. 5. The driver (or client) program should have a menu driven interface (console) to allow users to select one of the options to perform the above tasks and repeat them till the program is exited. The program should display appropriate messages to the user for each operation, successful or not 6. When exiting, the program should ask to save Yes/No the book catalog to file, taking in to account any of the above changes, for future reuseQuestion & Answer: Book Cataloging System Write a program to manage a Book Catalog. The catalog would cont..... 1Question & Answer: Book Cataloging System Write a program to manage a Book Catalog. The catalog would cont..... 2Question & Answer: Book Cataloging System Write a program to manage a Book Catalog. The catalog would cont..... 3

Book Cataloging System Write a program to manage a Book Catalog. The catalog would contain a collection of books, with following fields of information: book code (unique for each book; must be valid – see ISBN validation rules in this assignment for more info), author’s last name, author’s first name, book title, year of publication and price. The book catalog should use a file for storing books The information in the file uses tabs to separate each of the field of a book, with one book per line. Refer to sample file: booklist.txt for more details. Usage Requireinents: 1. At the start, the program should load the catalog from file (Books.txt from current directory), if it doesn’t exist, create a new catalog 2. The program should allow the user to add a new book to the catalog. The program should ask for the required information and then add the book to the catalog, only if same book code doesn’t alreadv exist in the catalog and if the book is a valid. Book is considered valid, if the book code is valid, name and title are valid (name can’t start with numbers, name/title can’t be empty, price is not negative or zero, year cannot be greater than 2013) 3. The program should allow user to find a book in the catalog, given one of the following: author first name or last name or the book code. If the book is found, all the information about the book should be displayed. If the book is not found, an appropriate message should be displayed 4. The program should allow user to delete an existing book from the catalog. 5. The driver (or client) program should have a menu driven interface (console) to allow users to select one of the options to perform the above tasks and repeat them till the program is exited. The program should display appropriate messages to the user for each operation, successful or not 6. When exiting, the program should ask to save Yes/No the book catalog to file, taking in to account any of the above changes, for future reuse

Expert Answer

 

Question & Answer: Book Cataloging System Write a program to manage a Book Catalog. The catalog would cont..... 4Question & Answer: Book Cataloging System Write a program to manage a Book Catalog. The catalog would cont..... 5Question & Answer: Book Cataloging System Write a program to manage a Book Catalog. The catalog would cont..... 6Question & Answer: Book Cataloging System Write a program to manage a Book Catalog. The catalog would cont..... 7Question & Answer: Book Cataloging System Write a program to manage a Book Catalog. The catalog would cont..... 8Question & Answer: Book Cataloging System Write a program to manage a Book Catalog. The catalog would cont..... 9Question & Answer: Book Cataloging System Write a program to manage a Book Catalog. The catalog would cont..... 10Question & Answer: Book Cataloging System Write a program to manage a Book Catalog. The catalog would cont..... 11Question & Answer: Book Cataloging System Write a program to manage a Book Catalog. The catalog would cont..... 12Question & Answer: Book Cataloging System Write a program to manage a Book Catalog. The catalog would cont..... 13Question & Answer: Book Cataloging System Write a program to manage a Book Catalog. The catalog would cont..... 14Question & Answer: Book Cataloging System Write a program to manage a Book Catalog. The catalog would cont..... 15Question & Answer: Book Cataloging System Write a program to manage a Book Catalog. The catalog would cont..... 16Question & Answer: Book Cataloging System Write a program to manage a Book Catalog. The catalog would cont..... 17Question & Answer: Book Cataloging System Write a program to manage a Book Catalog. The catalog would cont..... 18Question & Answer: Book Cataloging System Write a program to manage a Book Catalog. The catalog would cont..... 19Question & Answer: Book Cataloging System Write a program to manage a Book Catalog. The catalog would cont..... 20Question & Answer: Book Cataloging System Write a program to manage a Book Catalog. The catalog would cont..... 21Question & Answer: Book Cataloging System Write a program to manage a Book Catalog. The catalog would cont..... 22

Copyable code:

File Name: Book.java

import java.io.*;

import java.util.Scanner;

import java.util.*;

class Book

{

private String myBookISBN;

private String myBookLastName;

private String myBookFirstName;

private String myBookTitle;

private int myBookPubYear;

private double myBookPrice;

private Book myNextBook;

public Book(String mycode, String mylast, String myfirst, String myBookTitle, int mypubyear, double myBookPrice, Book myNextBook)

{

Scanner myConsoleScanner = new Scanner(System.in);

this.myBookISBN=checkBookInputs(mycode,mypubyear, myConsoleScanner);

this.myBookLastName = checkNameTitle(mylast, myConsoleScanner);

this.myBookFirstName = checkNameTitle(myfirst, myConsoleScanner);

this.myBookTitle = checkNameTitle(myBookTitle, myConsoleScanner);

this.myNextBook = myNextBook;

if(myBookPrice > 0)

{

this.myBookPrice = myBookPrice;

}

else{

boolean isvalid = false;

while(isvalid != true)

{

System.out.println(“ERROR: Price negative.”);

System.out.print(“Enter price “);

myBookPrice = myConsoleScanner.nextDouble();

if(myBookPrice > 0){

this.myBookPrice = myBookPrice;

}

else{

isvalid = false;

}

}

}

}

private String checkNameTitle(String myBookInput, Scanner myConsoleScanner)

{

if(myBookInput != null && myBookInput.charAt(0) > 64 && myBookInput.charAt(0) < 123){

return myBookInput;

}

else

{

boolean isvalid = false;

System.out.println(“ERROR”);

while(isvalid != true){

System.out.print(“Enter datas: “);

myBookInput = myConsoleScanner.nextLine();

if(myBookInput != null && myBookInput.charAt(0) > 64 && myBookInput.charAt(0) < 123){

isvalid = true;

}

else{

isvalid = false;

}

}

return myBookInput;

}

}

public String checkBookInputs(String mycode, int mypubyear, Scanner myConsoleScanner)

{

boolean mycodeInput = false;

boolean valBookInput = false;

boolean checkBookInput = false;

while(mycodeInput == false)

{

if(mycode.replaceAll(“-“, “”).length()!=10)

{

while(mycode.replaceAll(“-“, “”).length()!=10){

System.out.println(“ERROR”);

System.out.print(“Enter book code: “);

mycode = myConsoleScanner.nextLine();

}

}

else if(mycode.startsWith(“-“)||mycode.endsWith(“-“))

{

System.out.println(“ERROR”);

System.out.print(“Enter book code: “);

mycode = myConsoleScanner.nextLine();

}

else if(valBookInput == false)

{

int count = 0;

for(int kk = 0;kk<mycode.length();kk++){

char a = mycode.charAt(kk);

if(a==’-‘&& mycode.charAt(kk+1)==’-‘){

System.out.println(“ERROR”);

System.out.print(“Enter book code: “);

mycode = myConsoleScanner.nextLine();

}

else if(a==’-‘){

count++;

}

else if((int)a<48 && ((int)a!=45)||(int)a>57 && ((int)a!=88 && (int)a!=120)){

System.out.println(“ERROR”);

System.out.print(“Enter book code: “);

mycode = myConsoleScanner.nextLine();

}

}

if(count >= 4||count<3){

System.out.println(“ERROR”);

System.out.print(“Enter book code: “);

mycode = myConsoleScanner.nextLine();

}

else if(checkBookInput == false){

String mylastDigit = mycode.substring(mycode.length()-1, mycode.length());

if(mylastDigit.equalsIgnoreCase(“X”)){

mylastDigit = “10”;

}

int checkSum = Integer.valueOf(mylastDigit);

int sum = 0;

for(int kk = 0;kk<mycode.replaceAll(“-“, “”).length()-1;kk++){

sum = sum + (Integer.valueOf(mycode.replaceAll(“-“, “”).substring(kk, kk+1))*(kk+1));

}

if(sum%11!=checkSum){

System.out.println(“ERROR”);

System.out.print(“Enter book code: “);

mycode = myConsoleScanner.nextLine();

}

else{

checkBookInput = true;

}

}

else

{

valBookInput = true;

}

}

else{

mycodeInput = true;

}

while(mypubyear>2016)

{

System.out.println(“ERROR”);

System.out.print(“Enter year: “);

String x = myConsoleScanner.next();

int value = 0;

boolean isvalid = false;

while(isvalid == false){

for(int kk = 0;kk<x.length();kk++){

char y = x.charAt(kk);

if((int)y<48||(int)y>57){

System.out.print(“ERROR “);

x = myConsoleScanner.next();

}

else{

isvalid = true;

}

}

}

mypubyear = Integer.valueOf(x);

}

this.myBookPubYear = mypubyear;

}

return mycode;

}

//

public boolean getBookData(String bookVl){

if(bookVl.equalsIgnoreCase(myBookISBN)||bookVl.equalsIgnoreCase(myBookFirstName)||bookVl.equalsIgnoreCase(myBookLastName)||bookVl.equalsIgnoreCase(myBookTitle)){

return true;

}

return false;

}

public Book getBookNxt() {

return myNextBook;

}

public void setBookNxt(Book myNextBook) {

this.myNextBook = myNextBook;

}

@Override

public String toString() {

return “Book [ISBN: “+this.myBookISBN+”n”+”Title: ” + this.myBookTitle +”n”+”Author: “+this.myBookFirstName+” “+this.myBookLastName+”n”+”Date of Publication: “+this.myBookPubYear+”n”+”Price: $”+this.myBookPrice+ “]”;

}

public String getBookISBN() {

return myBookISBN;

}

public void setBookISBN(String myBookISBN) {

this.myBookISBN = myBookISBN;

}

public String getAutLastName() {

return myBookLastName;

}

public void setAutLastName(String myBookLastName) {

this.myBookLastName = myBookLastName;

}

public String getAutFirstName() {

return myBookFirstName;

}

public void setAutFirstName(String myBookFirstName) {

this.myBookFirstName = myBookFirstName;

}

public String getMyBookTitle() {

return myBookTitle;

}

public void setMyBookTitle(String myBookTitle) {

this.myBookTitle = myBookTitle;

}

public int getMyBookPubYear() {

return myBookPubYear;

}

public void setMyBookPubYear(int myBookPubYear) {

this.myBookPubYear = myBookPubYear;

}

public double getMyBookPrice() {

return myBookPrice;

}

public void setMyBookPrice(double myBookPrice) {

this.myBookPrice = myBookPrice;

}

}

File Name: BookCatalog.java

class BookCatalog

{

private Book myHeadBook;

private int myBookCnt;

public String toString()

{

if(myHeadBook!=null)

{

Book myCurrentBookCur = myHeadBook;

while(myCurrentBookCur!=null)

{

System.out.println(“n”+ myCurrentBookCur.toString());

myCurrentBookCur = myCurrentBookCur.getBookNxt();

}

}

else

System.out.println(“EMPTY CATALOG.”);

return null;

}

public Book[] displayMyBookCat()

{

Book myCurrentBookCur = myHeadBook;

Book[] tp = new Book[myBookCnt-1];

int kk = 0;

while(myCurrentBookCur!=null)

{

Book delBook = myCurrentBookCur;

myCurrentBookCur = myCurrentBookCur.getBookNxt();

tp[kk] = delBook;

kk++;

}

return tp;

}

public void addMyBook( String isbn, String myBookLastName, String myBookFirstName, String myBookTitle, int mypubyear, double myBookPrice)

{

//

myHeadBook = new Book(isbn, myBookLastName, myBookFirstName, myBookTitle, mypubyear, myBookPrice, myHeadBook);

myBookCnt++;

}

public void addMyBookEnd(String isbn, String myBookLastName, String myBookFirstName, String myBookTitle, int mypubyear, double myBookPrice)

{

Book myNewBok = new Book(isbn, myBookLastName, myBookFirstName, myBookTitle, mypubyear, myBookPrice, null);

if(myHeadBook==null)

myHeadBook = myNewBok;

else

{

Book myCurrentBookCur = myHeadBook;

while(myCurrentBookCur.getBookNxt()!=null)

myCurrentBookCur = myCurrentBookCur.getBookNxt();

myCurrentBookCur.setBookNxt(myNewBok);

}

}

public String checkMyBookPresence(String bookVl)

{

Book myCurrentBookCur = myHeadBook;

while(myCurrentBookCur!=null)

{

if(myCurrentBookCur.getBookData(bookVl)== true)

return myCurrentBookCur.toString();

myCurrentBookCur = myCurrentBookCur.getBookNxt();

}

return (“NOT FOUND”);

}

//Get count of books in the catalog

public int getMyCatSize()

{

int myBookCnt = 0;

for(Book myCurrentBookCur = myHeadBook; myCurrentBookCur != null; myCurrentBookCur = myCurrentBookCur.getBookNxt())

++myBookCnt;

return myBookCnt;

}

//Delete book

public void delMyBookFromCat(String id)

{

Book myCurrentBookCur = myHeadBook;

Book myBookref = null;

while(myCurrentBookCur.getBookData(id)!=true && myCurrentBookCur!=null)

{

myBookref = myCurrentBookCur;

myCurrentBookCur = myCurrentBookCur.getBookNxt();

}

myBookref.setBookNxt(myCurrentBookCur.getBookNxt());

System.out.printf(“SUCCESSFULLY REMOVED.”);

}

}

File Name: BookCatalogClient.java

import java.io.*;

import java.util.Scanner;

import java.util.*;

public class BookCatalogClient

{

public static BookCatalog myBookCatalog;

public static Scanner myFileScanner;

public static Scanner myConsoleScanner;

public static OutputStream myStreamOut;

private static void displayUserMenu()

{

myConsoleScanner = new Scanner(System.in);

System.out.println(“MENU”);

System.out.println(“t 1.) ADD BOOK”);

System.out.println(“t 2.) SEARCH”);

System.out.println(“t 3.) REMOVE”);

System.out.println(“t 4.) LIST”);

System.out.println(“t 5.) TOTAL BOOKS COUNT.”);

System.out.println(“t 6.) END.”);

System.out.println(“Enter ur choice: “);

String myUserChoice = myConsoleScanner.next();

try

{

if(Integer.valueOf(myUserChoice) == 1)

{

System.out.print(“ENTER BOOK CODE: “);

String [] mybook = new String[6];

mybook[0]=myConsoleScanner.next();

System.out.print(“ENTER LAST NAME: “);

mybook[1]=myConsoleScanner.next();

System.out.print(“FIRST NAME: “);

mybook[2]=myConsoleScanner.next();

myConsoleScanner.nextLine();

System.out.print(“TITLE: “);

String myBookTitle = myConsoleScanner.nextLine();

mybook[3]=myBookTitle;

System.out.print(“PUBLISHED YEAR: “);

mybook[4]=myConsoleScanner.next();

System.out.print(“BOOK PRICE: “);

mybook[5]=myConsoleScanner.next();

myBookCatalog.addMyBook(mybook[0],mybook[1],mybook[2], mybook[3],Integer.valueOf(mybook[4]),Double.valueOf(mybook[5]));

displayUserMenu();

}

else if(Integer.valueOf(myUserChoice) == 2)

{

System.out.print(“ENTER ANY OF THE FOLLOWING BOOK CODE, LAST NAME, FIRST NAME, TITLE: “);

System.out.println(myBookCatalog. checkMyBookPresence(myConsoleScanner.next()));

displayUserMenu();

}

else if(Integer.valueOf(myUserChoice) == 3)

{

System.out.print(“ENTER ANY OF THE FOLLOWING BOOK CODE, LAST NAME, FIRST NAME, TITLE: “);

myBookCatalog.delMyBookFromCat( myConsoleScanner.next());

displayUserMenu();

}

else if(Integer.valueOf(myUserChoice) == 4)

{

myBookCatalog.toString();

displayUserMenu();

}

else if(Integer.valueOf(myUserChoice) == 5)

{

System.out.println(“TOTAL BOOKS “+ myBookCatalog.getMyCatSize());

displayUserMenu();

}

else if(Integer.valueOf(myUserChoice) == 6)

{

System.out.print(“Do You want to save?”);

if(myConsoleScanner.next().equalsIgnoreCase(“yes”) || myConsoleScanner.next().equalsIgnoreCase(“y”) )

{

try

{

PrintStream pout = new PrintStream(new File(“books.txt”));

for(Book book: myBookCatalog.displayMyBookCat())

{

pout.println(book.getBookISBN()+”t”+book.getAutLastName()+”t”+book.getAutFirstName()+”t”+book.getMyBookTitle()+”t”+book.getMyBookPubYear()+”t”+book.getMyBookPrice());

}

pout.close();

}

catch (FileNotFoundException ep)

{

System.out.println(“Error, unable to save. File not found.”);

}

System.exit(-1);

}

else

{

System.out.println(“Thank you.”);

System.exit(-1);

}

}

else{

System.out.println(“INVALID CHOICE”);

displayUserMenu();

}

}

catch(NumberFormatException ep)

{

System.out.println(“Wrong input”);

displayUserMenu();

}

}

public static void loadBooksFromFile() throws FileNotFoundException

{

System.out.println(“BOOKS CATALOG PROGRAM.”);

System.out.println(“LOADING DATABASE”);

try

{

myBookCatalog = new BookCatalog();

myFileScanner = new Scanner(new File(“books.txt”));

while(myFileScanner.hasNext()==true)

{

String myBok = myFileScanner.nextLine();

String[] bookData = myBok.split(“t”);

myBookCatalog.addMyBook(bookData[0], bookData[1], bookData[2], bookData[3], Integer.valueOf(bookData[4]), Double.valueOf(bookData[5]));

}

System.out.println(” OK!”);

}

catch (FileNotFoundException ep) {

System.out.println(“NO FILE.”);

}

}

public static void main(String[] args) throws FileNotFoundException {

loadBooksFromFile();

displayUserMenu();

}

}

Still stressed from student homework?
Get quality assistance from academic writers!