Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

To solve this problem you need to have solution of Part A. And the link to the P

ID: 3625583 • Letter: T

Question

To solve this problem you need to have solution of Part A. And the link to the Part A of this problem is given below:

http://www.cramster.com/answers-apr-11/computer-science/java-array-object-array-objectsa-shopping-cart-array-itemsin_1284838.aspx
---------------------------------------------------------------------------------------------------

A Shopping Cart Using the ArrayList Class

In this exercise you will implement a shopping cart using the ArrayList class. The file Item.java contains the definition of a class named Item that models an item one would purchase (this class was used in an earlier lab). An item has a name, price, and quantity (the quantity purchased). The file Shop.java is an incomplete program that models shopping.

1. Complete Shop.java as follows:
a. Declare and instantiate a variable cart to be an empty ArrayList.
b. Fill in the statements in the loop to add an item to the cart and to print the cart contents (using the default toString in the ArrayList class). Comments in the code indicate where these statements go.
c. Compile your program and run it.

2. You should have observed two problems with using the default printing for the cart object: the output doesn't look very good and the total price of the goods in the cart is not computed or printed. Modify the program to correct these problems by replacing the print statement with a loop that does the following:
a. gets each item from the cart and prints the item
b. computes the total price of the items in the cart (you need to use the getPrice and getQuantity methods of the Item class). The total price should be printed after the loop.

3. Compile and run your program.


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

import java.util.ArrayList;
import java.util.Scanner;

public class Shop
{
public static void main (String[] args)
{
ArrayList cart = new ArrayList();

Item item;
String itemName;
double itemPrice;
int quantity;

Scanner scan = new Scanner(System.in);

String keepShopping = "y";

do
{
System.out.print ("Enter the name of the item: ");
itemName = scan.nextLine();

System.out.print ("Enter the unit price: ");
itemPrice = scan.nextDouble();

System.out.print ("Enter the quantity: ");
quantity = scan.nextInt();

// *** create a new item and add it to the cart



// *** print the contents of the cart object using println


System.out.print ("Continue shopping (y/n)? ");
keepShopping = scan.nextLine();
}
while (keepShopping.equals("y"));

}
}

Explanation / Answer

import java.util.Scanner; public class Shop { public static void main(String[] args) { ShoppingCart cart = new ShoppingCart(); Item item; String itemName; double itemPrice; int quantity; Scanner scan = new Scanner(System.in); String keepShopping = "y"; do { System.out.print("Enter the name of the item: "); itemName = scan.nextLine(); System.out.print("Enter the unit price: "); itemPrice = scan.nextDouble(); System.out.print("Enter the quantity: "); quantity = scan.nextInt(); // *** create a new item and add it to the cart Item i = new Item(itemName, itemPrice, quantity); cart.addToCart(i.getName(), i.getPrice(), i.getQuantity()); System.out.println(cart); // *** print the contents of the cart object using println System.out.print("Continue shopping (y/n)? "); scan=new Scanner(System.in); keepShopping = scan.nextLine(); } while (keepShopping.equalsIgnoreCase("y")); } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote