title: ShoppingCart.Java Program Assignment
body: <p>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.</p>
  
  <ol>
  <li><p>Complete ShoppingCart.java as follows: </p>
  
  <ul>
  <li><p>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.</p></li>
  <li><p>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. </p></li>
  <li><p>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.</p></li>
  </ul></li>
  <li><p>Compile and run your program with following inputs (Provide output of your program)</p>
  
  <ul>
  <li>First product as <code>Bicycle Horn</code> with unit price as <code>7.19</code>, quantity as <code>2</code>, and manufacturer as <code>Klout</code>.</li>
  <li>Second product as <code>Forerunner Watch</code> with unit price as <code>140.89</code>, quantity as <code>2</code>, and manufacturer as <code>Garmin</code>.</li>
  </ul></li>
  </ol>
  
  <p>Here is the code I've written:</p>
  
  <pre><code>  //***************************************************************
  //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 &lt;Item&gt; void main(String[] args){
  
          //Declare and instantiate a variable that is an ArrayList that can hold Product objects
          ArrayList&lt;Product&gt; item = new ArrayList&lt;Product&gt;();
          //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
  </code></pre>
  
  <p>And the Product class the professor provided:</p>
  
  <pre><code>//***************************************************************
  //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()
      {
          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 &gt; quantity)
          {
              flag = true;
              inventory = inventory - quantity;
  
              if (inventory &lt;= 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  
  </code></pre>