I am working on an assignment and I have run into a few problems. Here is the Assignment Question and what I’ve done so far and my question.
- 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 as7.19
, quantity as2
, and manufacturer asKlout
. - Second product as
Forerunner Watch
with unit price as140.89
, quantity as2
, and manufacturer asGarmin
.
- First product as
Here is the code I’ve written:
//***************************************************************
//ShoppingCart.java
//
//Uses the Product class to create items and add them to a shopping
//cart stored in an ArrayList.
//***************************************************************
// import statements
import java.util.ArrayList;
import java.text.NumberFormat;
import java.util.Scanner;
//Class header
public class ShoppingCart {
//Start of main method
public static <Item> void main(String[] args){
//Declare and instantiate a variable that is an ArrayList that can hold Product objects
ArrayList<Product> item = new ArrayList<Product>();
//Declare necessary local variables here
String Name = null;
double Price = 0;
int Quantity = 0;
String Seller = null;
Scanner scan = new Scanner(System.in);
// 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
*/
String keepShopping = "Yes";
Product item1 = new Product(Name, Price, Quantity, Seller);
//do while loop start
do
{
//Ask user to enter product name and store it in appropriate local variable
System.out.print("Please Enter the Product Name: ");
Name = scan.next();
//Ask user to enter product price and store it in appropriate local variable
System.out.print("Please Enter the Price of the Product: ");
Price = scan.nextDouble();
//Ask user to enter quantity and store it in appropriate local variable
System.out.print("Please enter the Quantity: ");
Quantity = scan.nextInt();
//Ask user to enter product manufacturer name and store it in appropriate local variable
System.out.print("Please Enter the Manufacturer: ");
Seller = scan.next();
// create a new Product object using above inputed values
Product newitem = new Product(Name, Price, Quantity, Seller);
//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
} while (keepShopping.equals("Yes"));
// 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
And the Product class the professor provided:
//***************************************************************
//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;
}
public Product(String itemName, double itemPrice, int quantity2) {
// TODO Auto-generated constructor stub
}
// -------------------------------------------------------
// Return a string with the information about the Product
// -------------------------------------------------------
public String toString ()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
return (manufacturer + " " + name + "\t\t " + fmt.format(price) + "\t " + quantity
+ "\t\t" + fmt.format(subtotal));
}
// Returns the unit price of the Product
public double getPrice()
{