OOP Design & Analysis: BookStore Management Project — Part II (Cont.)
Keep implementing our project
Task 10: solution for BookListWithArray.add()
Here is one possible solution for the add()
method:
Task 11: implement class BookListWithArray: update() method
Here is the pseudo-code for the update()
method:
– Ask users for the updating isbn
– If the isbn not exist: print out “This book does not exist”
– Else
» ask users for new price
» Ask users for the new published year
» Update the book with the new price and new published year
Try to implement yourself first. Once you have done or get stuck, check out the provided solution in the next task.
Task 12: solution for BookListWithArray.update()
Task 13: implement class BookListWithArray: remove() method
Here is the pseudo-code for the remove() method:
– Ask users for the removing ISBN
– If the isbn not exist: print out “The book does not exist”
– Else
» remove the book in the list by shifting up the array elements up to the removing book index
Try to implement yourself first. Once you have done or get stuck, check out the provided solution in the next task.
Task 14: solution for BookListWithArray.remove()
Task 15: implement class BookListWithArray: print() method
The print()
method lists all the books in the array.
Here is the pseudo-code:
- Check if there are any books on the list. If there is none, then an appropriate message should be displayed and exit the method
- Otherwise: traverse the array and print out all the books
Again, try to implement yourself first. Once you have done or get stuck, check out the provided solution in the next task.
Note that, in the Book class, we have already implemented the toString()
method which returns the book’s details. So, if we have:
Book book = new Book();
Then, invoking:
System.out.println(book);
is exactly the same as:
System.out.println(book.toString());
Task 16: solution for BookListWithArray.print()
Task 17: implement Menu class
As the name suggests, this class provides a list of operations so that users can select on-demand.
The production from the Menu class looks something like:
Why do we need a class to hold these menu items?
Because we are programming using OOP. Everything needs to be encapsulated in an object. To have objects, we need classes. Furthermore, by defining a separate class for Menu
, later on, it would be easy to change the way we implement the menu without affecting the main program.
The basic ideas for implementing the Menu class
- A list of menu items needed to be held, hence we need an array of String
- We need a method to add a menu item to the array. But before adding menu items to the array, we might need a constructor to initialize the array. Let’s first go for initialization of 5 items
- We need a method to print out all available menu items and let users select an option and return an integer representing the selected operation
Here is the skeleton of the Menu class:
Why don’t you try to implement these methods yourself first?
Don’t worry, if you don’t feel like it, check out the source code in the next task
Task 18: the Menu class source code
Task 19: Put everything together (Or connect all objects together)
Now everything needs to be put in the MainProgram
to build up complete business logic.
There are 3 main sub-tasks we need to accomplish:
Build the menu so that users know what features the program supports
Next, initialize the BookListWithArray
class to provide functional requirements:
Finally, we need to provide users a repetitive way to use the features.
There are many options to achieve this task, but I find the do..while
loop best for this purpose:
Complete code for the MainProgram
class:
This is the end of Part II.
Let’s move to the next part for improvement