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

Write code in Java programming language. The ShoppingCart class will be composed

ID: 3717220 • Letter: W

Question

Write code in Java programming language.

The ShoppingCart class will be composed with an array of Item objects. You do not need to

implement the Item class for this question, only use it. Item has a getPrice() method that returns a

float and a one-argument constructor that takes a float that specifies the price. The ShoppingCart

class should have:

Constructors: A no-argument and a single argument that takes an array of Items.

Fields:

• Items: an array of Item objects (maximum 10 objects).

• numItems: An int that tracks the number of items in the users cart.

• taxRate: A double representing the tax rate to be applied, defaulted to .05

Methods:

• Setters and getters for numItems and taxRate

• addItem(Item i): Adds an item to the cart. Returns a boolean. If the cart is full, return

false and do not add the item to the cart. Otherwise, add the item to the array and return

true.

• subtotal(): Returns a float representing the combined cost of all the items in the cart not

including tax.

• total(): Returns a float representing the subtotal plus tax. The tax amount is subtotal

times the taxRate.

Write a class called ‘TestShoppingCart’ with a main method that does the following:

1. Create a ShoppingCart object.

2. Adds 10 Items to it, each item should have a randomly generated price between $0 and

$100.

3. Print the subtotal, tax and total dollar amount that the customer would have to pay

Explanation / Answer

// ShoppingCart.java

import java.util.*;

public class ShoppingCart

{

//declaring variables

List<ShoppingCartItem> items;

int numberOfItems;

double total;

//default constructor

public ShoppingCart(){

items = new ArrayList<ShoppingCartItem>();

numberOfItems = 0;

total = 0;

   }

  public  void addItem(Product product) {

        boolean newItem = true;

for (ShoppingCartItem scItem : items)

{

if (scItem.getProduct().getId() == product.getId())

{

              newItem = false;

              scItem.incrementQuantity();

}

}

if (newItem) {

ShoppingCartItem scItem = new ShoppingCartItem(product);

items.add(scItem);

}

}

public void update(Product product, String quantity) {

int qty = -1;

qty = Integer.parseInt(quantity);

        if (qty >= 0)

  {            

ShoppingCartItem item = null;

for (ShoppingCartItem scItem : items) {

if (scItem.getProduct().getId() == product.getId()) {

if (qty != 0) {

// set item quantity to new value

scItem.setQuantity(qty);

}

else

{

item = scItem;

break;

}

}

}

if (item != null) {

// remove from cart

items.remove(item);

}

}

}

public s List<ShoppingCartItem> getItems() {

return items;

}

public synchronized int getNumberOfItems()

{

numberOfItems = 0;

for (ShoppingCartItem scItem : items)

{

numberOfItems += scItem.getQuantity();

}

return numberOfItems;

}

public  double getSubtotal() {

        double amount = 0;

for (ShoppingCartItem scItem : items)

{

Product product = (Product) scItem.getProduct();

amount += (scItem.getQuantity() * product.getPrice().doubleValue());

}

return amount;

}

public  double getTotal() {

         double amount = 0;

                   double tax=this.getSubtotal()*0.05;

                   amount += tax;

                    total = amount;

                    return total;

}

public  void clear() {

items.clear();

numberOfItems = 0;

total = 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