Question

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

Homework Answers

Answer #1

I have covered functions as much as possible , Please Thumbs Up If you found this helpful , Thank you!!

*********************************************************

BagInterface.java


public interface BagInterface {
   Item bag[]=new Item[10];
  
   public void AddItemToBag(Item i);
  
}

*****************************************

ShoppingCart.java

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

   int index=0;
   @Override
   public void AddItemToBag(Item i) {
      
       bag[index]=i;
       index++;
       System.out.println("added to cart");
      
   }
   public void DisplayItems()
   {
       for(int i=0;i<bag.length-1;i++)
       {
           if(bag[i]!=null)
           {
           System.out.print(bag[i].getItemId()+"\t");
           System.out.print(bag[i].getItemName()+"\t");
           System.out.println(bag[i].getItemPrice());
          
           }
       }
   }
  
   public void Checkout()
   {
       DisplayItems();
       int sum=0;
       for(int i=0;i<bag.length-1 && bag[i]!=null ;i++)
       {
           sum=sum+bag[i].getItemPrice();
       }
       System.out.println("Total price is:"+sum);
   }
   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;
   }
  
}
******************************************

Test.java

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

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       Scanner sc=new Scanner(System.in);
       System.out.println("************Item List***********");
       System.out.println("id\tname\tprice\n");
       System.out.println("1\ta\t10\n");
       System.out.println("2\tb\t15\n");
       System.out.println("3\tc\t30\n");
       System.out.println("4\td\t20\n");
       Item i=new Item(1,10,"a");
       Item i1=new Item(2,15,"b");
       Item i2=new Item(3,30,"c");
       Item i3=new Item(4,20,"d");
       ShoppingCart scart=new ShoppingCart();
       System.out.println("adding first item");
       scart.addItem(i);
       scart.DisplayItems();
       System.out.println("adding second,third,fourth item at same time");
       Item itemarr[]=new Item[3];
       itemarr[0]=i1;
       itemarr[1]=i2;
       itemarr[2]=i3;
       scart.addMultipleItem(itemarr);
       scart.DisplayItems();
       System.out.println("Removing second item");
       scart.removeItem(i1);
       scart.DisplayItems();
       System.out.println("Calling checkout function");
       scart.Checkout();
      
   }

  
}
******************************************************

Output Screenshot

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). Extend...
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). Extend the ItemToPurchase class per the following specifications              Private fields string itemDescription - Initialized in default constructor to "none" Parameterized constructor to assign item name, item description, item price, and itemquantity (default values of 0).             Public instance member methods setDescription() mutator & getDescription() accessor (2 pts) printItemCost() - Outputs the item name followed by the quantity, price, and subtotal printItemDescription() - Outputs the...
****in java Create a class CheckingAccount with a static variable of type double called interestRate, two...
****in java Create a class CheckingAccount with a static variable of type double called interestRate, two instance variables of type String called firstName and LastName, an instance variable of type int called ID, and an instance variable of type double called balance. The class should have an appropriate constructor to set all instance variables and get and set methods for both the static and the instance variables. The set methods should verify that no invalid data is set. Also define...
this is the book name. Data Structures and Abstractions with Java 1) Description: The sample programs...
this is the book name. Data Structures and Abstractions with Java 1) Description: The sample programs in Chapter 1 of your textbook are not complete. They are used for illustration purpose only. The implementation of Listing 1-1 on page 39 is explained in Chapter 2. And, in order to see the result of using it, we will need the following set of files: i. BagInteface.java – the specification only. ii. ArrayBag.java – the implementation of BagInerface.java. iii. ArrayBagDemo.java – a...
In java just need work in the billards class it is in bold everything else is...
In java just need work in the billards class it is in bold everything else is done just the sections marked TODO Suppose that you have several numbered billiard balls on a pool table. The smallest possible number on the ball is “1”. At each step, you remove a billiard ball from the table. If the ball removed is numbered n, you replace it with n balls randomly numbered less than n. For example, if you remove the “5” ball,...
Write a class called Item that has a string field called name and a double field...
Write a class called Item that has a string field called name and a double field called price and an integer field called quantity. In main, create a bag of type Item and place several item objects in the bag. Then in main calculate the total price of items purchased. You may remove each item and add the price to a total variable in a loop. The loop should end when the bag is empty. Using bag algorithms public final...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how...
Delta airlines case study Global strategy. Describe the current global strategy and provide evidence about how the firms resources incompetencies support the given pressures regarding costs and local responsiveness. Describe entry modes have they usually used, and whether they are appropriate for the given strategy. Any key issues in their global strategy? casestudy: Atlanta, June 17, 2014. Sea of Delta employees and their families swarmed between food trucks, amusement park booths, and entertainment venues that were scattered throughout what would...