I have to write a shopping program for my computer science class.

User Generated

Nyyvrqfvatu

Computer Science

Description

I have to write a shopping program for my computer science class. I can get help from other people.

Unformatted Attachment Preview

Programming Assignment #5 This assignment will allow you to review defining Java classes and the use of arrays. You are to write a set of supporting classes for a shopping program. 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, if the user is ordering 12 buttons that cost $0.99 each but can they are also sold in bulk at 10 for $5.00. Charge the customer at that bulk price ($5.00) for the first 10 and the two extras at the single item price ($0.99 each) for a total of $6.98. You must implement 4 classes in addition to your driver class for this project. You should implement a class called Item that will store information about the individual items. It should have the following public methods. Item Instance variables Method Item(name, price) Item(name, price, bulk quantity, bulk price) priceFor(quantity) toString() getBulkPricePerItem() getHasBulk() getBulkQuantity() getPrice() -String name -double price -boolean hasBulk -int bulkQuantity -double bulkPrice Description Constructor that takes a name and a price as arguments. The name will be a String and the price will be a double. 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. Returns the price for a given quantity of the item (take into account bulk price, if applicable). Quantity will be an integer. 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. Return the bulk price for only 1 item Retruns the instance variable hasBulk Returns the instance variable bulkQuantity Returns the instance variable price You should implement a class called Catalog that stores information about a collection of these items. It should have the following public methods. You may assume that a catalog will never have more than 100 items. Catalog Instance variables -String name Page 1 Method Catalog(name, count) add(item) size() get(index) getName() getList() -Item[] items +public static int index = 0 Description Constructor that takes the name of this catalog and the number of the items in the catalog as its parameters. The count will be used to instantiate the array of items Adds an Item to the array of items at the index, then increments the index by 1. Returns the the size of the array items Returns the Item with the given index (0-based) in the array of items Returns the name of this catalog. Returns the array items 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. ItemOrder Instance variables Method ItemOrder(item, quantity) getPrice() getItem() getQuantity() toString() -Item item -int quantity Description Constructor that creates an item order for the given item and given quantity. The quantity will be an integer. Use the following code to check if the quantity is less than zero if (quantity < 0) throw new IllegalArgumentException(); if quantity is not less than zero then set the instance variable quantity to the given value Returns the cost for this item order. Returns a reference to the item in this order. Returns the quantity for the item Returns a string containing the item description and the quantity. You must call the toString method from the Item class You should implement a class called ShoppingCart that stores information about the overall order. It should have the following public methods. You may assume that you will never have more than 100 items in the shopping cart. ShoppingCart Instance variables Method ShoppingCart() -ItemOrder[] list -boolean discount -int count Description Constructor that creates an empty list of 20 item orders. Sets discount variable to false Sets count to zero Page 2 add(item order) setDiscount(value) getTotal() getCount() getDiscountedTotal() getList() Adds an item order to the list, replacing any previous order for this item with the new order. If the item is not already in the list, add the item at the index count, increment count by 1. The parameter will be of type ItemOrder. Sets whether or not this order gets a discount (true means there is a discount, false means no discount). Returns the total cost of the shopping cart. A for loop is needed to get the price of each item in the list and calculate the total. The method getPrice from the item class must be called. Returns the instance variable count This method returns the total after the DISCOUNT_PRICE percentage. In this method you need to call the getTotal. If the discount instance variable is true, then calculate the discounted total and return it. If no discount is applicable just return the total without the discount Returns the instance variable list You are not to introduce any other public methods to these classes, although you can add your own private methods. You are allowed to redefine toString in any of these classes (you might find that helpful in testing and debugging your code). You should use an array to implement the ShoppingCart and Catalog classes. In both cases you can assume that you will never have more than 100 items. You will probably want to write your own testing code so that you can develop these classes in stages rather than all at once. You will be graded on program style including the use of good variable names, comments on each class and each method, using local variables when possible, correct use of generics and the other standard style guidelines. Your classes should be stored in files called Item.java, Catalog.java, ItemOrder.java and ShoppingCart.java. You also need to create a driver class called ShoppingMain which will have your main method and use the classes above. Below is the algorithm for your ShoppingMain including the methods that you will need • Method: run(): void o Create your new Scanner object to read input from the user o Declare your Catalog object and initialize it by calling makeCatalog(). Since makeCatalog() returns an object of type Catalog all you need to do in the run method is assign the return from makeCatalog() to your Catalog object. o Loop while there are more customers ▪ Call your shop() method and assign the result to a ShoppingCart object ▪ Call your displayShoppingCart() method to show the user what is in their shopping cart ▪ Call your displayTotal() method to show them the total charges ▪ Ask them if there is going to be another customer, use your getYesOrNo() method • Method: getYesOrNo(Scanner kb, String prompt): returns String Page 3 • o Print the prompt o Loop until the user enters “Yes” or “No” and return the result Method: makeCatalog(): returns Catalog o For this method you can create whatever items you would like to have in your catalog. If you want you can use the same items that are in the sample output but you do not have to. In this method you will create a new Catalog object and then call the add() method several times, one for each item that you would like to add. Here is an example: Catalog list = new Catalog("CS Gift Catalog",20); list.add(new Item("silly putty", 3.95, 10, 19.99)); • • • • o After all of your items have been added to your catalog object then return it Method: shop(Scanner kb, Catalog list): returns a ShoppingCart object o Create your new ShoppingCart object o Print out a welcome message and include the name of the catalog o For each item in the catalog ▪ Prompt the user to enter the quantity that they would like to order ▪ Use the getValidInt() method to ensure that an int is entered ▪ Create a new ItemOrder object for the item that they are ordering and add it to the shopping cart. o Return the ShoppingCart object Method: getValidInt(Scanner kb): void o Use examples from class to ensure that the user enters a positive number. Note you will want to have two loops, one on the outside to loop while the numbered entered is negative and one on the inside to loop until they enter an integer. If you use this structure then you should initialize the input to a value that guarantees the first loop will execute. Method: displayShoppingCart(ShoppingCart cart): void o Print a header o For each item in the cart ▪ If the quantity is greater than 0 ▪ Print the item details. If you implemented the toString() method correctly then a simple call to System.out.println() with the item object as the parameter should print everything that you need. Method: displayTotal(ShoppingCart cart): void o Print a total line and the grand total for the shopping cart. Page 4
Purchase answer to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

Please unzip, compile and run th...


Anonymous
Super useful! Studypool never disappoints.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4

Related Tags