creating a program, simiulating an inquiry system of a small library 1. Read the
ID: 3871478 • Letter: C
Question
creating a program, simiulating an inquiry system of a small library
1. Read the library catalog from an input data file. The data file should be named catalog.txt. There is one input line per book, and these lines have the following format: <book_id> <book_title> <ISBN> <author_last_name> <category> where <book_id> is a 5-digit positive integer (you may assume the leftmost digit is not a zero), <book_title> is a string of at most 30 characters with no embedded blanks (you can use “_” in between words in a title), <ISBN> is a string of 10 digits, <author_last_name> is a string of at most 10 characters, and <category> is a character (‘F’ if the book is fiction; ‘N’ if it is a nonfiction book). You may assume the catalog has no more than 100 books (a small library). A sample catalog is shown at the end of the specification.
Make sure to include exception handling code related to file input.
2. Read from standard input a customer’s inquiry with a given book_id and output the complete information of the book. Your program should allow the customer to continue to inquiry about other books. When a zero input for <book_id> is entered, it means the end of the customer’s inquiry.
The output should include the book_id, book_title, ISBN, author_last_name, and “Fiction” or “Non-Fiction” for category, printed on a single line. A sample output is shown at the end of the specification.
3. Exception handling: The following error is possible and should be handled by your program: <book_id> not found. You need to define a BookNotFoundException and handle it by writing an error message. This should be defined as a checked exception. It should include two constructors. One is the default constructor, and the other is one-parameter constructor and the parameter type is String.
Program Structure:
Your source code should be developed in three files: Book.java, BookDemo.java, and BookNotFoundException.java. Book.java will contain the class definition for a book according to the requirements specified below. BookDemo.java will be the application program that runs the simulation of the inquiry. BookNotFoundException.java will define the checked exception.
Data Structure:
Each catalog item should be an object of the Book class. Define a separate instance variable of the appropriate type for the five pieces of information about each book. Instance variables should be maintained as private data. Besides constructor(s), the following methods are required for the Book class. • A getter method for each instance variable. • A toString method that takes no parameter and returns all the information of the book as a combined string, including book_id, book_title, ISBN, author_last_name, and “Fiction” or “Non-Fiction” for category. • A static method bookSearch that takes three input parameters: (1) an array of Book objects that represent the entire catalog; (2) an integer specifying how many books are actually in the array; and (3) an integer representing a book_id. The method should search the array of books looking for the book with the given book_id as specified by the third parameter. The method should return the index within the array. If it cannot find the item, the method should throw a BookNotFoundException but it is not handled in this method. Instead it will be handled in the main method where it is called.
Sample Catalog file:
10001 Emma 0486406482 Austen F
12345 My_Life 0451526554 Johnson N
Sample interaction:
Enter book id: 12345 Book id: 12345, Title: My_Life, ISBN: 0451526554, Author: Johnson, Non-fiction
Enter book id: 10001 Book id: 10001, Title: Emma, ISBN: 0486406482, Author: Austen, Fiction
Enter book id: 0
Note: if you use Eclipse, the input file should be placed outside of src folder.
Explanation / Answer
package com.kanth.chegg;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class BookDemo {
public static void main(String[] args) {
try{
List<Book> bookData=getFileInformation();
Book [] bookArray=new Book[bookData.size()];
bookData.toArray(bookArray);
String bookid="0";
do {
System.out.println("Please Enter the Book Id:");
Scanner sc = new Scanner(System.in);
bookid = sc.next();
int n=0;
if(Integer.parseInt(bookid)!=0){
n=bookSearch(bookArray,bookData.size(),Integer.parseInt(bookid));
Book reqBook=bookArray[n];
if(reqBook.getCategory().trim().equals("F")){
reqBook.setCategory("Fiction");
}else{
reqBook.setCategory("Non-fiction");
}
System.out.println("Book id:"+reqBook.getBookId()+", Title:"+reqBook.getBookTitle()+" ,ISBN:"+reqBook.getISBN()+", Author:"+reqBook.getLastName()+","+reqBook.getCategory());
}
}
while(Integer.parseInt(bookid)!=0);
}
catch(Exception e){
e.printStackTrace();
}
}
public static List<Book> getFileInformation() throws Exception {
List<Book> bookinfoList=new ArrayList<Book>();
BufferedReader br = new BufferedReader(new FileReader("file.txt"));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
String[] splited = line.split("\s+");
if(splited.length>0){
Book bean=new Book();
bean.setBookId(splited[0]);
bean.setBookTitle(splited[1]);
bean.setISBN(splited[2]);
bean.setLastName(splited[3]);
bean.setCategory(splited[4]);
bookinfoList.add(bean);
}
line = br.readLine();
}
} finally {
br.close();
}
return bookinfoList;
}
public static int bookSearch(Book [] bookArray ,int size,int bookId) throws Exception{
for(int i=0;i<size;i++){
Book b=bookArray[i];
if(Integer.parseInt(b.getBookId())==bookId){
return i;
}
}
throw new BookNotFoundException("Book Not found ");
}
}
class BookNotFoundException extends Exception{
BookNotFoundException(String s){
super(s);
}
}
class Book {
private String bookId="";
private String bookTitle="";
private String ISBN="";
private String lastName="";
private String category="";
public String toString() {
return bookId+" "+bookTitle+" "+" "+ISBN+" "+lastName+" "+category;
}
public String getBookId() {
return bookId;
}
public void setBookId(String bookId) {
this.bookId = bookId;
}
public String getBookTitle() {
return bookTitle;
}
public void setBookTitle(String bookTitle) {
this.bookTitle = bookTitle;
}
public String getISBN() {
return ISBN;
}
public void setISBN(String iSBN) {
ISBN = iSBN;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.