Question

In: Computer Science

Abstract Cart class import java.util.ArrayList; import java.util.HashMap; /** * The Abstract Cart class represents a user's...

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);
   }
}

Solutions

Expert Solution


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);
   }


  
}

Related Solutions

import java.util.ArrayList; import java.util.Collections; public class BirthdayList { /** * This is the main process for...
import java.util.ArrayList; import java.util.Collections; public class BirthdayList { /** * This is the main process for class BirthdayList */ public static void main() { System.out.println("\n\tBegin Birthday List Program\n");    System.out.println("Declare an ArrayList for Birthday objects"); // 1. TO DO: Declare an ArrayList to store Birthday objects (3 Pts)       // 2. TO DO: Replace the xxx.xxxx() with the appropriate method (2 Pts) System.out.printf("\nBirthday List is empty: %s\n", xxx.xxxx() ? "True" : "False");    System.out.println("Add at least five Birthdays to...
import java.util.ArrayList; import java.util.Collections; import java.lang.Exception; public class ItemList { /** This is the main process...
import java.util.ArrayList; import java.util.Collections; import java.lang.Exception; public class ItemList { /** This is the main process for the project */ public static void main () { System.out.println("\n\tBegin Item List Demo\n"); System.out.println("Declare an ArrayList to hold Item objects"); ArrayList<Item> list = new ArrayList<Item>(); try { System.out.println("\n Add several Items to the list"); list.add(new Item(123, "Statue")); list.add(new Item(332, "Painting")); list.add(new Item(241, "Figurine")); list.add(new Item(126, "Chair")); list.add(new Item(411, "Model")); list.add(new Item(55, "Watch")); System.out.println("\nDisplay original Items list:"); listItems(list); int result = -1; // 1....
Convert this pseudo code program into sentences. import java.util.ArrayList; import java.util.Collections; class Bulgarian {    public...
Convert this pseudo code program into sentences. import java.util.ArrayList; import java.util.Collections; class Bulgarian {    public static void main(String[] args)    {        max_cards=45;        arr->new ArraryList        col=1;        card=0;        left=max_cards;        do{            col->random number            row->new ArrayList;            for i=0 to i<col            {                card++                add card into row            }   ...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode...
import java.util.Stack; import java.util.ArrayList; import java.util.Scanner; class TreeNode{ int data; ArrayList<TreeNode> children = new ArrayList<>(); TreeNode parent = null;    public TreeNode(int d){ data = d; }    public TreeNode addChild(int d){ TreeNode n = new TreeNode(d); n.setParent(this); children.add(n); return n; }    public ArrayList<TreeNode> getChildren(){ return children; }    public void setParent(TreeNode p){ parent = p; }    public TreeNode getParent(){ return parent; } } class Main { public static void main(String[] args)    {        Scanner scan...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task>...
NEed UML diagram for this java code: import java.util.ArrayList; import java.util.Scanner; class ToDoList { private ArrayList<Task> list;//make private array public ToDoList() { //this keyword refers to the current object in a method or constructor this.list = new ArrayList<>(); } public Task[] getSortedList() { Task[] sortedList = new Task[this.list.size()];//.size: gives he number of elements contained in the array //fills array with given values by using a for loop for (int i = 0; i < this.list.size(); i++) { sortedList[i] = this.list.get(i);...
This is the code that needs to be completed... import java.util.ArrayList; import java.util.Collections; /** * Write...
This is the code that needs to be completed... import java.util.ArrayList; import java.util.Collections; /** * Write a description of class SpellChecker here. * * @author (your name) * @version (a version number or a date) */ public class SpellChecker { private ArrayList words; private DictReader reader; /** * Constructor for objects of class SpellChecker */ public SpellChecker() { reader = new DictReader("words.txt"); words = reader.getDictionary(); } /** * This method returns the number of words in the dictionary. * Change...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final ArrayList<ItemOrder> itemOrder;    private double total = 0;    private double discount = 0;    ShoppingCart() {        itemOrder = new ArrayList<>();        total = 0;    }    public void setDiscount(boolean selected) {        if (selected) {            discount = total * .1;        }    }    public double getTotal() {        total = 0;        itemOrder.forEach((order) -> {            total +=...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT