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).

**PLEASE USE THE CLASSES BELOW*** to show 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 ***IMPORTANT

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

    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

Shopping cart in Java, I am going to make Cart as per your question but may be there are some chances that I will miss something so you can add that from your end.

And most importand Please Thumbs-up if you like my work, Thanks in Advance!!(I Hope you will do that).

So lets start coding now:-

BagInterface.java

public interface BagInterface {

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

   Item bag[]=new Item[25];
  public void AddItemToBag(Item i);
  }

ShoppingInJava.java

//Importing needed libraries

import java.util.*;
import java.io.*;
public class ShoppingInJava implements BagInterface{

   int value=0; //Initial value set to 0
   @Override
   public void AddItemToBag(Item i) {
bag[value]=i;
value++; //Post increment of value
       System.out.println("Item Successfully added to your cart");
      
   }
   public void DisplayItems()
   {

// for loop for checking all bag items
       for(int i=0;i<bag.length-1;i++)
       {
           if(bag[i]!=null)
           {

//Facing cart or bag id,name and price
           System.out.print(bag[i].getItemId()+"\t");
           System.out.print(bag[i].getItemName()+"\t");
           System.out.println(bag[i].getItemPrice());
          
           }
       }
   }
  
   public void Checkout()
   {
       DisplayItems(); //Will show all items
       int total=0;
       for(int i=0;i<bag.length-1 && bag[i]!=null ;i++)
       {
total=total+bag[i].getItemPrice();
       }
       System.out.println("Cart Value or Price is:"+total);
   }
   public void addItem(Item i)
   {
       AddItemToBag(i);
   }
   public void addMultipleItem(Item items[])
   {
       for(int i=0;i<3;i++)
       {
           AddItemToBag(items[i]);
       }
   }
   public void removeItem(Item i)
   {
       for(int j=0;j<bag.length-1;j++)
       {
           if(bag[j].getItemId()==i.getItemId())
           {
               for(int k = j; k < bag.length - 1; k++){
       bag[k] = bag[k+1];
       }
       break;
           }
       }
   }
}

Item.java

public class Item {

   private int itemId;
   private int itemPrice;
   private String itemName;
   public String getItemName() {
   return itemName;
   }
   public void setItemName(String itemName) {
       this.itemName = itemName;
   }
   public int getItemId() {
       return itemId;
   }
   public void setItemId(int itemId) {
       this.itemId = itemId;
   }
   public int getItemPrice() {
       return itemPrice;
   }
   public void setItemPrice(int itemPrice) {
       this.itemPrice = itemPrice;
   }
   public Item(int itemId, int itemPrice, String itemName) {
       super();
       this.itemId = itemId;
       this.itemPrice = itemPrice;
       this.itemName =itemName;
   }
  
}

Check.java

import java.util.*;
import java.io.*;
public class Check {

   public static void main(String[] args)
       Scanner sc=new Scanner(System.in);
       System.out.println("Available Items in your Cart");
       System.out.println("ID of the Product\tName\t Final Price\n");
       System.out.println("12. \t Soap \t 80\n");
       System.out.println("22. \t Milk \t 60\n");
       System.out.println("30. \t T-shirt \t 300\n");
       System.out.println("46. \t Power bank \t 699\n");
       Item zero=new Item(12,80,"Soap");
       Item one=new Item(22,60,"Milk");
       Item two=new Item(30,300,"T-shirt");
       Item three=new Item(46,699,"Power bank");
       ShoppingInJava scart=new ShoppingInJava();
       System.out.println("Adding items in Cart");
       scart.addItem(i);
       scart.DisplayItems();
       System.out.println("Adding Multiple items, please wait........");
       Item itemarr[]=new Item[3];
       itemarr[0]=one;
       itemarr[1]=two;
       itemarr[2]=three;
       scart.addMultipleItem(itemarr);
       scart.DisplayItems();
       System.out.println("Removing items from Cart");
       scart.removeItem(one);
       scart.DisplayItems();
       System.out.println("Returning to Checkout Function() ");
       scart.Checkout();
      }

}

Again, Please consider a thumbs-up or hit that like button.


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...
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....
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.
Create a simple shopping cart application. Ask the user to input an item number and quantity....
Create a simple shopping cart application. Ask the user to input an item number and quantity. Update the shopping cart. Display the updated cart. Include the following when displaying cart: item number, name, quantity and total price. Ask for another update or end the program. Design Requires 2 classes: the main class and the cartItem class In the main class: Need one array named cartArray of type cartItem. Size = ? Need a loop that asks the user for input....
Create a simple shopping cart application. Ask the user to input an item number and quantity....
Create a simple shopping cart application. Ask the user to input an item number and quantity. Update the shopping cart. Display the updated cart. Include the following when displaying cart: item number, name, quantity and total price. Ask for another update or end the program. Design Requires 2 classes: the main class and the cartItem class In the main class: Need one array named cartArray of type cartItem. Size = ? Need a loop that asks the user for input....
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount to represent a bank account according to the following requirements: A bank account has three attributes: accountnumber, balance and customer name. Add a constructor without parameters. In the initialization of the attributes, set the number and the balance to zero and the customer name to an empty string. Add a constructor with three parameters to initialize all the attributes by specific values. Add a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT