In: Computer Science
in JAVA please
Some of the characteristics of a book are the title, authors, publisher, ISBN, price, and year of publication. Design the class Book so that each object 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 the number of authors, you can add a variable; Including the methods to perform various operations on the objects of book. 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 an actual title of the book. In the similar way, 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. Write the definitions of the methods of the class Book based on the instruction in a). Write a program that uses the class Book and test various operations on the objects of class Book. Say declare an array of 100 components of the type book. Some 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.
/***********************************Book.java*********************/
package books;
import java.util.ArrayList;
/**
* The Class Book.
*/
public class Book {
/** The title. */
private String title;
/** The authors. */
private ArrayList<String> authors;
/** The publisher. */
private String publisher;
/** The isbn. */
private String isbn;
/** The price. */
private double price;
/** The number of copies in stock. */
private int numberOfCopiesInStock;
/**
* Instantiates a new book.
*/
public Book() {
super();
this.title = "";
this.authors = new
ArrayList<>();
this.publisher = "";
this.price = 0;
this.isbn = "";
this.numberOfCopiesInStock =
0;
}
/**
* Instantiates a new book.
*
* @param title the title
* @param authors the authors
* @param isbn the isbn
* @param price the price
*/
public Book(String title, ArrayList<String>
authors, String isbn, double price) {
super();
this.title = title;
this.authors = authors;
this.isbn = isbn;
this.price = price;
}
/**
* Gets the title.
*
* @return the title
*/
public String getTitle() {
return title;
}
/**
* Sets the title.
*
* @param title the new title
*/
public void setTitle(String title) {
this.title = title;
}
/**
* Gets the authors.
*
* @return the authors
*/
public ArrayList<String> getAuthors() {
return authors;
}
/**
* Sets the authors.
*
* @param authors the new authors
*/
public void setAuthors(ArrayList<String>
authors) {
this.authors = authors;
}
/**
* Gets the publisher.
*
* @return the publisher
*/
public String getPublisher() {
return publisher;
}
/**
* Sets the publisher.
*
* @param publisher the new publisher
*/
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 new isbn
*/
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 new price
*/
public void setPrice(double price) {
this.price = price;
}
/**
* Gets the number of copies in stock.
*
* @return the number of copies in stock
*/
public int getNumberOfCopiesInStock() {
return numberOfCopiesInStock;
}
/**
* Sets the number of copies in stock.
*
* @param numberOfCopiesInStock the new number of
copies in stock
*/
public void setNumberOfCopiesInStock(int
numberOfCopiesInStock) {
this.numberOfCopiesInStock =
numberOfCopiesInStock;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Book [title=" + title + ",
authors=" + authors + ", publisher=" + publisher + ", isbn=" +
isbn
+ ", price=" + price + ",
numberOfCopiesInStock=" + numberOfCopiesInStock + "]";
}
}
/*********************TestBook.java********************/
package books;
import java.util.ArrayList;
/**
* The Class TestBook.
*/
public class TestBook {
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
Book[] books = new Book[100];
ArrayList<String> authors
= new ArrayList<>();
authors.add("James Gosling");
books[0] = new Book("Intro to
Java", authors, "2134-32-3233", 150);
books[1] = new Book("Intro To C++",
authors, "343-545-4545", 120);
books[0].setNumberOfCopiesInStock(20);
books[1].setNumberOfCopiesInStock(10);
searchByTitle(books, "Intro to
Java");
searchByISBN(books,
"343-545-4545");
}
/**
* Search by ISBN.
*
* @param books the books
* @param string the string
*/
private static void searchByISBN(Book[] books, String
string) {
try {
for (Book book :
books) {
if (book.getIsbn().equalsIgnoreCase(string)) {
System.out.println(book.toString());
}
}
} catch (Exception e) {
}
}
/**
* Search by title.
*
* @param books the books
* @param string the string
*/
private static void searchByTitle(Book[] books, String
string) {
try {
for (Book book :
books) {
if (book.getTitle().equalsIgnoreCase(string)) {
System.out.println(book.toString());
}
}
} catch (Exception e) {
}
}
}
/******************output******************/
Book [title=Intro to Java, authors=[James Gosling],
publisher=null, isbn=2134-32-3233, price=150.0,
numberOfCopiesInStock=20]
Book [title=Intro To C++, authors=[James Gosling], publisher=null,
isbn=343-545-4545, price=120.0, numberOfCopiesInStock=10]
Please let me know if you have any doubt or modify the answer, Thanks :)