a. Some of the characteristics of a book are the title, author(S), publisher, IS
ID: 3890574 • Letter: A
Question
a. Some of the characteristics of a book are the title, author(S), publisher, ISBN, price, and year of publication. Design a class bookType that defines the book as an ADT i. Each object of the class bookType can hold the following information about a book: title, up to four authors, publisher, ISBN, price, and number of copies in stock. To keep track of the number of authors, add another member variable i. Include the member functions to perform the various operations on objects of type bookType For example, the usual operations that can be performed on the title are to show the title, set the title, and check whether a title is the same as the actual title of the book. Similarly, the typical operations that can be performed on the number of copies in stock are to show the number of copies in stock, set the number of copies in stock, update the number of copies in stock, and return the number of copies in stock. Add similar operations for the publisher, ISBN, book price and authors. Add the appropriate constructors and a destructor (if one is needed) b. Write the definitions of the member functions of the class bookType c. Write a program that uses the class bookType and tests various operations on the objects of the class bookType. Declare an array of 100 components of type bookType. Some of the operations that you should perform are to search for a book by its title, search by ISBN, and update the number of copies of a book.Explanation / Answer
Tested on Eclipse:
/*************************BookType.java*****************************/
import java.util.Arrays;
/**
* The Class BookType.
*/
public class BookType {
/** The title. */
private String title;
/** The author. */
private String[] author;
/** The publisher. */
private String publisher;
/** The isbn. */
private String isbn;
/** The price. */
private double price;
/** The year of publication. */
private int yearOfPublication;
/** The number of copies. */
private int numberOfCopies;
/**
* Instantiates a new book type.
*
* @param title the title
* @param author the author
* @param publisher the publisher
* @param isbn the isbn
* @param price the price
* @param yearOfPublication the year of publication
* @param numberOfCopies the number of copies
*/
public BookType(String title, String[] author, String publisher, String isbn, double price, int yearOfPublication,
int numberOfCopies) {
super();
this.title = title;
this.author = author;
this.publisher = publisher;
this.isbn = isbn;
this.price = price;
this.yearOfPublication = yearOfPublication;
this.numberOfCopies = numberOfCopies;
}
/**
* Gets the title.
*
* @return the title
*/
public String getTitle() {
return title;
}
/**
* Sets the title.
*
* @param title
* the title to set
*/
public void setTitle(String title) {
this.title = title;
}
/**
* Gets the author.
*
* @return the author
*/
public String[] getAuthor() {
return author;
}
/**
* Sets the author.
*
* @param author
* the author to set
*/
public void setAuthor(String[] author) {
this.author = author;
}
/**
* Gets the publisher.
*
* @return the publisher
*/
public String getPublisher() {
return publisher;
}
/**
* Sets the publisher.
*
* @param publisher
* the publisher to set
*/
public void setPublisher(String publisher) {
this.publisher = publisher;
}
/**
* Gets the isbn.
*
* @return the isbn
*/
public String getIsbn() {
return isbn;
}
/**
* Sets the isbn.
*
* @param isbn
* the isbn to set
*/
public void setIsbn(String isbn) {
this.isbn = isbn;
}
/**
* Gets the price.
*
* @return the price
*/
public double getPrice() {
return price;
}
/**
* Sets the price.
*
* @param price
* the price to set
*/
public void setPrice(double price) {
this.price = price;
}
/**
* Gets the year of publication.
*
* @return the yearOfPublication
*/
public int getYearOfPublication() {
return yearOfPublication;
}
/**
* Sets the year of publication.
*
* @param yearOfPublication
* the yearOfPublication to set
*/
public void setYearOfPublication(int yearOfPublication) {
this.yearOfPublication = yearOfPublication;
}
/**
* Gets the number of copies.
*
* @return the numberOfCopies
*/
public int getNumberOfCopies() {
return numberOfCopies;
}
/**
* Sets the number of copies.
*
* @param numberOfCopies the numberOfCopies to set
*/
public void setNumberOfCopies(int numberOfCopies) {
this.numberOfCopies = numberOfCopies;
}
@Override
public String toString() {
return "BookType [author=" + Arrays.toString(author) + "]";
}
}
/************************************BookDriver.java*********************************/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
/**
* The Class BookDriverTest.
*/
public class BookDriver {
/** The book types. */
static BookType[] bookTypes = new BookType[100];
/**
* Title search.
*
* @param bookList
* the book list
* @param query
* the query
* @return the list
*/
private static List<BookType> titleSearch(BookType[] bookList, String query) {
List<BookType> result = new ArrayList<>();
for (int i = 0; i < bookList.length; i++) {
BookType bookType = bookList[i];
if (bookType != null) {
String title = bookType.getTitle();
if (title.equals(query.trim())) {
result.add(bookType);
}
}
}
return result;
}
/**
* Isbn search.
*
* @param bookList
* the book list
* @param query
* the query
* @return the list
*/
private static List<BookType> isbnSearch(BookType[] bookList, String query) {
List<BookType> result = new ArrayList<>();
for (int i = 0; i < bookList.length; i++) {
BookType bookType = bookList[i];
if (bookType != null) {
String isbn = bookType.getIsbn();
if (isbn.equals(query.trim())) {
result.add(bookType);
}
}
}
return result;
}
/**
* Update number of copies.
*
* @param bookList
* the book list
* @param query
* the query
* @param copies
* the copies
* @return the list
*/
private static List<BookType> updateNumberOfCopies(BookType[] bookList, String query, int copies) {
List<BookType> result = new ArrayList<>();
for (int i = 0; i < bookList.length; i++) {
BookType bookType = bookList[i];
if (bookType != null) {
String title = bookType.getTitle();
if (title.equals(query.trim())) {
bookType.setNumberOfCopies(copies);
result.add(bookType);
}
}
}
return result;
}
/**
* The main method.
*
* @param args
* the arguments
*/
public static void main(String[] args) {
bookTypes[0] = new BookType("Believe in Yourself", new String[] { "Dr. Joseph Murphy" },
"Manjul Publishing House Pvt. Ltd", "ISBN-10: 8183225098", 43.0, 2014, 2);
bookTypes[1] = new BookType("Patanjali Yoga Sutra Hardcover – 2010",
new String[] { "H.H.Sri Sri Ravi Shankar" }, "Sri Sri Publications Trust", "ISBN-10: 9380592817", 199.0,
2010, 4);
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a Search Option: (1) Search By Title (2) Search By ISBN (3) Update number of Copies");
int userChoice = scanner.nextInt();
while (userChoice != -1) {
String query = "";
switch (userChoice) {
case 1:
System.out.print("Enter your search term: ");
scanner.nextLine();
query = scanner.nextLine();
printBooks(titleSearch(bookTypes, query));
break;
case 2:
System.out.print("Enter your search term: ");
scanner.nextLine();
query = scanner.nextLine();
printBooks(isbnSearch(bookTypes, query));
break;
case 3:
System.out.print("Enter the book title for update");
scanner.nextLine();
query = scanner.nextLine();
System.out.print("Enter the number of copies");
int copies = scanner.nextInt();
printBooks(updateNumberOfCopies(bookTypes, query, copies));
break;
default:
break;
}
System.out.println("Please enter a Search Option: (1) Search By Title (2) Search By ISBN (3) Update number of Copies");
userChoice = scanner.nextInt();
}
scanner.close();
}
/**
* Prints the books.
*
* @param books
* the books
*/
private static void printBooks(List<BookType> books) {
System.out.print(" ");
System.out.println(String.format("%-50s %-30s %-30s %-30s %-30s %-15s", "TITLE", "AUTHOR", "ISBN",
"Publisher", "Publication Year", "Number Of Copies"));
for (BookType b : books) {
String line = String.format("%-50s %-30s %-30s %-30s %-30s %-15s", b.getTitle(), Arrays.toString(b.getAuthor()),
b.getIsbn(), b.getPublisher(), b.getYearOfPublication(),b.getNumberOfCopies());
System.out.println(line);
}
System.out.print(" ");
}
}
/***********************************output*****************************************/
Please enter a Search Option:
(1) Search By Title (2) Search By ISBN (3) Update number of Copies
1
Enter your search term: Believe in Yourself
TITLE AUTHOR ISBN Publisher Publication Year Number Of Copies
Believe in Yourself [Dr. Joseph Murphy] ISBN-10: 8183225098 Manjul Publishing House Pvt. Ltd 2014 2
Please enter a Search Option:
(1) Search By Title (2) Search By ISBN (3) Update number of Copies
2
Enter your search term: ISBN-10: 8183225098
TITLE AUTHOR ISBN Publisher Publication Year Number Of Copies
Believe in Yourself [Dr. Joseph Murphy] ISBN-10: 8183225098 Manjul Publishing House Pvt. Ltd 2014 2
Please enter a Search Option:
(1) Search By Title (2) Search By ISBN (3) Update number of Copies
3
Enter the book title for updateBelieve in Yourself
Enter the number of copies4
TITLE AUTHOR ISBN Publisher Publication Year Number Of Copies
Believe in Yourself [Dr. Joseph Murphy] ISBN-10: 8183225098 Manjul Publishing House Pvt. Ltd 2014 4
Please enter a Search Option:
(1) Search By Title (2) Search By ISBN (3) Update number of Copies
1
Enter your search term: Believe in Yourself
TITLE AUTHOR ISBN Publisher Publication Year Number Of Copies
Believe in Yourself [Dr. Joseph Murphy] ISBN-10: 8183225098 Manjul Publishing House Pvt. Ltd 2014 4
Please enter a Search Option:
(1) Search By Title (2) Search By ISBN (3) Update number of Copies
-1
Thanks a lot.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.