ITEC 120 | Back to Assignment index page |
Assignment 5
- Shopping Submit your program to the RU11 in your folder on neelix. |
Description:
You will design and implement a system which simulates items in a shopping cart. You may start with Item.java and ShoppingCart.java which you used in lab 22.
You will need to add a barcode (as instance data, an int) to Item. barcode will be a parameter passed to the constructor.
Write a new driver which will ask the user for a barcode, and, if it is a valid barcode, add the appropriate item to the cart. Quit when the user enters 0.
The driver will have a list of item names and another list of item prices associated with barcodes. In a real system, there might be hundreds of thousands of items, but in our system, we'll just use 25 items. Assume barcode 100 through 124 correspond to these items (you can copy and paste into your program):
"Tomato paste", "Chicken
soup", "Candy corn", "Bag of Potatoes",
"Cheddar cheese", "Bacon ", "Box
of Donuts", "Bag of Onions",
"Orange juice", "Gallon of milk", "Butter ",
"Frozen pizza",
"Frosted Flakes","Loaf of bread", "Popcorn ",
"Pound of Coffee",
"8 oz. Yogurt", "Potato chips", "2 Liter Pepsi",
"2 Liter Coke",
"Pinto beans", "Oreo cookies", "Salsa ",
"Corn chips", "Box of crackers"
and these prices:
.34, 1.23, .99, 2.39, 3.49, 2.89, 3.09,
2.49, 1.79, 2.29, 2.19, 3.89, 3.19, 1.29, 3.47, 7.58, .79, 3.29, .99, 1.19,
.78, 3.59, 3.29, 2.89, 2.39
So, barcode 100 is Tomato Paste, and it costs 34 cents. Barcode 101 is Chicken soup for $1.23. Think about how you can use arrays to make the lookup of these items easy. (Use these lists in this order so that your output will be correct.)
After the user enters a valid barcode, ask them for a quantity. Then add those items to the cart array. If an item already exists in the cart array, you should not add it to the array again but instead update the quantity of the item already in the cart array. For example, if the user adds 2 boxes of donuts, but there are already 3 boxes of donuts in the cart array, you should have one element in the array which represents 5 boxes of donuts. To help you accomplish this task, you should write a .equals method for item. Two items are the same if they have the same barcode.
After the user finishes entering barcodes, print out the contents of the cart, along with the total prices of all items.
Then, sort the items by total price and print the contents again. This list will show the user how much money was spent on each item, highest cost first. To accomplish this task, you use the sort method in Sorts.java, which sorts an array of any Comparable objects. So you will need to make Item comparable, which involves writing a compareTo method.
Program displays: |
Please enter a barcode (100 thru 125) for an item, 0 to quit |
User enters: |
22 |
Program displays: |
barcode must be between 100 and 125. |
User enters: |
1 |
Program displays: |
barcode must be between 100 and 125. |
User enters: |
121 |
Program displays: |
Please enter quantity: |
User enters: |
-3 |
Program displays: |
Please enter a valid quantity: |
User enters: |
2 |
Program displays: | Please enter a barcode (100 thru 125) for an item, 0 to quit |
User enters: |
105 |
Program displays: | Please enter quantity: |
User enters: |
1 |
Program displays: | Please enter a barcode (100 thru 125) for an item, 0 to quit |
User enters: |
103 |
Program displays: | Please enter quantity: |
User enters: |
2 |
Program displays: | Please enter a barcode (100 thru 125) for an item, 0 to quit |
User enters: |
102 |
Program displays: | Please enter quantity: |
User enters: |
8 |
Program displays: | Please enter a barcode (100 thru 125) for an item, 0 to quit |
User enters: |
109 |
Program displays: | Please enter quantity: |
User enters: |
3 |
Program displays: | Please enter a barcode (100 thru 125) for an item, 0 to quit |
User enters: |
105 |
Program displays: |
Please enter quantity: |
User enters: |
3 |
Program displays: | Please enter a barcode (100 thru 125) for an item, 0 to quit |
User enters: |
111 |
Program displays: | Please enter quantity: |
User enters: |
1 |
Program displays: | Please enter a barcode (100 thru 125) for an item, 0 to quit |
User enters: |
105 |
Program displays: | Please enter quantity: |
User enters: |
3 |
Program displays: | Please enter a barcode (100 thru 125) for an item, 0 to quit |
User enters: |
112 |
Program displays: | Please enter quantity: |
User enters: |
2 |
Program displays: | Please enter a barcode (100 thru 125) for an item, 0 to quit |
User enters: |
113 |
Program displays: | Please enter quantity: |
User enters: |
1 |
Program displays: | Please enter a barcode (100 thru 125) for an item, 0 to quit |
User enters: |
105 |
Program displays: | Please enter quantity: |
User enters: |
2 |
Program displays: | Please enter a barcode (100 thru 125) for an item, 0 to quit |
User enters: |
123 |
Program displays: | Please enter quantity: |
User enters: |
1 |
Program displays: | Please enter a barcode (100 thru 125) for an item, 0 to quit |
User enters: |
124 |
Program displays: | Please enter quantity: |
User enters: |
7 |
Program displays: | Please enter a barcode (100 thru 125) for an item, 0 to quit |
User enters: |
109 |
Program displays: | Please enter quantity: |
User enters: |
6 |
Program displays: | Please enter a barcode (100 thru 125) for an item, 0 to quit |
User enters: |
0 |
Program displays: |
Shopping Cart Barcode Item Unit
Price Quantity Total Total Price: $97.68
Barcode Item Unit
Price Quantity Total Total Price: $97.68 |
NOTE: The input for this sample run
is in the file in.txt. You can redirect input from the
keyboard at the command line:
java yourprogramname < in.txt