Homework 2
22C:22 Object-oriented Software Development
Due Wednesday, January 31, 2007
15 points
- Starting from textbook example 8.13, called "Order", create a
more complex shopping cart application.
A set of classes (and data files) that you can run and that meets the requirements
is available in this zip file.
You can try the sample either from Eclipse or from a command line.
From a command line, you'd execute 'java Order catalog.text'
In Eclipse, you'd need to add "catalog.text" (without the quotes) as
an program argument (via the window that pops up when you
select Run from the drop-down Run menu).
The original Order program (textbook example 8.13) worked as follows:
- Given an the name of an image file (e.g. gif or jpeg format) as an argument,
the program initially creates a window showing the image, an associated line
of text, and an "Order" button.
- Clicking the "Order" button yields a dialog window in which the user can
enter address and payment information. If "OK" is clicked in the dialog
window, the dialog window closes and the order information is printed. If
"Cancel" is clicked, the window disappears without printing order information.
For this assignment, you must implement a more interesting Order program, similar
to many on-line retail systems. The program must allow a user to
- navigate through a "catalog" of items
- add selected items (perhaps more than one of each kind) to a "cart"
- review what's in the cart, including total cost of items in the cart,
and edit the contents of the cart,
- and, finally, "checkout." In checkout, the user enters address and payment
information exactly as before, but this time the program prints a "receipt"
showing the list of items ordered, total cost, and the address and pavment info.
Detailed requirements for the new Order program:
- instead of an image file, the argument given to the program is a text
file that provides the "catalog" of items. The format is simple.
The first line of the file contains an integer indicating how many different
items (call it n) are in the catalog. After that, there will be n item lines.
Each item line is of the form: itemname price itemimagefile
where itemname is a string (possibly with double quotes around it, so we
can include strings with spaces in them, like "VW Beetle"), price is an integer,
and itemimagefile is the name of an image file (e.g. beetle.gif).
The input file included with hw2sample is catalog.text.
To make this part easy, I'm providing source code for simple Java application,
ReadTest.java, that reads and prints the info from
correctly formatted catalog files (you can assume you'll only get "good" files -
that is, don't worry about error checking on catalog files). It is very easy
to compile and test this program ("javac ReadTest.java" and "java ReadTest catalog.test").
- Each click on the "Add item to cart" button should increase the total number
of that particular item in the current order.
- The "View Cart" window should show the current status of the order. Upon
opening, it should only show items that are actually in the current cart.
Each line should give the item name, price, and show how many of that
item are in the current cart. It must be possible to edit the cart
by changing the number of items ordered (hw2sample shows the current number
in an editable TextBox component). It is okay to change the number to 0
(you don't have to immediately update the window to make that item line disappear,
but if you change it to 0, click "Ok" to close the window, and then click
"Show Cart again" that item should no longer appear.)
The "View Cart" window also should display the current total order price.
It does not need to update this immediately when you edit item amount (though
it would be okay if you implemented a way for that to happen.) Instead you
can simply update the total price when the user clicks "Update total."
- If the cart is empty, the "Checkout" button on the main window should
be disabled (Java provides a simple method to disable buttons - look it up).
While disabled, it should be a good color that while enabled.
- If the cart has items in it, the clicking on the "Checkout" button simply
should pop up a dialog window just like the one in the original Order program.
The main thing you need to do differently now is that when the user clicks
"Ok" on the Order dialog, after entering address and payment info, the program
should (1) print out (to a console, not in a window) the address and payment info along with the full order information
- list of things purchased and total cost, and (2) empty the cart so that
a new order can be started. If the user clicks "Cancel" instead of "Ok"
the program should not print the order and should not empty the cart.
Potentially helpful information:
- In /group/class/c022/Homework2 of the CS Linux machines, we put the code for
textbook example 8.13 along with README files giving details for how to
run the example both in Eclipse and at a command line.
/group/class/c022/Homework2.zip is a zip file of that folder and its contents.
- /group/class/c022/hw2sample and /group/class/c022/hw2sample.zip are
the same sample program given in the link above. You don't need them if
if you've already downloaded using the link above.
It's probably easiest to run hw2sample from the command line (since
I'm not providing your the source files), but in case you want
to try to run it in Eclipse, here
are instructions.