Home > Developing > Java-Programmed Library System
Free Sample: Java-Programmed Library System paper example for writing essay

Java-Programmed Library System - Essay Example

Computer Science (Bsc)

Abstract A simple information system for the school library was designed, where records of books could be added, deleted or printed to the screen. The classes Record and Library were created in order to represent the main purpose of the project, which was storing information regarding a particular book in a collection of book records allocated in the Library class. From the Library class we can access these records, edit them, or get the data contained in them. We can also get a list of all the records in the collection printed out to the screen.

1. Introduction

The laboratory session was carried out with the use of a programming utility for Java named BlueJ. The theory, and main objective, of java programming is the interaction between objects. An object is regarded as an instance of a class, which can be seen as a set of instructions towards the construction and interactivity of the object – its data and behaviour. So while a class is responsible for characterising the objects, these are in charge of interacting with other objects (defined by other classes), if that is the programmer’s will.

In this lab session only 2 classes were used, Library and Record. The programming environment in which this laboratory was produced can be witnessed in the figure below. The objects are represented by the red rectangles in the bottom, while the two classes in use are above the objects, respectively named. The arrow from Library to BookRecord shows that Library uses objects from the other one, which will be verified throughout the report.

Figure 1: The programming environment provided by BlueJ

2. The BookRecord class

This class was designed to represent a record of a book. Therefore, we should have attributes such as title, author name, publishing year and number of copies (which we call instance variables or fields). We should have a way to access these fields, so accessor methods were created, in order to retrieve the values represented by these instance variables. Of course, each class has to contain a constructor, with which an instance of that class is created. So the first method we implemented was this constructor method, which would take values for each of the fields defined above (we call these values parameters passed onto the method), and create a book record. The code for the constructor is as follows:

public BookRecord(String author, String title, int year, int copies)

{

// Create a new book record, error check everything using exceptions

try {

// Check author is valid

if(author == null || author.length() == 0) {

throw new IOException(“No author entered”);

} else {

this.author = author;

}

// Check title is valid

if(title == null || title.length() == 0) {

throw new IOException(“No Title entered”);

} else {

this.title = title;

}

// Get the year and check book isn’t from the future

Calendar cal = new GregorianCalendar();

int currentyear = cal.get(Calendar.YEAR);

if(year > currentyear || year < 0) {

throw new IOException(“No time travelling books”);

} else {

this.year = year;

}

// Check number of copies is valid

if(copies < 0) {

throw new IOException(“Too few copies”);

} else {

this.copies = copies;

}

} catch(IOException e) {

// If we have an error, print it

System.out.println(“Error Creating Book Record: ” + e.getMessage());

System.out.println();

}

}

Code sample 2: The BookRecord constructor

Code reprinted from the .java document on this specific lab (BookRecord.java by Joao R. Goncalves)

This code sample uses Java exception handling to keep track of possible errors, for later debugging, if necessary. The other methods in the class were simple accessor methods designed to return the value contained in a specific field. For example, methods such as getAuthor(), getTitle() and getYear() returned the author name, title of the book and year of publication, respectively. This makes it possible for other classes to access the information of the one in question, without allowing them to edit or delete the values of the fields (unless we instruct the fields to be “editable” by other classes, by making them public instead of private).

3. The Library class

In this class we want to store all the books, be able to edit or delete them, as well as print information regarding those books. For the purpose of managing the books in the library created an ArrayList where we could add the books using the method addBookRecord(). This method took four parameters in order to create a Book object and add it to the list. We would also like to get all of the books printed out, so a listAllBooks() method was implemented, which would present us with the list in case there were any books inserted. To print all the records a for-loop was used to iterate through the ArrayList and print its details.

The big challenge was to create a method to search the list for a specific book, or a book by a particular author. Named as queryBookList, this new method would take either one or two string parameters, according to whether the user wanted to search for just an author or book, or both of them. Once again, a for-loop was implemented to go through all the book records and return us the details of the books matching the values inputted by the user. In case a book was to be found by its title, the method would only try to match the title entered with the ones in the book list, and the same applies if the user wanted to search by author. This is ensured by the if-statements in the code for this class (See Code Sample 2). If one or more books are found, the details for each one of them are printed to the screen. On the other hand, if no books match the search criteria a message appears: (“Books found: ” followed by “None.”). The code for this class is as follows:

import java.io.*;

import java.lang.*;

import java.util.*;

public class Library

{

ArrayList booklist;

/**

* Constructor for objects of class Library

*/

public Library()

{

booklist = new ArrayList();

}

/**

* Add a book to the library

* @param author The author of the book

* @param title The title of the book

* @param year The year the book was published

* @param copies The number of copies in stock

* @return a reference to the BookRecord

*/

public BookRecord addBookRecord(String author, String title, int year, int copies)

{

BookRecord book = new BookRecord(author, title, year, copies);

booklist.add(book);

return book;

}

/**

* List all the books

*/

public void listAllBooks()

{

// Check we have books

if(booklist.size() == 0) {

System.out.println(“No books on file.”);

System.out.println(“”);

return;

}

// Print all the books

System.out.println(“Books found:”);

for(int i=0; i&lt;booklist.size(); i++)

{

BookRecord book = (BookRecord)booklist.get(i);

book.printDetails();

}

System.out.println(booklist.size() + ” books on file.”);

System.out.println();

}

/**

* Query for a book based on author or title

*

* @param author The author of the book, null to not search for this

* @param title The title of the book, null to not search for this

*/

public void queryBookList(String author, String title)

{

// Print header

System.out.println(“Books found:”);

int booksfound = 0;

for(int i=0; i&lt;booklist.size(); i++)

{

// Get the book

boolean found = true;

BookRecord book = (BookRecord)booklist.get(i);

// If author is specified, check equality

if(author != null)

if(book.getAuthor() != author)

found = false;

// If title is specified, check equality

if(title != null)

if(book.getTitle() != title)

found = false;

// If both tests have been passed, print book

if(found)

{

book.printDetails();

booksfound++;

}

}

// Print footer

if(booksfound == 0)

System.out.println(“None.”);

System.out.println(“End of query.”);

System.out.println();

}

}

Code sample 2: The Library Class

Code reprinted from the .java document on this specific lab (Library.java by Joao R. Goncalves)

Some tests were run after the library class was complete, in order to determine if these were working properly. These included adding records to the book list, and then printing them to the screen, as well as searching the list for a specific book or author. To carry out these functionality tests, some books were added to the Library using the addBookRecord(), and then the listAll() method was executed, which printed the results in Figure 2. The queryBookList method was then called, to retrieve the books written by the author Peter Grossman, and the output can be checked in Figure 3. The images of the results of the tests are available below:

Figure 2: The list of books in the ArrayList

Figure 3: The search results for books by Peter Grossman

4. Conclusions

The laboratory session provided useful knowledge on the use of data collection objects such as ArrayList’s, as well as methods to iterate through them in order to retrieve data. Also, the interaction between objects becomes clearer, as it is the main idea behind object-oriented programming languages such as Java or C++.

5. References

COMP1004 – Programming Principles lab 3 challenge specifications on project B. Viewed on 28-10-04 at https://secure.ecs.soton.ac.uk/notes/comp1004/Resources/Week3/lab03.html