java book store application
HW#01-W3Mon-OnlineBookstore-Rev2.docx Page 1 of 7
TCSS 305A Programming Practicum (Autumn 2019)
Programming Assignment #1 – Online Bookstore Application
Due Date: Fri, Oct 11th, 2019, 11:00 am (Submission I) Mon, Oct 14th, 2019, 11:00 am (Submission II)
I. Problem Definition: In this assignment, you are given some skeleton code for the partial implementation of a simple shopping cart for an online bookstore application. You are going to complete the implementation of the provided Java classes and write the required new codes to implement the provided class APIs below in Part IV.
Following the MVC software architecture pattern, your software will consist of three logical components or packages that contain the relevant classes: • The View component classes, or the front-end codes, implement the presented simple Graphical User Interfaces (GUI)
below.
• The Controller component classes listens to the end user actions and implements them by making changes on the Model classes.
• The Model component classes to be implemented by you will keep and manage the data items. You are going to write mainly the back-end codes to complete the application.
Image Source: https://en.wikipedia.org/wiki/Model–view–
controller
*You will implement the back-end codes for this GUI and test it for the above two cases.
TCSS 305A (Autumn 2019) – Programming Assignment #1
Page 2 of 7
II. Business Rules: 1. Some items may have a bulk quantity discount when purchased more than one items at once and if the customer has a
store membership.
2. The customer should indicate her membership status by clicking on the membership checkbox on the GUI in order to benefit from bulk quantity discounts.
3. When computing the total price for bulk items, apply as many of the bulk quantity as possible and then use the single item price for any leftovers.
For example, if the user purchases seven 3-Ring Binders, the total price will be 2 x 25.00 (for the two sets) + 9.99 (for the remaining single one.)
4. When the customer changes the number of an item in the cart, the old order amount should be replaced with the new one in the shopping cart.
For example, A user might have initially added three items to the cart, and then might want to change it to five. The 5 should replace the previous 3 in this case. And note that for removing an item in the card, the user will enter a zero.
III. Data Dictionary:Item prices and quantities are expressed as real numbers and integers, respectively. 1. Some items have two prices: a single item price and a bulk item price for a bulk quantity. For example, A 3-Ring Binder
normally costs $9.99 per unit, but if a store member purchases three of them as a set she pays only $25.00.
2. The items, line items, and cart are the major classes that manage data in the application as detailed in Part IV.
IV. Implementation Guidelines: 1. Implement the following three classes - using Collections - to complete the given skeleton code and obtain a working application:
• Item • LineItem • Cart
2. An Item object stores information about an individual item. It must have the following public methods:
Warning: Note that the names (such as price and bulk quantity) given here are not meant to be the actual parameter names (Checkstyle would complain about them); they’re meant to be descriptive.
Method Description
Item(String name,BigDecimal price) Class constructor
Item(String name, BigDecimal price, int bulkQuantity, BigDecimal bulkPrice)
Class constructor
BigDecimal getPrice() returns the single item price for this Item.
int getBulkQuantity() returns the bulk quantity for this Item.
BigDecimal getBulkPrice() returns the bulk price for this Item.
Boolean isBulk() returns True if the Item has bulk pricing; false otherwise.
TCSS 305A (Autumn 2019) – Programming Assignment #1
Page 3 of 7
String toString()
returns a String representation of this Item: name, followed by a comma and a space, followed by price.
If the item has a bulk price, you should append an extra space and a parenthesized description of the bulk pricing that has the bulk quantity, the word “for” and the bulk price. 1 See the examples below.
boolean equals(object)
returns true if the specified object is equivalent to this Item, and false otherwise.
Two items are equivalent if they have exactly equivalent names, prices, bulk quantities and bulk prices.
This method must properly override java.lang.Object.equals().
int hashCode()
returns an integer hash code for this item.
This method must override java.lang.Object.hashCode() and be consistent with equals().
1Note: The String representation of an Item must exactly match that shown in the GUI screenshot above:
a) an Item named “X” with a per-item price of $19.99 and no bulk quantity would have the String representation “X, $19.99” (without the quotes);
b) an item named “X” with a per-item price of $19.99, a bulk quantity of 5, and a bulk price of $89.99 would have the String representation “X, $19.99 (5 for $89.99)” (without the quotes).
The format of these String representations should/will be tested.
In the Item class, you need to construct a String representation of the price. An acceptable way to format the output would be to use the format() method of the String class. This is not easy to do for several reasons, but Java provides a convenient built-in class that will do it for you. It is called NumberFormat and is part of the java.text package (so you need to import java.text.NumberFormat).
You obtain a number formatter by calling the static method named getCurrencyInstance(), as in: NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
You can then call the format() method of this object, passing it the price as a BigDecimal, and it will return a String with a dollar sign and the price in dollars and cents. For example, you might say:
BigDecimal price = BigDecimal.valueOf(38.50);
String text = nf.format(price);
This would set the variable text to "$38.50".
Warning: Note that you only need one NumberFormat reference, and you can use it repeatedly. Do not create a number formatter every time you need to format a number, as that would be wasteful of memory (and would cost you points for redundancy in your implementation). Instead make a single NumberFormat reference that can be shared by all Item objects.
TCSS 305A (Autumn 2019) – Programming Assignment #1
Page 4 of 7
3. A LineItem object stores information about a purchase order for an item: namely, a reference to the item itself and the quantity desired. It must have the following public methods:
Method Description
LineItem(String item, int quantity) Constructor that creates an item order for the given quantity of the given Item.
Item getItem() returns a reference to the Item in this LineItem.
int getQuantity() returns the quantity for this LineItem.
String toString() returns a String representation of this LineItem: You may use any format that seems reasonable to you for this string.
4. A Cart object stores information about the customer's overall purchase. One field must be a collection of some type to hold information about all the item the customer has added to her shopping cart.
You should use a generic collection from the Java Collections Framework in your implementation. One possible choice, though perhaps not the easiest to work with, is List<ItemOrder>.
The Cart class must have the following public methods:
Method Description
Cart() Constructor that creates an empty shopping cart.
void add(ItemOrder order)
adds an order to the shopping cart, replacing any previous orders for an equivalent item with the new order.
(equals() would return true if used to compare equivalent items)
void setMembership(boolean value) sets whether or not the customer for this shopping cart has a store membership (the parameter is a boolean; true means the customer has a membership, false means the customer doesn’t). The return is void.
BigDecimal calculateTotal() returns the total cost of this shopping cart as a BigDecimal. This returned BigDecimal should have scale of 2 and use the ROUND_HALF_EVEN rounding rule.
void clear() removes all orders from the cart. The return is void.
int getCartSize() returns the number of LineItems currently in the cart.
String toString() returns a String representation of this Cart. You may use any format that seems reasonable to you for this String.
One technique (which is not the best one, but will certainly work) is to use an ArrayList to implement your Cart. If you do, the methods you are most likely to be interested in are the following:
Method Description
ArrayList<T>() Constructor that creates an empty ArrayList to hold elements of type T.
void add(<T> value) adds the given value to end of the ArrayList. The parameter type is T, so only the type you specified at construction can be added to the ArrayList.
<T> get(index) gets the item at the given index (0-based). The return type is T.
void set(index, value) sets the entry at the given index to be the given value.
void remove(index) removes the value at the given index.
TCSS 305A (Autumn 2019) – Programming Assignment #1
Page 5 of 7
String toString() returns a string representation of the list.
int size() returns the number of values stored in the list.
5. You should not change any method signatures or return types defined in this assignment. You may change the parameter names, if you like. 6. You should not introduce any other non-private methods to these classes, although you may add your own private helper methods. 7. You must override toString in these classes (you may find this helpful for testing and debugging). 8. You must also override the equals and hashCode for the Item class, as described above. You are also allowed to override other (non-final) methods declared in java.lang.Object, such as equals and hashCode.
Warning: If you do, however, your definitions must be reasonable and consistent with each other; if you implement inconsistent equals and hashCode methods, you will certainly lose points. You must also override the equals and hashCode for the Item class, as described above.
9. As you develop your classes do not code any dependencies based on the current list of items. That is, your classes should still work correctly if the list of items is replaced with an entirely different list of items.
For this assignment, you should code defensively: 10. You should test setters and constructors for invalid values.
11. You should throw IllegalArgumentException (explicitly) for any of the following conditions: prices passed to your classes are < 0, quantities passed to your classes are < 0, String parameters passed to your classes are empty.
12. You should throw NullPointerException (implicitly) for any of the following conditions: String parameters passed to your classes are null, BigDecimal objects passed to your classes are null, the Item passed to the ItemOrder constructor is null, the ItemOrder passed to the add() method in Cart is null.
(Of course the behavior for overridden methods from class Object, such as the equals() method, are defined in class Object.)
V. Stylistic Guidelines: The program style will affect your final grade, therefore be careful to use descriptive variable names and full Javadoc comments on each method. The provided BookstoreFrame and BookstoreMain classes have full Javadoc comments that would be considered acceptable in a homework submission. Notice that the provided classes (other than the skeletons you must fill in) have no Checkstyle, FindBugs, or PMD, warnings.
1. You must include a header comment at the beginning of each file with some basic information, in addition to full Javadoc comments.
Examples of acceptable file headers (including the class Javadoc comment) appear in Assignment 0 and in the provided BookstoreFrame and BookstoreMain classes.
2. Note that the class skeletons provided for you have no comments of any kind; adding the comments is a critical part of the assignment.
3. It is generally a good idea to try to eliminate all (or as many of the) warnings before submitting your code.
If you have questions about what a warning means, post your question on the Canvas discussion forum without posting actual code from your project. It is perfectly OK to help your classmates in explaining the reason why it style checker complains and how to avoid the warning.
TCSS 305A (Autumn 2019) – Programming Assignment #1
Page 6 of 7
VI. Verification & Testing of Your Code 1. Your classes are to exactly reproduce the views shown in the two screenshots above following the guidelines in this assignment. 2. You should run the GUI and enter the individual quantities from the screenshots to verify that your classes are working correctly (You will write unit tests that does so in the next assignment.) 3. No console output should appear when running your program or your unit tests; get rid of the console outputs that you might have used during your development and debugging by commenting them before submitting your assignment.
VII. Submission and Grading:
Note that, You can find the detailed instructions for some of the following items in the hw0-project description or in the Guidelines documents posted under the Canvas Files > Guidelines folder).
For example, an executive summary template, which you must use, is available on the Canvas under Files > Templates folder.
As with the naming convention for your Eclipse project, your assignment will lose points if it does not follow the guidelines properly.
1. Create your Eclipse project by downloading the hw1-project.zip file from Canvas, importing it into your workspace (as described for hw0-project.zip in Assignment 0 or in documents posted in the
2. Use “Refactor” to change “username” in the project name to your UWNetID. Remember to make this change before you first commit the project to Subversion.
3. (SUBMISSION I) You must submit the following as text using the given text areas on the Canvas at least 3 days before the code submission – You will use your first attempt to submit these, and the second attempt to submit the rest by the assignment deadline:
• a Project Plan (about one page of 250 words) bulleted/itemized list of ordered tasks (i.e., Download source codes, Review source codes, Build the codes, Try out the program, Read Array Lists from Core Java, … ) that you have to do in the context of this assignment. You can use indentation to indicate parallel tasks, if you need.
• a Proposed Technical Solution, (about one page of 250 words) explaining how you plan to attack the implementation problems. You can write here pseudo algorithms, drafts/sketches of partial codes, notes on data or control flows between different classes, how to use a certain collection class in your solution, etc. You can also note your questions and challenges still not resolved.
4. (SUBMISSION II) You must check your Eclipse project into Subversion, including all configuration files that were supplied with it (even if you have not changed them from the ones that were distributed).
When you have checked in the revision of your code you wish to submit, make a note of its Subversion revision number. To get the revision number, perform an update on the top level of your project; the revision number will then be displayed next to the project name. Your revision number will pick up where you left off on Assignment 1; if you submitted revision 12 of Assignment 1, the first commit of Assignment 2 will have a number greater than 12. This is because you have a single Subversion repository for all your projects, and the revision number counts revisions to the entire repository.
TCSS 305A (Autumn 2019) – Programming Assignment #1
Page 7 of 7
5. (SUBMISSION II) After checking your project into Subversion, you must submit the following on Canvas by typing them in the given textareas:
• an Executive Summary, containing the Subversion revision number of your submission,
• an Assignment Overview (1 paragraph, up to about 250 words) explaining what you understand to be the purpose and scope of the assignment, and
• a Technical Impression (1-2 paragraphs, about 200-500 words) describing your experiences while carrying out the assignment.
• List of Test Cases that you used to test your program thoroughly. You should avoid using test cases that have the same behavior and try to find distinct cases that will test different aspects of the application logic. The GUI screenshots given in Part I, for example, show the total price in the case of bulk item purchasing for members and non-members.
6. (SUBMISSION II) The filename for your executive summary must be “username-hw1-project.txt”, where username is your UWNetID.
Your executive summary must have a line containing exactly the text “Subversion Revision Number: #”, with no leading spaces, where “#” is the Subversion revision number you made a note of above (with no parentheses or other symbols).
Executive summaries will only be accepted in plain text format – other file formats (RTF, Microsoft Word, Acrobat PDF, Apple Pages) are not acceptable.
7. Part of your program's score will come from its "external correctness." For this assignment, external correctness is measured by the output generated (correct calculations, correct behavior when values are entered, changed and deleted, correct discount behavior, etc.), and is determined by running automated tests on your code by the instructor.
8. Another part of your program's score will come from its "internal correctness." Internal correctness includes meaningful and systematically assigned identifier names, proper encapsulation, avoidance of redundancy, good choices of data representation (though this is not applicable to this assignment), the use of comments on particularly complex code sections, and the inclusion of headers (as described above) on your classes.
Internal correctness also includes whether your source code follows the stylistic guidelines discussed in class. This includes criteria such as the presence of Javadoc comments on every method and field (even private ones!), the use of variable names, spacing, indentation, and bracket placement specified in the class coding standard, and the absence of certain common coding errors that can be detected by the tools.
It is therefore to your advantage to be sure the plug-in tools like your code before you submit it.