In this problem you will implement a shopping cart using the ArrayList class. The file Product.java contains the definition of a class named Product that models an item one would purchase. Product has a name, price, quantity, and manufacturer name. The file ShoppingCart.java is an incomplete program that models shopping.

Complete ShoppingCart.java as follows:

  • Declare and instantiate a variable cart to be an empty ArrayList that can hold Product objects. Remember to import ArrayList. Comments in the code indicate where these statements should go.
  • Fill in the required statements to write a loop that requests required information from the user regards to a product item, create a Product object using inputted information, and add the product to the ArrayList. Ensure user can enter information for multiple products by enabling loop to continue work based on whether user wants to continue shopping. Comments in the code indicate where these statements go.
  • Print the shopping cart contents by printing each product items stored in the ArrayList. Remember that Product object has toString method. Print the total price of the cart after printing shopping cart contents. Remember to format the output of total price, so that it prints in current format. Comments in the code indicate where these statements go.

Compile and run your program with following inputs (Provide output of your program)

  • First product as “Bicycle Horn” with unit price as “7.19”, quantity as “2”, and manufacturer as “Klout”.
  • Second product as “Forerunner Watch” with unit price as “140.89”, quantity as “2”, and manufacturer as “Garmin”.

Sample screen shots for obtaining product information inputs from user See image.

Screen shot for asking user whether they want to continue shopping See image.

Some helpful hints

See below Java API documentation and examples for JOptionPane

http://docs.oracle.com/javase/7/docs/api/javax/swing/JOptionPane.html

http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

Following two methods in JOptionPane would be useful for this program: showInputDialog(Component parentComponent, Object message, String title, int messageType) showConfirmDialog(Component parentComponent, Object message, String title, int optionType)

Hint: for parentComponent use value null. Null is reserved word that represents no value or an empty object.

ArrayList hints: See below Java API documentation for ArrayList http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

ArrayList[Actor] names = new ArrayList [Actor]();

Above statement creates an ArrayList that can hold String objects

Actor hollywoodActor = names.get(1);

Above statement gets the Actor located at index location 1 from the names ArrayList

for (int i=0; i {
System.out.println(names.get(i);
}

Above for loop statement prints out all Actor objects stored in the names ArrayList. It will print following the format described in the toString method of the Actor object.

Sample output produced by the program

Welcome to COP 2551 Amazing JavaShop!!!

Product Unit Price Quantity SubTotal
Klout Bicycle Horn $7.19 2 $14.38
Garmin Forerunner Watch $140.89 2 $281.78
Total Price: $296.16

Incomplete ShoppingCart Program

//***************************************************************
//ShoppingCart.java
//
//Uses the Product class to create items and add them to a shopping
//cart stored in an ArrayList.
//***************************************************************

// import statements

//Class header
//Start of main method

//Declare and instantiate a variable that is an ArrayList that can hold Product objects

//Declare necessary local variables here

// Write a print statement that welcome's the customer to our shop

/**
* create a do while that will be keep looping as long as user wants to continue shopping
*/
//do while loop start

//Ask user to enter product name and store it in appropriate local variable

//Ask user to enter product price and store it in appropriate local variable

//Ask user to enter quantity and store it in appropriate local variable

//Ask user to enter product manufacturer name and store it in appropriate local variable

// create a new Product object using above inputed values

//add above created Product to the ArrayList cart if Product has available stock
// if stock not available inform user requested product is out of stock

//Ask user whether wants to continue shopping
//set the do while loop to continue to loop if Yes option is selected

// do while loop end

// header for shopping cart contents

// print details of each product added to the ArrayList
// calculate total price of the shopping cart

// print the total price of the shopping cart

//end of main method
//end of Shop class

Complete Product Program

//***************************************************************
//Product.java
//Represents an item in a shopping cart.
//***************************************************************
import java.text.NumberFormat;

public class Product
{
private String name;
private double price;
private int quantity;
private double subtotal;
private String manufacturer;
private int inventory;

// -------------------------------------------------------
// Create a new item with the given attributes.
// -------------------------------------------------------
public Product (String name, double price, int quantity, String manufacturer)
{
this.name = name;
this.price = price;
this.quantity = quantity;
this.manufacturer = manufacturer;

subtotal = price*quantity;

inventory = 10;
}

// -------------------------------------------------------
// Return a string with the information about the Product
// -------------------------------------------------------
public String toString ()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
/*
String item;
if (name.length() >= 8)
item = name + "t";
else
item = name + "tt";
*/
return (manufacturer + " " + name + "tt " + fmt.format(price) + "t " + quantity
+ "tt" + fmt.format(subtotal));
}

// Returns the unit price of the Product
public double getPrice()
{
return price;
}

// Returns the name of the Product
public String getName()
{
return name;
}

// Returns the quantity of the Product
public int getQuantity()
{
return quantity;
}

// Returns the sub total of the Product
public double getSubTotal()
{
return subtotal;
}

// Returns product manufacturer
public String getManufacturer()
{
return manufacturer;
}

// Checks whether product is in stock
// if after inventory get below stock after processing order, then
// places order to replenish stock
public boolean checkInventory()
{
boolean flag = false;
if (inventory > quantity)
{
flag = true;
inventory = inventory - quantity;

if (inventory <= 0)
placeOrder(quantity);
}

return flag;
}

// Replenishes stock to 10 times the quantity of last order
private void placeOrder(int orderQuantity)
{
inventory = orderQuantity * 10;
}
}//end of class Item
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.