Java programming exercise.

You are to write a set of supporting classes for a simple shopping program. Prices are expressed using doubles and quantities are expressed as simple integers (e.g., you can’t buy 2.345 of something). Notice that some of the items have a discount when you buy more. For example, product X 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 bought 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.You are to implement four classes that are used to make this code work. You should first implement a Class called Item that will store information about the individual items. It should have the following public methods.*-Description of Method Item(name, price):-Constructor that takes a name and a price as arguments. The name will be aString and the price will be a double. Should throw anIllegalArgumentException if price is negative.*-Description of Method Item(name, price, bulk quantity,bulk price):- Constructor that takes a name and a single-item price and a bulk quantity and abulk price as arguments. The name will be a String and the quantity will be aninteger and the prices will be doubles. Should throw anIllegalArgumentException if any number is negative.*-Description of Method 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 anIllegalArgumentException if quantity is negative.*-Description of Method 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 anextra space and a parenthesized description of the bulk pricing that has the bulkquantity, the word “for” and the bulk price.You should implement a Class called Catalog that stores information about a collection of these items. It should have the following public methods.*-Description of Method Catalog(name):-Constructor that takes the name of this catalog as a parameter. The name will bea String.*-Description of Method addItem()-Adds an Item at the end of this list.*-Description of Method size()-Returns the number of items in this list.*-Description of Method getIndex()-Returns the Item with the given index (0-based).*-Description of Method getName()-Returns the name of this catalog. *-Description of Method toString()-Returns list of items in the catalog (see output)You should implement a class called ItemOrder that stores information about a particular itemand the quantity ordered for that item. It should have the following public methods:*-Description of Method ItemOrder(item,quantity):-Constructor that creates an item order for the given item and given quantity.The quantity will be an integer.*-Description of Method 12 getPrice():-Returns the cost for this item order.*-Description of Method 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.*-Description of Method ShoppingCart():-Constructor that creates an empty list of item orders.*-description of Method add(item order):-Adds an item order to the list, replacing any previous order for this item with thenew order. The parameter will be of type ItemOrder.*-Description of Method getTotal():-Returns the total cost of the shopping cart. Notice that when you add an ItemOrder to a ShoppingCart, you have to deal with replacing any old order for the item. A user at one time might request 3 of some item and later change the request to 5 of that item. The order for 5 replaces the order for 3. The user isn’t requesting 8 of the item in making such a change. The add method might be passed an item order with a quantity of 0. This should behave just like the others, replacing any current order for this item or being added to the order list. In the Item class you need to construct a String representation of the price. This isn’t easy to do for a number of reasons, but Java provides a convenient built-in object that will do it for you. It’s called a NumberFormat object and it appears in the java.text package (so you need to import java.text.*). You obtain a formatter by calling the static method called getCurrencyInstance(), asin: NumberFormat nf = NumberFormat.getCurrencyInstance(); You can then call the “format” method of this object passing it the price as a double and it willreturn a String with a dollar sign and the price in dollars and cents. For example, you might say:double price = 38.5;String text = nf.format(price);This would set the variable text to “$38.50”. classes should be stored in files called Item.java, Catalog.java, ItemOrder.java andShoppingCart.java. Below is a Driver for your code: public class Driver { public static void main(String[] args) { ShoppingCart shopCart = new ShoppingCart(); Item I1 = new Item("Pepsi", 3.99); Item I2 = new Item("Apple", 1.99, 10, 5.99); Item I3 = new Item("Orange", 1.25); Item I4 = new Item("Cake", 0.99, 7, 4.25); Item I5 = new Item("Galaxy", 1.0); Item I6 = new Item("Twix", 1.0, 4, 4.0); Item I7 = new Item("Trident", 2.0); Item I8 = new Item("Chips", 0.5, 3, 1.0); Catalog catalog = new Catalog("Ayre Market"); catalog.add(I1); catalog.add(I2); catalog.add(I3); catalog.add(I4); catalog.add(I5); catalog.add(I6); catalog.add(I7); catalog.add(I8); System.out.println(catalog.getName()+" offers "+catalog.size()+" items:"); System.out.println(catalog); ItemOrder IO1 = new ItemOrder(I1, 4); ItemOrder IO2 = new ItemOrder(I2, 25); ItemOrder IO3 = new ItemOrder(I3, 5); ItemOrder IO32 = new ItemOrder(I3, 2); ItemOrder IO4 = new ItemOrder(I4, 13); ItemOrder IO5 = new ItemOrder(I5, 10); ItemOrder IO6 = new ItemOrder(I6, 9); ItemOrder IO7 = new ItemOrder(I7, 2); ItemOrder IO72 = new ItemOrder(I7, 0); ItemOrder IO8 = new ItemOrder(I8, 6); shopCart.add(IO1); shopCart.add(IO2); shopCart.add(IO3); shopCart.add(IO4); shopCart.add(IO5); shopCart.add(IO6); shopCart.add(IO7); shopCart.add(IO8); System.out.println("Your cart total cost is: "+ shopCart.getTotal()); shopCart.add(IO32); shopCart.add(IO72); System.out.println("Your cart total cost is: "+ shopCart.getTotal()); }}Output:Ayre Market offers 8 items:Item 1: Pepsi, $3.99Item 2: Apple, $1.99 (10 for $5.99)Item 3: Orange, $1.25Item 4: Cake, $0.99 (7 for $4.25)Item 5: Galaxy, $1.00Item 6: Twix, $1.00 (4 for $4.00)Item 7: Trident, $2.00Item 8: Chips, $0.50 (3 for $1.00)Your cart total cost is: 79.33Your cart total cost is: 71.58 PLEASE NOTE: THIS IS NOT AN ASSIGNMENT.

Add a comment