SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Stuart Reges, Marty Stepp
Chapter:
Arraylists
Exercise:
Programming Projects
Question:1 | ISBN:9780136091813 | Edition: 2

Question

Write classes to model a shopping list. Make an Item class that represents a grocery item’s name and price, such as tissues for $3. Also implement an ItemOrder class that represents a shopper’s desire to purchase a given item in a given quantity, such as five boxes of tissues. You might wish to implement bulk-discounted items, such as two boxes of tissue for $4, which would bring the cost of the given item order of 2 + 2 + 1 boxes of tissues to $4 + $4 + $3, or $11.00. Lastly, implement a ShoppingCart class that stores ItemOrder s in an ArrayList and allows item orders to be added to, removed from, or searched for in the cart. The cart should be able to report the total price of all item orders it currently carries.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

//Item.java
public class Item 
{

	private String iName;
	private double cost;

	public Item(String iName, double cost) 
	{

		setItem(iName, cost);
	}

	public void setItem(String iName, double cost) 
	{
		this.setiName(iName);
		this.setcost(cost);
	}

	public String getiName() 
	{
		return iName;
	}

	public void setiName(String iName)
	{
		this.iName = iName;
	}

	public double getcost() 
	{
		return cost;
	}

	public void setcost(double cost) 
	{
		this.cost = cost;
	}

}

 

//ItemOrder.java
public class ItemOrder
{
	
	private int qty;
	private Item itm;
	public ItemOrder(Item itm,int qty)
	{
		this.itm=itm;
		this.setqty(qty);
	}
	public int getqty() 
     {
		return qty;
	}
	public void setqty(int qty)
     {
		this.qty = qty;
	}
	public double getiOrderPrice()
	{
		double iOrderPrice=0;
		if(qty%2==0)
		{
			for(int i=0;i<qty/2;i++)
			iOrderPrice+=4.0;		
			return iOrderPrice;
		}
		else
		{
			for(int index=0;index<qty/2;index++)
			iOrderPrice+=4.0;
			
			return iOrderPrice+itm.getcost();	
		}
	}
}

 

//ShoppingCart.java
import java.util.ArrayList;

public class ShoppingCart
{
	
    private ArrayList<ItemOrder> shpCart=new ArrayList<ItemOrder>();
	
 	public ShoppingCart()
 	{
 		
 	}
	public void add(ItemOrder iOrder)
     {
	  shpCart.add(iOrder);
	}
	public void remove(ItemOrder riOrder)
	{
		shpCart.remove(riOrder);
	}
	public boolean searchItem(ItemOrder sOrder)
	{
	  boolean found=false;
	   for(int index=0;index<shpCart.size();index++)
	   {
		if(sOrder.equals(shpCart.get(index)))
		found=true;
	   }
	    return found;
	}
	public double getTotalPrice()
	{
	   double total=0;
	   for(int i=0;i<shpCart.size();i++)
		{
		ItemOrder iorder=shpCart.get(i);
		total+=iorder.getiOrderPrice();
		}
	   return total;
	}
}

 

//ShoppingCartDemo.java
public class ShoppingCartDemo
{
	
   public static void main(String[] args) 
   {
		
	Item item1=new Item("Steel",2);
	Item item2=new Item("Iron",2);
	Item item3=new Item("magnatic",1);
	ItemOrder item1Order=new ItemOrder(item1, 4);
	ItemOrder item2Order=new ItemOrder(item2, 4);
	ItemOrder item3Order=new ItemOrder(item3, 3);
	ItemOrder item4Order=new ItemOrder(item3, 11);
	ShoppingCart shopCart=new ShoppingCart();
	shopCart.add(item1Order);
	shopCart.add(item2Order);
	shopCart.add(item3Order);

        System.out.println("Total cost:" +shopCart.getTotalPrice());
        System.out.println("Removing item one Order from  shopping cart:");
          shopCart.remove(item1Order);
          System.out.println("Total cost :"+shopCart.getTotalPrice());
           System.out.println("Checking for item three order:");
          	boolean isFound=shopCart.
                       searchItem(item3Order);
		
		if(isFound)
			System.out.println("Yes");
		else
			System.out.println("No");
		
	     System.out.println("Checking for item four order in the shopping cart");
	      isFound=shopCart.searchItem(item4Order);
	if(isFound)
		System.out.println(":Yes");
else
		System.out.println(":No");	
	}
}

 

OUTPUT:

Total cost:21.0
Removing item one Order from  shopping cart:
Total cost :13.0
Checking for item three order:
Yes
Checking for item four order in the shopping cart
:No

 

1 0

Discussions

Post the discussion to improve the above solution.