In: Computer Science
Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart instance should contain a BagInterface implementation that will serve to hold the Items that will be added to the cart. Use the implementation of the Item object provided in Item.java. Note that the price is stored as the number of cents so it can be represented as an int (e.g., an Item worth $19.99 would have price = 1999).
Using the CLASSES BELOW Your shopping cart should support the following operations:
Add an item
Add multiple quantities of a given item (e.g., add 3 of Item __)
Remove an unspecified item
Remove a specified item
Checkout – should "scan" each Item in the shopping cart (and display its
information), sum up the total cost and display the total cost
Check budget – Given a budget amount, check to see if the budget is large
enough to pay for everything in the cart. If not, remove an Item from the shopping cart, one at a time, until under budget.
Write a driver program to test out your ShoppingCart implementation.
*Item Class*
/**
* Item.java - implementation of an Item to be placed in ShoppingCart
*/
public class Item
{
private String name;
private int price;
private int id;//in cents
//Constructor
public Item(int i, int p, String n)
{
name = n;
price = p;
id = i;
}
public boolean equals(Item other)
{
return this.name.equals(other.name) && this.price == other.price;
}
//displays name of item and price in properly formatted manner
public String toString()
{
return name + ", price: $" + price/100 + "." + price%100;
}
//Getter methods
public int getPrice()
{
return price;
}
public String getName()
{
return name;
}
}
BAG INTERFACE CLASS
/**
* BagInterface.java - ADT Bag Type
* Describes the operations of a bag of objects
*/
public interface BagInterface<T>
{
//getCurrentSize() - gets the current number of entries in this bag
// @returns the integer number of entries currently in the bag
public int getCurrentSize();
//isEmpty() - sees whether the bag is empty
// @returns TRUE if the bag is empty, FALSE if not
public boolean isEmpty();
//add() - Adds a new entry to this bag
// @param newEntry - the object to be added to the bag
// @returns TRUE if addition was successful, or FALSE if it fails
public boolean add(T newEntry);
//remove() - removes one unspecified entry from the bag, if possible
// @returns either the removed entry (if successful), or NULL if not
public T remove();
//remove(T anEntry) - removes one occurrence of a given entry from this bag, if possible
// @param anEntry - the entry to be removed
// @returns TRUE if removal was successful, FALSE otherwise
public boolean remove(T anEntry);
//clear() - removes all entries from the bag
public void clear();
//contains() - test whether this bag contains a given entry
// @param anEntry - the entry to find
// @returns TRUE if the bag contains anEntry, or FALSE otherwise
public boolean contains(T anEntry);
//getFrequencyOf() - count the number of times a given entry appears in the bag
// @param anEntry - the entry to count
// @returns the number of time anEntry appears in the bag
public int getFrequencyOf(T anEntry);
//toArray() - retrieve all entries that are in the bag
// @returns a newly allocated array of all the entries in the bag
// NOTE: if bag is empty, it will return an empty array
public T[] toArray();
}
BagInterface.java
import ItemLinkedBag.LinkedBag.Node;
public interface BagInterface<T> {
//isEmpty() - sees whether the bag is empty
// @returns TRUE if the bag is empty, FALSE if not
public boolean isEmpty();
//getCurrentSize() - gets the current number of entries in this
bag
// @returns the integer number of entries currently in the
bag
public int getCurrentSize();
//add() - Adds a new entry to this bag
// @param newEntry - the object to be added to the bag
// @returns TRUE if addition was successful, or FALSE if it
fails
public boolean add(T newEntry);
//toArray() - retrieve all entries that are in the bag
// @returns a newly allocated array of all the entries in the
bag
// NOTE: if bag is empty, it will return an empty array
public T[] toArray();
//getFrequencyOf() - count the number of times a given entry
appears in the bag
// @param anEntry - the entry to count
// @returns the number of time anEntry appears in the bag
public int getFrequencyOf(T anEntry);
//contains() - test whether this bag contains a given entry
// @param anEntry - the entry to find
// @returns TRUE if the bag contains anEntry, or FALSE
otherwise
public boolean contains(T anEntry);
//remove(T anEntry) - removes one occurrence of a given entry from
this bag, if possible
// @param anEntry - the entry to be removed
// @returns TRUE if removal was successful, FALSE otherwise
public boolean remove(T anEntry);
//clear() - removes all entries from the bag
public void clear();
//remove() - removes one unspecified entry from the bag, if
possible
// @returns either the removed entry (if successful), or NULL if
not
public T remove();
public Node getReferenceTo(T anEntry);
public void display();
}
LinkedBag.java
import java.util.Random;
public class LinkedBag<T> implements BagInterface<T> {
private Node firstNode;
private int numberOfEntries;
// Default constructor
public LinkedBag() {
firstNode = null;
numberOfEntries = 0;
}
@Override
public void display()
{
Node temp = firstNode;
while(temp != null)
{
System.out.println("*** " + temp.item);
temp = temp.next;
}
System.out.println();
}
@Override
public boolean isEmpty() {
return numberOfEntries == 0;
}
@Override
public int getCurrentSize() {
return numberOfEntries;
}
@Override
public boolean add(T newEntry) {
Node newNode = new Node(newEntry);
newNode.next = firstNode;
firstNode = newNode;
numberOfEntries++;
return true;
}
@Override
public T[] toArray() {
@SuppressWarnings("unchecked")
T[] result = (T[]) new Object[numberOfEntries]; // Unchecked
cast
int index = 0;
Node currentNode = firstNode;
while ((index < numberOfEntries) && (currentNode !=
null)) {
result[index] = currentNode.item;
index++;
currentNode = currentNode.next;
}
return result;
}
@Override
public int getFrequencyOf(T anEntry) {
int frequency = 0;
int loopCounter = 0;
Node currentNode = firstNode;
while ((loopCounter < numberOfEntries) && (currentNode
!= null)) {
if (anEntry.equals(currentNode.item)) {
frequency++;
}
loopCounter++;
currentNode = currentNode.next;
}
return frequency;
}
@Override
public boolean contains(T anEntry) {
boolean found = false;
Node currentNode = firstNode;
while (!found && (currentNode != null)) {
if (anEntry.equals(currentNode.item)) {
found = true;
break;
} else {
currentNode = currentNode.next;
}
}
return found;
}
@Override
public Node getReferenceTo(T anEntry) {
boolean found = false;
Node currentNode = firstNode;
while (!found && (currentNode != null)) {
if (anEntry.equals(currentNode.item)) {
found = true;
break;
} else {
currentNode = currentNode.next;
}
}
return currentNode;
}
@Override
public boolean remove(T anEntry) {
boolean result = false;
Node nodeN = getReferenceTo(anEntry);
if (nodeN != null) {
nodeN.item = firstNode.item;
firstNode = firstNode.next;
numberOfEntries--;
result = true;
}
return result;
}
@Override
public void clear() {
while (!isEmpty()) {
remove();
}
}
@Override
public T remove() {
T result = null;
/*T[] display = toArray();
for (int j = 0; j < display.length; j++) {
System.out.println(display[j]);
}*/
//System.out.println(numberOfEntries);
//System.out.println(this.toString());
Random rng = new Random();
Node scout = firstNode;
if (numberOfEntries <= 0) {
return result; // Do nothing
} else if (numberOfEntries == 1) {
int index = 0;
T[] temp = toArray();
//System.out.println("Removing: "+temp[index]);
result = temp[index];
boolean success = remove(temp[index]);
//System.out.println(success);
} else {
int index = rng.nextInt(numberOfEntries - 1);
//System.out.println("Index: "+index);
if (!isEmpty() && index >= 0) {
for (int i = 0; i < index; i++) {
scout = scout.next;
}
result = scout.item;
//System.out.println("Result: "+result);
scout.item = firstNode.item;
//System.out.println("Scout: "+scout.data);
}
if (firstNode != null) {
firstNode = firstNode.next;
//System.out.println("First: "+firstNode.data);
numberOfEntries--;
}
}
return result;
}
public class Node {
private T item; // Entry in bag
private Node next; // link to next node
private Node(T dataPortion) {
this(dataPortion, null);
} // end constructor
private Node(T dataPortion, Node nextNode) {
item = dataPortion;
next = nextNode;
}
}
}
Test.java (Main class)
public class Test {
public static void main(String[] args) {
LinkedBag<Item> bag = new LinkedBag<>();
System.out.println("Adding some items to the bag..");
Item item1 = new Item(1722, 289, "HP Speakers");
Item item2 = new Item(2984, 14, "Boat Rockerzz Headphone");
Item item3 = new Item(6518, 98, "Logitech Gaming Mouse");
Item item4 = new Item(4521, 3899, "Apple iPhone 11");
Item item5 = new Item(8355, 2673, "OnePlus 7T");
bag.add(item1);
bag.add(item2);
bag.add(item3);
bag.add(item4);
bag.add(item5);
System.out.println("Displaying the contents of the bag..");
bag.display();
System.out.println("Current size of the bag = " +
bag.getCurrentSize());
Item itemTest = new Item(3902, 365, "Zebronics Pro Max");
System.out.println("\nChecking whether the bag contains an
item..\n"
+ item3 + " --> " + bag.contains(item3) + "\n"
+ itemTest + " --> " + bag.contains(itemTest));
}
}
************************************************************ SCREENSHOT ****************************************************