All pastes #960844 Raw Edit

Miscellany

public text v1 · immutable
#960844 ·published 2008-03-28 14:36 UTC
rendered paste body
import java.text.NumberFormat;

public class Item
{
	private String name;
	private double price;
	private int quantity;
	
	public Item (String itemName, Double itemPrice, int numPurchased)
	{
		name=itemName;
		price=itemPrice;
		quantity=numPurchased;
	}
	
	public String toString()
	{
		NumberFormat fmt = NumberFormat.getCurrencyInstance();
		return ( name + "\t" + fmt.format(price) + "\t" + quantity + "\t" + fmt.format(price*quantity));
	}
	
	public double getPrice()
	{ 
	     return price;
	}
	
	public String getName()
	{
		return name;
	}
	
	public int getQuantity()
	{
		return quantity;
	}
}



import java.text.NumberFormat;

public class ShoppingCart
{
	private int itemCount;
	private double totalPrice;
	private int capacity;
	private Item [] cart;
	private int count = 0;
	
	public ShoppingCart()
	{
		capacity = 5;
		itemCount =0;
		totalPrice =0.0;
		cart=new Item[capacity];
	}
	
	public void addToCart(String itemName,double price,int quantity)
	{
		if(count==cart.length)
		increaseSize();
		cart[count]=new Item(itemName,price,quantity);
		totalPrice=totalPrice+quantity*price;
		count++;
	}
	
	public String toString()
	{
		NumberFormat fmt = NumberFormat.getCurrencyInstance();
		
		String contents = "\nShopping Cart\n";
		contents += "\nItem\t\tUnit Price\tQuantity\tTotal\n";
		
		for (int i=0; i < itemCount; i++)
		contents += cart[i].toString()+"\n";
		
		contents += "\nTotal Price: " + fmt.format(totalPrice);
		contents += "\n";
		
		return contents;
	}
	
	private void increaseSize()
	{
		Item[] temp=new Item[cart.length+3];
		for (int x=0; x<cart.length; x++)
		temp[x]=cart[x];
		cart=temp;
	}
	
}