java programming question...please solve according to question using classes D.
ID: 3730893 • Letter: J
Question
java programming question...please solve according to question using classes
D. Suppose that you have been assigned the task of computerizing the card catalog system for a library. As a first step, your supervisor has asked you to develop a class capable of storing the following information for each book. The title - A list of authors · The Library of Congress catalog number A list of subject headings . The publisher The year of publication -Whether the book is circulating or non-circulating Include the variables and methods that would be necessary to keep all the information required for this prototype database. Create a Library class that contains an arrayList of books. Include a method SearchBySubject that takes as parameter a subject string. For each book in the library that lists the subject string as one of its headings, SearchBySubject should display the title, the name of the author, and the L.ibrary of Congres cgumber of the book.Explanation / Answer
Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.
// Book.java
public class Book {
/**
* Defining all the required attributes
*/
private String title;
private String[] authors;
private int catalogNumber;
private String[] subjects;
private String publisher;
private int year;
private boolean isCirculating;
// constructor with no arguments
public Book() {
/**
* Initializing everything to default values
*/
title = "";
authors = new String[0];
catalogNumber = 0;
subjects = new String[0];
publisher = "";
year = 0;
isCirculating = false;
}
// constructor with parameters
public Book(String title, String[] authors, int catalogNumber,
String[] subjects, String publisher, int year, boolean isCirculating) {
/**
* Initializing everything to the given values
*/
this.title = title;
this.authors = authors;
this.catalogNumber = catalogNumber;
this.subjects = subjects;
this.publisher = publisher;
this.year = year;
this.isCirculating = isCirculating;
}
// getters and setters
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String[] getAuthors() {
return authors;
}
public void setAuthors(String[] authors) {
this.authors = authors;
}
public int getCatalogNumber() {
return catalogNumber;
}
public void setCatalogNumber(int catalogNumber) {
this.catalogNumber = catalogNumber;
}
public String[] getSubjects() {
return subjects;
}
public void setSubjects(String[] subjects) {
this.subjects = subjects;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public boolean isCirculating() {
return isCirculating;
}
public void setCirculating(boolean isCirculating) {
this.isCirculating = isCirculating;
}
/**
* returns a properly formatted String which contain Title, authors and the
* catalog number
*/
@Override
public String toString() {
String data = "Title: " + title + " ";
data += "Authors: ";
for (int i = 0; i < authors.length; i++) {
data += authors[i];
if (i != authors.length - 1) {
data += ", ";
}
}
data += " Catalog number: " + catalogNumber;
return data;
}
}
// Library.java
import java.util.ArrayList;
public class Library {
// defining an array list of books
static ArrayList<Book> books;
public static void main(String[] args) {
/**
* Defining a few books
*/
Book book1 = new Book("On the origin of species",
new String[] { "Charles Darwin" }, 12345, new String[] {
"Science", "Education" }, "Some publisher", 1859, true);
Book book2 = new Book("The Discovery Of India",
new String[] { "Jawaharlal Nehru" }, 1008,
new String[] { "Autobiography" }, "XYZ publishers", 1955, true);
Book book3 = new Book("Good Omens", new String[] { "Neil Gaiman",
"Terry Pratchett" }, 10234, new String[] { "Horror", "Drama" },
"ABC publishers", 1955, true);
Book book4 = new Book("Wuthering Heights",
new String[] { "Emily Bronte" }, 200155,
new String[] { "Fiction" }, "RRR publishers", 2014, false);
Book book5 = new Book("The fault in our stars",
new String[] { "John Green" }, 144889, new String[] {
"Fiction", "Drama", "Romance" }, "CCV publishers",
2001, true);
Book book6 = new Book("The Underground Railroad",
new String[] { "Colson Whitehead " }, 348385,
new String[] { "Fiction" }, "XYZ publishers", 2017, true);
/**
* initializing the array list and adding books
*/
books = new ArrayList<Book>();
books.add(book1);
books.add(book2);
books.add(book3);
books.add(book4);
books.add(book5);
books.add(book6);
searchBySubject("fiction");
}
/**
* method to search books by a subject and display the results
*
* @param subject
* - subject to be searched
*/
static void searchBySubject(String subject) {
if (books != null) {
System.out.println("Search results by subject: " + subject + " ");
/**
* looping through all books
*/
for (Book b : books) {
/**
* looping through all subjects of the current book
*/
for (String sub : b.getSubjects()) {
if (sub.equalsIgnoreCase(subject)) {
//match found, displaying it
System.out.println(b + " ");
}
}
}
}
}
}
/*OUTPUT*/
Search results by subject: fiction
Title: Wuthering Heights
Authors: Emily Bronte
Catalog number: 200155
Title: The fault in our stars
Authors: John Green
Catalog number: 144889
Title: The Underground Railroad
Authors: Colson Whitehead
Catalog number: 348385
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.