Implement a class ShoppingCart that models the shopping cart of an e-commerce si
ID: 3686622 • Letter: I
Question
Implement a class ShoppingCart that models the shopping cart of an e-commerce site.
This shopping cart will be composed with an array of Item objects. You do not need to implement Item for this exam, only use it. Moreover, it is an abstraction and you do not need to know how it is implemented. Item has a getPrice() method that returns a float and a one-argument constructor that takes float thats specifies the price.
ShoppingCart has the following constructors:
A no-arg constructor.
A single argument constructor that takes an array of Items.
Note: You should make use of the ‘this’ keyword (both of its applications).
ShoppingCart has the following variables:
1.items: An array of Item objects representing the cart's contents.The cart can hold a maximum of 10 Item objects (enforce this wherever necessary).
2.numItems: An int that tracks the number of items in the users cart.
3.taxRate: A double representing the tax rate to be applied, defaulted to .05 The value is the same for all objects of ShoppingCart (hint, hint!).The value could change and it should not be allowed to be negative.
Note: The numItems and taxRate variables should have accessor and mutator methods in addition to the methods listed below. ShoppingCart has the following methods:
1. 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.
2. subtotal(): Returns a float representing the combined cost of all the items in the cart not including tax.
3. total(): Returns a float representing the combined cost of all the items in the cart not including tax. 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
2. Adds 10 Items to it, each item should have a randomly generated price between $0 and $100.
3. Print the dollar amount of tax that the customer would have to pay.
Explanation / Answer
package cart;
import entity.Product;
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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.