In: Computer Science
TestLibrary.java
Design and implement a class named Book. A book has a serial number, a title, an author, and a publisher. Create appropriate constructor(s) and accessor and mutator methods for the Book class.
Add a static variable to the Book class and set its initial value to 100000000. When a new book is created, this static variable is automatically incremented by 1 and the new value is assigned as the serial number of the new book.
Design and implement a class named Library. A library has a library name (such as Surrey Central Library), a list of books (which is an array of Book objects. Assume that the maximum number of books for a library is 10000), and the actual number of books in the library. Create a constructor for creating a library object with a given library name. Create a method for adding a book to the library and another method for returning the array of all books. Add another method to get the number of books in the library.
In the main method, create a new library object and create a few books. Add the books to the library. Get the list of books and display their serial numbers, titles, author names, and publishers.
Book.java
public class Book {
private int serial;
private String title;
private String author;
private String publisher;
private static int IV = 100000;
public Book(String title, String author, String
publisher) {
super();
IV++;
this.serial = IV;
this.title = title;
this.author = author;
this.publisher = publisher;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
public int getSerial() {
return serial;
}
public String getAuthor() {
return author;
}
//I 've added this for demonstration. You can delete
it if you want.
@Override
public String toString() {
return "Book [serial=" + serial +
", title=" + title + ", author=" + author + ", publisher=" +
publisher + "]";
}
}
Library.java
public class Library {
private Book[] books;
private String name;
private int numOfBooks;
private static final int MAX = 10000;
public Library(String name) {
super();
numOfBooks = 0;
books = new Book[10000];
this.name = name;
}
public void addBook(Book b) {
if(numOfBooks<MAX) {
books[numOfBooks] = b;
numOfBooks++;
}
}
public Book[] getBooks() {
Book[] buks = new
Book[this.numOfBooks];
for(int i=0; i<numOfBooks;
i++)
buks[i] =
this.books[i];
return buks;
}
public int getNumOfBooks() {
return this.numOfBooks;
}
}
TestLibrary.java
public class TestLibrary {
public static void main(String[] args) {
//Create library
Library lib = new Library("Surrey
Central Library");
//Create and add some books to
library
Book b = new Book("Book 1", "Author
1", "Publisher 1");
lib.addBook(b);
b = new Book("Book 2", "Author 2",
"Publisher 2");
lib.addBook(b);
b = new Book("Book 3", "Author 3",
"Publisher 3");
lib.addBook(b);
b = new Book("Book 4", "Author 4",
"Publisher 4");
lib.addBook(b);
b = new Book("Book 5", "Author 5",
"Publisher 5");
lib.addBook(b);
Book[] books =
lib.getBooks();
for(int i=0; i<books.length;
i++)
System.out.println(books[i]);
System.out.println("Number of books
in library : " + lib.getNumOfBooks());
}
}