


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



We will be designing, implementing and testing a bookstore application in the next several homework assignments. Each assignment will build on the previous. It will be to your advantage to complete each assignment and do so on-time.
If something is immutable, it means cannot be changed. Java can create immutable data with the final keyword, but that is only useful if you know what the data is ahead of time. Consider books and our Book class. Once a book is created, most of its properties aren’t really allowed to change. Books in the real world don’t (usually) change their titles or ISBN numbers. However there are some properties, such as price that can and will change frequently. To create a safer an more consistent Inventory system, we are going to make some properties of Merchandise immutable. To accomplish this:
We are going to modify our buy and returnMerchandise methods once again, this time to throw exceptions for certain exceptional cases. The new buy method should look like this: public void buy( int sku ) throws MerchandiseNotFoundException If the supplied sku does not match any of the current Merchandise in the Inventory, then the buy method should throw a MerchandiseNotFoundException. The new returnMerchandise method should look like this: public void returnMerchandise( int sku ) throws MerchandiseNotFoundException, MerchandiseNotReturnableException {... } Note the returnMerchandise method no longer returns a boolean. Instead we will throw a MerchandiseNotFoundException if the sku of the Merchandise being returned is not in our Inventory. Also we are adding a MerchandiseNotReturnableException. This will be used later.
As mentioned in the previous assignment, the owners of the Bookstore would like to sell digital merchandise over the Internet. For now they will just sell books, but perhaps they will see music or videos in the future. In order to be flexible, we will create a Downloadable interface that has the following methods: unsigned int getFileSizeInBytes() Create a ElectronicBook class that extends Book and implements Downloadable. ElectronicBook should add the following properties: unsigned int fileSize The fileSize property should also be immutable, and set via the constructor. ElectronicBook should only implement the following methods: unsigned int getFileSizeInBytes() Objects that are Downloadable must be handled differently in your Inventory. Basically, the number of available items is irrelevant since we can make infinite identical copies. Therefore if the Merchandise is Downloadable (hint: instanceof) your buy method should ignore the available count. Also Downloadable objects are not returnable. If a Downloadable object is returned, the returnMerchandise should throw a MerchandiseNotReturnableException.