OOP Design & Analysis: BookStore Management Project — Part II

Sera Ng.
Tech Training Space
6 min readOct 19, 2020

Part II: Implementation

Task 1: Initialize the project

In your Netbeans (or your preferred IDE), create a project structure as follows:

If you are using Netbeans, you can download the project template here

In each package, create classes and interface as defined in previous tasks.

In Java program, a package is a way to group classes/interfaces or other program resources. You can think of packages as folders in your computers so that you can organize all your files and other folders in a manageable way.

You can have many source files with the same name as long as they are in different packages.

In Java, the class name must have the same name as the source file’s.

You can download this project structure here to follow along if you desire.

Task 2: Implement Book class

Open the Book.java file and add the following code:

First, we need to define data or attributes for the Book class:

We have 2 keywords that need to discuss here: public and private.

In Java, these are called access modifiers which are the way we hide or limit access to object data from the outside world.

And this is the implementation of the concept of Encapsulation that we discussed in the previous task.

In Java, we have totally 4 access modifiers with the scope of access as follows:

When defining a class, you follow the Encapsulation policy: all data should be private and each of those should be accessed via getter and setter only.

Therefore, we need to define getters and setters:

Pay attention to the name convention for the getters and setters.

Tips: in Netbeans (and as in other IDEs) you can quickly generate getters and setters: right click anywhere in your current class, select Insert code…

Then, select Getter and Setter… menu item

Then check attributes that you want to generate getters and setters, and click Generate:

Task 3: Reading access modifiers and toString() method

Your task is to read the following tutorials:

Access modifiers: https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

The Object class as Supper class: https://docs.oracle.com/javase/tutorial/java/IandI/objectclass.html

Once you have done, proceed to the next task

Task 4: define interface BookInterface

In Java (or other OOP languages), an interface declares a list of abstract methods. Abstract methods are those that do not have implementation yet.

It is the responsibility of the class implementing that interface to implement those abstract methods.

Note that we don’t need to use the keyword public for methods in an interface because they are public by default.

And you might raise a question: what is the purpose of the interface? Because it seems that this interface does nothing except declaring those empty methods.

You are right.

An interface standing alone by itself is completely useless. Interfaces only gain significant values when they are implemented, which means those abstract methods are defined in the implementing class.

This is also the case for hardware interfaces we have seen in previous examples. For instance, a USB interface is completely useless if it stands alone by itself and not connected to any USB devices. It only produces values when there is a USB device (e.g a pen drive) connecting to it (or implementing).

The significant role of an interface is that it defines a standard way for communicating between involved parties (or objects). You can have one interface but as many implementations as you might want. Like we have one USB interface, but we can have different USB implemented devices such as pen drive, mouse, etc..

Task 5: implement class BookListWithArray: member variables and constructor

First, let’s make BookListWithArray class implement the BookInterface:

A class can implement many interfaces. An interface can even extend/inherit from many interfaces.

And keep in mind that once a class implements an interface, it needs to implement ALL abstract methods. Otherwise, the Java compiler produces compiling errors.

Before implementing abstract methods in BookInterface, we need to define some variables to store data:

The count variable keeps track of the current number of elements in the array.

The MAX_SIZE variable defines the maximum number of elements in the array. The keyword final makes this variable constant, which means it cannot be modified.

The list array of Book type stores a list of book objects. And I use a constructor to initialize the size for this array.

A constructor is a special method belonging to the current class. This method is automatically called when the class is instantiated.

You can have as many constructors in a class as you want, as long as they are different in signature (parameter types, orders, and numbers)

A constructor does have any parameter, like in this case, is called default constructor. If you don’t specifically create a constructor, a class always has an implicit constructor.

But if you have defined a parameterized constructor, then your class does not have the implicit default constructor. Unless you explicitly define one.

Task 6: more reading on the constructor

Take some time and read: https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

Task 7: implement class BookListWithArray: find() method

Since we have one constraint in the requirement: each book needs to have a unique ISBN, we will define the find() method for checking the existence of an ISBN before adding to our system.

Not only for duplicated checking, but we will also use the find() method to search for an existing book object for the purpose of updating or deleting operations.

The method returns an integer which the index of the found book object in the array. If there is no book object with the supplied ISBN, -1 is returned.

Notice that in the for loop condition, I used the count variable, not list.length. That’s because list.length always returns the length of the array, which is 50 in our case. The count variable keeps track of the current number of elements in the array. Therefore, if using list.length, we might traverse through empty elements.

And since ISBN is a String type, it is highly recommended to use the equals() method. The equals() method returns true if the two strings are exactly the same. Otherwise, false is returned.

Task 8: more reading on String and String comparison

Strings: https://docs.oracle.com/javase/tutorial/java/data/strings.html

String comparison: https://docs.oracle.com/javase/tutorial/java/data/comparestrings.html

Task 9: implement class BookListWithArray: add() method

Here is the pseudo-code for adding a book object to the array:

If the array is full: print out “list is full”

Else

– Ask users for ISBN

– Check if ISBN is already on the list. If it is:

» Print out “ISBN is already in the system”

» Exit the method

– Ask users for a price

– Ask users for a published year

– Create a new book object with new inputted values

– Add book object to bookList array

– Increase the Count variable

Try to implement yourself first. Once you have done or get stuck, check out the provided solution in the next task.

Hint: to get data from the console input, you can use the java.util.Scanner class.

[Read next part for next tasks]

--

--