In: Computer Science
Abstract Cart class
import java.util.ArrayList;
import java.util.HashMap;
/**
* The Abstract Cart class represents a user's cart. Items of Type T
can be added
* or removed from the cart. A hashmap is used to keep track of the
number of items
* that have been added to the cart example 2 apples or 4
shirts.
* @author Your friendly CS Profs
* @param -Type of items that will be placed in the Cart.
*/
public abstract class AbstractCart {
protected HashMap cart;
public AbstractCart() {
this.cart = new HashMap();
}
/**
* Calculates the total value of items in the
cart.
* @return total (double)
*/
public abstract double calculateTotal();
/**
* Add an item of type T to the Cart (HashMap: The
product is the key
* and the value is the count).
* If product doesn't exist in the cart add it and set
count to one.
* Otherwise increment the value.
* @param item
* @return boolean
*/
public void addItem(T item) {
//fill in
}
/**
* Adds every item in the Arraylist of Type T or any
subclass of T.
* @param items: An array of items
* @return true if items have been currently
added.
*/
public void addItems(ArrayList items) {
}
/**
* Removes an item of type T from the list.
* If the only one item, we remove that item.
* If item count is greater than one decrement the
count.
* If you are able to remove the item then return
true.
* If the item doesn't exist return false.
* @param item
* @return true
* in the list.
*/
public boolean removeItem(T item) {
//fill in
return true;
}
/**
* Removes all of the item of items of Type T or any
subclass of T from the cart.
* @param item
* @return true if items have been successfully
remove.
*/
public void removeItems(ArrayList items) {
//fill in
}
/**
* Check to see if the cart contains an item.
* @param item
* @return true if cart contains the item. Returns
False otherwize
*/
public boolean contains(T item) {
return
cart.containsKey(item);
}
}
AmazonCart
public class AmazonCart extends AbstractCart {
@Override
/**
* Calculate the total of all the items in the cart
include tax.
* @return total
*/
public double calculateTotal() {
double total = 0.0;
for(AmazonProduct item :
this.cart.keySet()) {
int itemCount =
this.cart.get(item);
total +=
(item.getPrice() + item.calcTax())*itemCount;
}
return total;
}
}
AmazonCartTest
import static org.junit.Assert.*;
import java.util.ArrayList;
import org.junit.Test;
public class AmazonCartTest {
@Test
public void testAddingItemToCard() {
Milk m = new Milk(2.5, "Lactose
Free", false);
AmazonCart amazonCart = new
AmazonCart();
amazonCart.addItem(m);
if(amazonCart.contains(m) != true)
{
fail("Error
AmazonCart-Line-13: Items not currently added to cart");
}
}
@Test
public void testCalculateTotal() {
Milk m = new Milk(3, "Lactose
Free", true);
DVD d = new DVD(10, "Black
Panther", true);
Shirt s = new Shirt(100, "Versace",
false);
AmazonCart amazonCart = new
AmazonCart();
amazonCart.addItem(m);
amazonCart.addItem(d);
amazonCart.addItem(s);
assert(amazonCart.calculateTotal()
== 120.33);
}
@Test
public void testAddingDupplicates() {
Milk m = new Milk(3, "Lactose
Free", true);
DVD d = new DVD(10, "Black
Panther", true);
Shirt s = new Shirt(100, "Versace",
false);
AmazonCart amazonCart = new
AmazonCart();
amazonCart.addItem(m);
amazonCart.addItem(m);
amazonCart.addItem(d);
amazonCart.addItem(d);
amazonCart.addItem(s);
amazonCart.addItem(s);
assert(amazonCart.calculateTotal()
== 240.66);
}
@Test
public void testRemovingItem() {
Milk m = new Milk(3, "Lactose
Free", true);
DVD d = new DVD(10, "Black
Panther", true);
Shirt s = new Shirt(100, "Versace",
false);
AmazonCart amazonCart = new
AmazonCart();
amazonCart.addItem(m);
amazonCart.removeItem(m);
amazonCart.addItem(d);
amazonCart.addItem(s);
amazonCart.addItem(s);
assert(amazonCart.calculateTotal()
== 224.3);
}
@Test
public void testRemovingItem2() {
Milk m = new Milk(3, "Lactose
Free", true);
DVD d = new DVD(10, "Black
Panther", true);
Shirt s = new Shirt(100, "Versace",
false);
AmazonCart amazonCart = new
AmazonCart();
amazonCart.addItem(m);
amazonCart.removeItem(m);
assert(amazonCart.removeItem(d) ==
false);
amazonCart.addItem(d);
amazonCart.addItem(s);
amazonCart.addItem(s);
assert(amazonCart.calculateTotal()
== 224.3);
}
@Test
public void testRemovingItem3() {
Milk m = new Milk(3, "Lactose
Free", true);
DVD d = new DVD(10, "Black
Panther", true);
Shirt s = new Shirt(100, "Versace",
false);
AmazonCart amazonCart = new
AmazonCart();
amazonCart.addItem(m);
amazonCart.addItem(m);
amazonCart.removeItem(m);
amazonCart.removeItem(m);
assert(amazonCart.removeItem(d) ==
false);
amazonCart.addItem(d);
amazonCart.addItem(s);
amazonCart.addItem(s);
assert(amazonCart.calculateTotal()
== 224.3);
}
@Test
public void testAddingArrayListOfItems() {
ArrayList mArray = new
ArrayList<>();
Milk m1 = new Milk(3, "Lactose
Free", true);
Milk m2 = new Milk(2, "Whole Milk",
true);
Milk m3 = new Milk(9.5, "Goats
Milk", true);
mArray.add(m1);
mArray.add(m2);
mArray.add(m3);
AmazonCart amazonCart = new
AmazonCart();
amazonCart.addItems(mArray);
}
@Test
public void testRemovingArrayListOfItems() {
ArrayList mArray = new
ArrayList<>();
Milk m1 = new Milk(3, "Lactose
Free", true);
Milk m2 = new Milk(2, "Whole Milk",
true);
Milk m3 = new Milk(9.5, "Goats
Milk", true);
mArray.add(m1);
mArray.add(m2);
mArray.add(m3);
AmazonCart amazonCart = new
AmazonCart();
amazonCart.addItems(mArray);
amazonCart.removeItems(mArray);
assert(amazonCart.calculateTotal()
== 0);
}
}
import java.util.ArrayList;
import java.util.HashMap;
/**
* The Abstract Cart class represents a user's cart. Items of Type T can be added
* or removed from the cart. A hashmap is used to keep track of the number of items
* that have been added to the cart example 2 apples or 4 shirts.
* @author Your friendly CS Profs
* @param <T>
* @param -Type of items that will be placed in the Cart.
*/
//public
abstract class AbstractCart<T> {
protected HashMap cart;
public AbstractCart() {
this.cart = new HashMap();
}
/**
* Calculates the total value of items in the cart.
* @return total (double)
*/
public abstract double calculateTotal();
/**
* Add an item of type T to the Cart (HashMap: The product is the key
* and the value is the count).
* If product doesn't exist in the cart add it and set count to one.
* Otherwise increment the value.
* @param item
* @return boolean
*/
public void addItem(T item) {
//fill in
int count=1;
if(cart.containsKey(item))
{
count+=cart.get(item);
}
cart.put(item, count);
}
/**
* Adds every item in the Arraylist of Type T or any subclass of T.
* @param items: An array of items
* @return true if items have been currently added.
*/
public void addItems(ArrayList items) {
for (int i=0;i<items.size();i++)
{
addItem((T)items.get(i));
}
}
/**
* Removes an item of type T from the list.
* If the only one item, we remove that item.
* If item count is greater than one decrement the count.
* If you are able to remove the item then return true.
* If the item doesn't exist return false.
* @param <T>
* @param item
* @return true
* in the list.
*/
public boolean removeItem(T item) {
//fill in
T temp= (T) cart.remove(item);
if(temp==null) // if item is not available
return false;
return true;
}
/**
* Removes all of the item of items of Type T or any subclass of T from the cart.
* @param item
* @return true if items have been successfully remove.
*/
public void removeItems(ArrayList items) {
for (int i=0;i<items.size();i++)
{
removeItem((T)items.get(i));
}
}
/**
* Check to see if the cart contains an item.
* @param item
* @return true if cart contains the item. Returns False otherwize
*/
public boolean contains(T item) {
return cart.containsKey(item);
}
}