Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are to write a set of supporting classes for a simple shopping program. We are providing GUI code ( Graphical User Interface ) that

You are to write a set of supporting classes for a simple shopping program. We are providing GUI code (Graphical User Interface) that will provide the front end to the program. You are to write the back end (what is often referred to as the domain specific code).
Below is a screen shot of what the program could look like when the user has selected various items to order.
ShoppingCart.png
Prices are expressed using doubles and quantities are expressed as simple integers (e.g., you cant buy 2.345 of something). Notice that some of the items have a discount when you buy more. For example, silly putty normally costs $3.95 each, but you can buy 10 for $19.99. These items have, in effect, two prices: a single item price and a bulk item price for a bulk quantity. When computing the price for such an item, apply as many of the bulk quantity as you can and then use the single item price for any leftovers. For example, the user is ordering 12 buttons that cost $0.99 each but can be bough t in bulk 10 for $5.00. The first 10 are sold at that bulk price ($5.00) and the two extras are charged at the single item price ($0.99 each) for a total of $6.98.
At the bottom of the frame you will find a checkbox for an overall discount. If this box is checked, the user is given a 10% discount off the total price. This is computed using simple double arithmetic, computing a price that is 90% of what it would be otherwise. For example, if we turn on that checkbox, the frame looks like this:
ShoppingCart_With_Discount.png
You are to implement four classes that are used to make this code work. You should implement a class called Item that will store information about the individual items. It should have the following public methods.
Class: Item
Method
Description
Item(name, price)
Constructor that takes a name and a price as arguments. The name will be a String and the price will be a double. Should throw an IllegalArgumentException if price is negative.
Item(name, price, bulk quantity, bulk price)
Constructor that takes a name and a single-item price and a bulk quantity and a bulk price as arguments. The name will be a String and the quantity will be an integer and the prices will be doubles. Should throw an IllegalArgumentException if any number is negative.
priceFor(quantity)
Returns the price for a given quantity of the item (taking into account bulk price, if applicable). Quantity will be an integer. Should throw an IllegalArgumentException if quantity is negative.
toString()
Returns a String representation of this item: name followed by a comma and space followed by price. If this has a bulk price, then 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.
equals()
You should make sure that you can compare items based on the contents of the item (rather than by asking if two variables refer to the same Item object). You can do this by overriding the .equals() method.
This is covered in section 9.2 of the textbook (starting with the section named "The equals Method" and finishing with the section named "The instanceOf keyword"). There's also a bunch of examples of overriding equals in Java online, such as this one (you might want to skip to the section entitled "Implementing Equals").
Make sure that you implement .equals, and do so correctly (as described in the textbook)
Two items are the same if they have the same name and the same price.
You should implement a class called Catalog that stores information about a collection of these items. It should have the following public methods.
Class: Catalog
Method
Description
Catalog(name)
Constructor that takes the name of this catalog as a parameter. The name will be a String.
add(item)
Adds an Item at the end of this list.
size()
Returns the number of items in this list.
get(index)
Returns the Item with the given index (0-based).
getName()
Returns the name of this catalog.
You should implement a class called ItemOrder that stores information about a particular item and the quantity ordered for that item. It should have the following public methods.
Class: ItemOrder
Method
Description
ItemOrder(item, quantity)
Constructor that creates an item order for the given item and given quantity. The quantity will be an integer.
getPrice()
Returns the cost for this item order.
getItem()
Returns a reference to the item in this order.
You should implement a class called ShoppingCart that stores information about the overall order. It should have the following public methods.
Class: ShoppingCart
Method
Description
ShoppingCart()
Constructor that creates an empty list of item orders.
add(item order)
Adds an item order to the list, replacing any previous order for this item with the new order. The parameter will be of type ItemOrder.
setDiscount(value)
Sets whether or not this order gets a discount (true means there is a discount, fals

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions