Docsity

Java CS190 Assignment 5: Immutability, Exceptions, and Downloadable Interface in Bookstore, Assignments of Java Programming

Toggle sidebar
bg1
Assignment 5
Bookstore Application 3
CS190: Java
out: 25 February 2008
due: 8 March 2008
1 Description
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.
2 Implementation Instructions
a. Immutability
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:
•ensure the appropriate data members (SKU, name) are private
•change their respective setter methods to be protected
•create a new constructor for Merchandise that sets the SKU and name when the object is created
•make the default constructor inaccessible
This will certainly affect classes that inherit from Merchandise. You will need to edit these classes also
in order to conform to the new access specifications of Merchandise. Also, you should change the
appropriate properties of classes further down the inheritance tree to be immutable also. When you are
done, your constructors should look like this:
Merchandise(int SKU, String name)
ReadableMerchandise(int SKU, String title, String editor)
Book(int SKU, String title, String editor)
Magazine(int SKU, String title, String editor, Date issueDate)
pf3
pf4

Partial preview of the text

Download Java CS190 Assignment 5: Immutability, Exceptions, and Downloadable Interface in Bookstore and more Assignments Java Programming in PDF only on Docsity!

Assignment 5

Bookstore Application 3

CS190: Java

out: 25 February 2008

due: 8 March 2008

1 Description

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.

2 Implementation Instructions

a. Immutability

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:

  • ensure the appropriate data members (SKU,^ name) are^ private
  • change their respective setter methods to be^ protected
  • create a new constructor for^ Merchandise^ that sets the^ SKU^ and^ name^ when the object is created
    • make the default constructor inaccessible This will certainly affect classes that inherit from Merchandise. You will need to edit these classes also in order to conform to the new access specifications of Merchandise. Also, you should change the appropriate properties of classes further down the inheritance tree to be immutable also. When you are done, your constructors should look like this: Merchandise(int SKU, String name) ReadableMerchandise(int SKU, String title, String editor) Book(int SKU, String title, String editor) Magazine(int SKU, String title, String editor, Date issueDate)

b. Exceptions

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.

c. Downloadable interface

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.