Question
Java shopping cart with arraylist Homework 14 Shopping Cart Objectives: Practice ArrayList. You are going to write a set of supporting classes for a simple
Java shopping cart with arraylist
Homework 14 Shopping Cart
Objectives:
Practice ArrayList.
You are going to write a set of supporting classes for a simple shopping program.
A shopping cart conceptually is a list of items ordered, each of which contains an item name and a quantity. It is assumed that 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, milk normally costs $2.99 each, but you can buy 2 for $4.59. 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. You should implement a class called Item that will store information about the individual items. It should have the following public constructors and methods:
Item(String name, double price) o Constructor that takes a name and a price as arguments. Should throw an IllegalArgumentException if price is negative.
Item(String name, double price, int bulkQuantity, double bulkPrice) o Constructor that takes a name, a single-item price, a bulk quantity, and a bulk price as arguments. Should throw an IllegalArgumentException if any number is negative.
String getName() o Returns the name of the item.
double priceFor(int quantity) o Returns the price for a given quantity of the item (taking into account bulk price, if applicable). Should throw an IllegalArgumentException if quantity is negative.
String toString() o 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.
You should implement a class called Catalog that stores information about a collection of all items offered by the store. It should have the following public constructor and methods:
Catalog(String name) o Constructor that takes the name of this catalog as a parameter. The name usually is the store name; for example, North Mart.
void add(Item next) o Adds an Item at the end of this catalog.
int size() o Returns the number of items in this catalog.
Item get(int index) o Returns the Item with the given index (0-based).
Item get(String itemName) o Returns the Item with the specified item ame.
String getName() o Returns the name of this catalog.
String toString() o Returns a String representation 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 constructor and methods:
ItemOrder(Item item, int quantity) o Constructor that creates an item order for the given item and given quantity.
Item getItem() o Returns the Item for this order.
int getQuantity() o Returns the quantity for this order.
double getPrice() o Returns the cost for this item order.
You should implement a class called ShoppingCart that stores information about the overall order. It should have the following public constructor and methods:
ShoppingCart() o Constructor that creates an empty shopping cart.
void add(ItemOrder next) o Adds an item order to the shopping cart.
double getTotal() o Returns the total cost for all orders in this shopping cart.
String toString() o Returns a String representation of this shopping cart.
You are not to introduce any other public methods to these classes, although you can add your own private methods. You should use an ArrayList to implement the ShoppingCart and Catalog classes, but they should not extend ArrayList.
The sources to populate Catalog and ShoppingCart objects are from two input files. The file for catalog contains lines of item pricings, exactly one for each item offered by the store. Each line has either two or four data, depending on whether the specific items has bulk price. If an item does not
have bulk price, its data consists of item name and unit price. For an item having bulk price, in addition to item name and regular unit price, there are quantity that qualifies for bulk discount and the total charge for such a bulk quantity. Data are separated by a comma character and there is no space before or after a comma separator. You can assume all item names are unique and there is not any comma character in an item name. Also, bulk quantities are always integers. A sample catalog file is illustrated below:
putty,2.95,10,25.59
waste basket,3.5,3,10.00
whole milk,2.99,2,4.59
Rubik's cube,9.10
plastic outlet,2.23
sockeye,38.75
bumper sticker,0.99,20,8.95
The file for shopping cart contains lines of items ordered, exactly one for each item purchased by a specific customer. Each line has two data, that is, item name and quantity. Same as catalog, data are also separated by a comma character. You can assume any item name in shopping cart file must correspond to an item in the catalog file; in other words, item names are unique identifiers for entries in both files. A sample shopping cart file is illustrated below:
putty,12
waste basket,2
whole milk,3
Rubik's cube,2
plastic outlet,3
bumper sticker,10
After class design, continue to write a Java program to simulate a transaction. The program prompts users for two file names on two lines, the first for catalog file and the other for shopping cart file. The catalog file is supposed to have been well prepared by database team, and the shopping cart file simulates barcode scanning at checkout.
Your program should print a receipt for the simulated transaction. The receipt should let customers understand how each item ordered is charged. At the bottom the receipt there should be the total cost for all purchases in the shopping cart. For the above sample catalog and shopping cart, the order total should be $80.86.
User interface specifications:
Input o The program prompts users for two files names on two separate lines. The first input is for the name of catalog file.
The second input is for the name of shopping cart file.
o Your program is not responsible for bad data in files.
Output o A receipt for the transaction, displayed on screen.
It contains transaction details and total charge before tax.
Code specifications:
The header comment lines at the top of your java files contain a brief description of the program or the class. The description should be one or 2 lines long describing the purpose of the program or the class.
Declare all class fields as private.
You also have to include comment lines in all of your public methods, static or not.
Use Javadocs comment formats for program and public methods documentation.
Use descriptive variable names and function names.
Define constants whenever appropriate.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started