Question

In: Computer Science

Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart...

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

  

}

Solutions

Expert Solution

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 ****************************************************


Related Solutions

Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart...
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). Your shopping cart should support...
Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart...
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...
Create a ShoppingCart class in java that simulates the operation of a shopping cart. The ShoppingCart...
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). **PLEASE USE THE CLASSES BELOW***...
Java Coding Background You will create a Java class that simulates a water holding tank. The...
Java Coding Background You will create a Java class that simulates a water holding tank. The holding tank can hold volumes of water (measured in gallons) that range from 0 (empty) up to a maximum. If more than the maximum capacity is added to the holding tank, an overflow valve causes the excess to be dumped into the sewer system. Assignment The class will be named HoldingTank. The class attributes will consist of two int fields – current and maxCapacity....
Java Coding Background You will create a Java class that simulates a water holding tank. The...
Java Coding Background You will create a Java class that simulates a water holding tank. The holding tank can hold volumes of water (measured in gallons) that range from 0 (empty) up to a maximum. If more than the maximum capacity is added to the holding tank, an overflow valve causes the excess to be dumped into the sewer system. Assignment The class will be named HoldingTank. The class attributes will consist of two int fields – current and maxCapacity....
5.9 Online shopping cart (Java) Create a program using classes that does the following in the...
5.9 Online shopping cart (Java) Create a program using classes that does the following in the zyLabs developer below. For this lab, you will be working with two different class files. To switch files, look for where it says "Current File" at the top of the developer window. Click the current file name, then select the file you need. (1) Create two files to submit: ItemToPurchase.java - Class definition ShoppingCartPrinter.java - Contains main() method Build the ItemToPurchase class with the...
HOMEWORK PROJECT #1 – SHOPPING CART Part I. Create two files to submit: ItemToPurchase.java – Class...
HOMEWORK PROJECT #1 – SHOPPING CART Part I. Create two files to submit: ItemToPurchase.java – Class Definition ShoppingCartPrinter.java – Contains main() method Build the ItemToPurchase class with the following specifications: Specifications Description ItemToPurchase(itemName) itemName – The name will be a String datatype and Initialized in default constructor to “none”. ItemToPurchase(itemPrice) itemPrice – The price will be integer datatype and Initialized in default constructor to 0. ItemToPurchase(itemQuantity) itemQuantity – The quantity will be integer datatype Initialized in default constructor to 0....
Create a Python 3 program that acts as a grocery store shopping cart. 1. Create a...
Create a Python 3 program that acts as a grocery store shopping cart. 1. Create a class named GroceryItem. This class should contain: - an __init__ method that initializes the item’s name and price - a get_name method that returns the item’s name - a get_price method that returns the item’s price - a __str__ method that returns a string representation of that GroceryItem - an unimplemented method is_perishable Save this class in GroceryItem.py. 2. Create a subclass Perishable that...
In C++ Create a class that simulates a school calendar for a course and has a...
In C++ Create a class that simulates a school calendar for a course and has a warner that provides the school administration with the option of warning students when the last day is that a student is permitted to drop the course. (Allow administrators to warn or not, as they wish. Do not make this a required function.) You will assume in this calendar that there are 12 months in a year, 4 weeks in a month, and 7 days...
9.) The 17 kg shopping cart carries a 73 kg child. The shopping cart wheel base...
9.) The 17 kg shopping cart carries a 73 kg child. The shopping cart wheel base b = 480 mm and its vertical CG is symmetrically located 580 mm above the floor surface. The child remains centered symmetrically between the cart wheels with her vertical CG located 750 mm above the floor surface. Determine (a) location above the floor of the vertical CG of the cart + child in mm and (b) the tipping angle in degrees.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT