OOP Design & Analysis: BookStore Management Project — Part III

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

Let’s make some improvement to our program

Task 1: Improvement requirements

Once the application is deployed, the Book Tiger company observed some problems with the program:

Problem 1: the current program only allows 50 books to be inputted, and the company does not want this. They want to input as many books as they want.

Problem 2: if employees mistakenly input wrong values, for instance, they input a string for the prompted price, the program will crash. The company wants to inform employees that they have inputted invalid values. Even if there are some unexpected behaviors, the program will keep working.

They want to restrict the inputs as follows:

  • ISBN must follow the format: ISBN-xxx-xxxx, where x is a digit from 0–9
  • Published year must be 4 digits
  • Price must be greater than 0

Problem 3: all the books are displayed in order the way they were added, which is sometimes a bit hard to look for a certain book. They want to view all books in ascending order based on the book published years.

Problem 4: currently, the printed book list is ugly. The company wants to show a book list in tabular format with table headers as well.

Problem 5: If the program or the computer restarts/shuts down for some reason, all data will be lost. They want to permanently keep all the data in a formatted text file unless their employees explicitly remove one.

Task 2: a proposed solution for problem 1

To store a list of books, currently, the program uses an array data structure. Array by itself has some limitations. One of those limitations is a fixed size. This means developers need to provide in advance a fixed number of elements for the array before making it in use.

Once you have reached the maximum size, you cannot add more elements to the array. If you need to add more and reserve the added elements, there are two tasks needed to be accomplished:

1. Create a brand new array, also with a new fixed size

2. Copy previously added elements to the new array

Those processes are painful, time-consuming, and error-prone.

To overcome the array’s limitations, Java provides a list of data structures or collections in the Collections Framework.

If you have not worked with Collections Framework, you might want to give yourself some time to get familiar with:

https://docs.oracle.com/javase/tutorial/collections/intro/index.html

As for our task, I will use the List interface and its implementation of ArrayList for the sake of performance.

Instead of replacing the old class (BookListWithArray), I will create a brand new class in the same package called BookListWithCollection. This way I can always go back to the old array version whenever required.

And of course, the BookListWithCollection has almost the same structure as its counterpart:

Try to implement the rest for yourself. I believe you can do it.

Once you have done the implementation of the new class, there only one thing you need to do to use the new version.

Navigate to MainProgram.main() method, and replace:

BookInterface list = new BookListWithArray();

with:

BookInterface list = new BookListWithCollection();

And that’s it.

Because from the beginning, we have defined business methods in the interface BookInterface, we just need to change the implementation (BookListWithCollection) and everything will work the same way as before. This is the beauty of using interfaces.

Task 3: a proposed solution for problem 2

To validate the input from users, I would recommend applying Regular Expression techniques.

When users input the wrong format, we need to inform the users of the appropriate message and allow the users to input again. Therefore, we need to create a separate method for each input checking.

Here is one to check ISBN format:

To prevent the program to be crashed when there are some unexpected events, exception handling should be applied.

Read the following tutorial: https://docs.oracle.com/javase/tutorial/essential/exceptions/index.html

Then, try to apply to the program.

Hints: for add(), update(), remove() methods, throws Exception class. For instance:

Then, in the main() method, surround these method callings with try-catch block.

Task 4: a proposed solution for problem 3

The requirement is to print all books’ details in ascending orders based on published years.

Therefore, we need to sort our book list before printing out.

Since items in the list are book objects, not primitive types, we need to specify how we want to sort those objects.

In order to sort a list of objects, Java language provides 2 interfaces: Comparable and Comparator.

Read some related tutorial before jumping into implementation: https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html

In our case, using Comparable is enough.

First, make the Book class implements the Comparable interface

Second, override the compareTo() method and specify the attribute you want to compare, which should be the published year in this case

Finally, calling the helper method Collections.sort(list) to sort the list before outputting the list

Task 5: a proposed solution for problem 4

According to the requirement, the output should be in tabular format, something likes:

In order to format a string, first, you need to use the method:

System.out.format()

Then follow the necessary flags and conversions.

Read the tutorial at:

https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html

If you come from C language background, you should find the tutorial very easy to adapt.

Because the method System.out.format() is very similar to the printf() function in C language.

Task 6: a proposed solution for problem 5

The requirement is to store permanent data in a formatted text file, I will use a CSV file for this task.

CSV stands for comma-separated values. It is just a text file storing data line by line. In each line, commas are used to separate pieces of data.

Here is an example if we use a CSV file (data.csv) to store our list of books:

ISBN-123–1234,99.9,2009
ISBN-986–4748,88.99,2012
ISBN-342–39387,12.99,2004

CSV file can be imported or exported from a tabular supported program such as Microsoft Excel.

If you open data.csv file in MS Excel, it looks like this:

So, reading and writing to a CSV file is just a matter of any text file.

In our case, the process of reading from data.csv file is as follows:

  • Reading line by line
  • For each line, use a comma as a delimiter to get each piece of data. There are different ways to achieve this such as String.split(), java.util.Scanner, and java.util.Stringtokenizer. For convenient purposes, I will go for Scanner class

The process of writing to data.csv as follows:

  • Combine all pieces of data of a book (ISBN, price, published year) into a comma-separated string
  • Write the string as a line in the data.csv. To achieve this task, I will use the PrintWriter class.

However, be noticed that we only need to perform reading and writing at two events:

  • When the program first starts, we read all the data from the data.csv file. For each fetched data of a book, create a new book object and add it to the list. Then, every other adding/updating/removing data from a list is conducted as normal
  • When users quit the program, we get all the data in the list and save it to the data.csv file. The actual process is to override the old data.csv with the new data.csv file.

Here is the detailed implementation for the solution:

Step 1:

We need to declare the two methods for file reading and writing in the BookInterface:

Step 2:

At this point, you will get compiling errors in both BookListWithArray class and BookListWithCollection class because these two classes have not implemented the two new methods in the BookInterface yet.

Since we are not going to support file reading and writing features in the BookListWithArray class, we just need to throw UnsupportedOperationException:

We will implement those two new methods in BookListWithCollection class:

Get stuck? You can download the complete source code here:

https://github.com/stackera/LearnOOP

This is The End and Happy coding

--

--