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:
Compile and run your program with following inputs (Provide output of your program)
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.
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
//***************************************************************
//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
//***************************************************************
//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