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

JavaScript Shopping Cart In this exercise, you will implement a shopping card pr

ID: 3848784 • Letter: J

Question

JavaScript Shopping Cart

In this exercise, you will implement a shopping card program using an array.

1. Write a class named Item as follows. a. The class has attributes of a name, price, and quantity (the quantity purchased) which are initialized in the constructor by the corresponding parameters of the constructor. b. The class has toString printing an item as a string of price, quantity, and sub-total price (price*quantity) and getters for name, price, and quanitity.

2. Write a class named ShoppingCart as follows. a. The class has attributes of itemCount, totalPrice, and capacity which are initialized to 0, 0, and 5 respectively in the constructor. b. The class has a cart implemented as an array of items. cart is instantiated in the constructor to be an array holding the capacity number of items. c. The class has a method named increaseSize increasing the size of the array by 3 elements. Hint: The code is similar to that in Listing 8.7 in the text. d. The class has a method named addToCart adding the item to the cart and updating the totalPrice (note this takes into account the quantity). e. The class has toString printing a cart as a string of all the items in the cart with the grand total price.

3. Write the driver class Shopping that simulates shopping. The class should have a loop that continues as long as the user wants to shop up to two times of size increase of capacity. Each time through the loop read in the name, price, and quantity of the item the user wants to add to the cart. After adding an item to the cart, the cart contents should be printed. After the loop print a "Please pay ..." message with the total price of the items in the cart.

Explanation / Answer

ShoppingCart.java


import java.text.NumberFormat;

public class ShoppingCart
{
private int itemCount; // total number of items in the cart
private double totalPrice; // total price of items in the cart
private int capacity; // current cart capacity

  
private Item cart[];
// -----------------------------------------------------------
// Creates an empty shopping cart with a capacity of 5 items.
// -----------------------------------------------------------
public ShoppingCart()
{
  
capacity = 5;
itemCount = 0;
totalPrice = 0.0;
cart = new Item[capacity];
}
int totalItems = 0;

// -------------------------------------------------------
// Adds an item to the shopping cart.
// -------------------------------------------------------
public void addToCart(String itemName, double price, int quantity)
{
   totalItems++;
   System.out.println("Total items "+totalItems);
   if(cart.length < totalItems ){
       increaseSize();
   }
   Item item = new Item(itemName, price, quantity);
   cart[totalItems - 1] = item;
   System.out.println("Item has been added");
   System.out.println("----------------------Item Details--------------------------");
   for(int i=0; i<cart.length; i++){
       if(cart[i] != null){
       System.out.println("Item Name : "+cart[i].getName()+" Item Price : "+cart[i].getPrice()+" Item Quantity :"+cart[i].getQuantity());  
       }
   }
   System.out.println("--------------------------------------------------------------");
  
}

// -------------------------------------------------------
// Returns the contents of the cart together with
// summary information.
// -------------------------------------------------------
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();

String contents = " Shopping Cart ";
contents += " Item Unit Price Quantity Total ";

for (int i = 0; i < itemCount; i++)
contents += cart[i].toString() + " ";

contents += " Total Price: " + fmt.format(totalPrice);
contents += " ";

return contents;
}

// ---------------------------------------------------------
// Increases the capacity of the shopping cart by 3
// ---------------------------------------------------------
private void increaseSize()
{
int arrayGrowth = 3;
Item temp[] = new Item[cart.length + arrayGrowth];
System.arraycopy(cart, 0, temp, 0, cart.length);
cart = temp;
}
}

Shopping.java


public class Shopping {
   public static void main(String args[]){
       java.util.Scanner in = new java.util.Scanner(System.in);
       String itemName = null;
       double price = 0;
       int quantity = 0;
       ShoppingCart cart = new ShoppingCart();
       double totalPrice = 0;
       while(true){
           System.out.println("Enter the item name");
           itemName = in.next();
           System.out.println("Enter Item Price ");
           price = in.nextDouble();
           System.out.println("Enter item Quantity");
           quantity = in.nextInt();
           cart.addToCart(itemName, price, quantity);
           totalPrice = totalPrice + (price * quantity);
           System.out.println("Do you want to add more? Press any character for continue or press q for Quit");
           char c = in.next().charAt(0);
           if(c == 'q' || c=='Q'){
               break;
           }
       }
       System.out.println("Please pay total price of the items "+totalPrice);
   }

}

Item.java


import java.text.NumberFormat;

public class Item
{
private String name;
private double price;
private int quantity;

// -------------------------------------------------------
// Create a new item with the given attributes.
// -------------------------------------------------------
public Item (String itemName, double itemPrice, int numPurchased)
{
name = itemName;
price = itemPrice;
quantity = numPurchased;
}

// -------------------------------------------------------
// Return a string with the information about the item
// -------------------------------------------------------
public String toString ()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();

return (name + " " + fmt.format(price) + " " + quantity + " "
+ fmt.format(price*quantity));
}

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

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

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

Output:

Enter the item name
aaaa
Enter Item Price
12
Enter item Quantity
2
Total items 1
Item has been added
----------------------Item Details--------------------------
Item Name : aaaa Item Price : 12.0 Item Quantity :2
--------------------------------------------------------------
Do you want to add more? Press any character for continue or press q for Quit
a
Enter the item name
bbb
Enter Item Price
45
Enter item Quantity
3
Total items 2
Item has been added
----------------------Item Details--------------------------
Item Name : aaaa Item Price : 12.0 Item Quantity :2
Item Name : bbb Item Price : 45.0 Item Quantity :3
--------------------------------------------------------------
Do you want to add more? Press any character for continue or press q for Quit
q
Please pay total price of the items 159.0

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