I have to make a program that simulates a shopping cart, I have it done, it's just that the shopping cart wont print. Please help me in fixing this, this is due for me tomorrow and is worth 40% of this chapter grade.
The files are attached below. The main one is ShoppingSim.java

Attachments
// ***************************************************************
//   Item.java
//
//   Represents an item in a shopping cart.
// ***************************************************************

import java.text.NumberFormat;

public class Item
{
    private String name;
    private double price;
    private int quantity;

    // -------------------------------------------------------
    //  Create a new item with the given attributes.
    // -------------------------------------------------------
    public Item (String itemName, double itemPrice, int numPurchased)
    {
	name = itemName;
	price = itemPrice;
	quantity = numPurchased;
    }

    // -------------------------------------------------------
    //   Return a string with the information about the item
    // -------------------------------------------------------
    public String toString()
    {
	NumberFormat fmt = NumberFormat.getCurrencyInstance();
	String ret = (name + "\t" + quantity + "\t" + fmt.format(price) + "\t"
		+ fmt.format(price*quantity));
	return ret;
    }

    // -------------------------------------------------
    //   Returns the unit price of the item
    // -------------------------------------------------
    public double getPrice()
    {
	return price;
    }

    // -------------------------------------------------
    //   Returns the name of the item
    // -------------------------------------------------
    public String getName()
    {
	return name;
    }

    // -------------------------------------------------
    //   Returns the quantity of the item
    // -------------------------------------------------
    public int getQuantity()
    {
	return quantity;
    }
}
// **********************************************************************
//   ShoppingCart.java
//
//   Represents a shopping cart as an array of items
// **********************************************************************

import java.text.NumberFormat;

public class ShoppingCart
{
    private int itemCount;      // total number of items in the cart
    private double totalPrice;  // total price of items in the cart
    private int capacity;       // current cart capacity
    Item[] cart;

    // -----------------------------------------------------------
    //  Creates an empty shopping cart with a capacity of 5 items.
    // -----------------------------------------------------------
    public ShoppingCart()
    {
	capacity = 5;
	itemCount = 0;
	totalPrice = 0.0;
	cart = new Item[itemCount];
    }

    // -------------------------------------------------------
    //  Adds an item to the shopping cart.
    // -------------------------------------------------------
    public void addToCart(String itemName, double price, int quantity)
    {
    	// check if the new element has no space, if so increase the size
        if (itemCount + 1 > capacity)
            increaseSize();
        //store the element
        cart[itemCount] = new Item(itemName,price,quantity);
 		System.out.print(cart[itemCount]);
        // we have an additional element
        System.out.print(itemCount);
        itemCount=itemCount+1;
        System.out.print(itemCount);
    }

    // -------------------------------------------------------
    //  Returns the contents of the cart together with
    //  summary information.
    // -------------------------------------------------------
    public void print()
    {
	NumberFormat fmt = NumberFormat.getCurrencyInstance();

	String contents = "\nShopping Cart\n";
	contents += "\nItem\t\tQuantity\tPrice\tTotal\n";
	System.out.print(contents);
	System.out.println(itemCount);
	for (int I = 0; I < itemCount; I++)
	   System.out.println(cart[I].toString() + "\n");

	System.out.println("\nTotal Price: " + fmt.format(totalPrice));
	

	
    }

    // ---------------------------------------------------------
    //  Increases the capacity of the shopping cart by 3
    // ---------------------------------------------------------
    private void increaseSize()
    {
    	//create an new array and add 3 to the size
        Item[] myarray = new Item[cart.length +3];
 
        //copy all existing elements
        for (int i = 0; i < cart.length+1; i++)
            myarray[i] = cart[i];
 
        //make the original array point to the larger, new array
        cart = myarray;
        
    }
}
/**
 * @(#)ShoppingSim.java
 *
 *
 * @author ABDUL ALI SHAIKH
 * @version 1.00 2009/1/15
 */
import java.util.Scanner;
public class ShoppingSim {
        
    static ShoppingCart mart;
    static Scanner scan = new Scanner(System.in);
    
    public static void main(String[] args)
    {
        printMenu();
        int choice = scan.nextInt();
        while (choice != 0)
        {
            dispatch(choice);
            printMenu();
            choice = scan.nextInt();
        }
    }
 
    //--------------------------------------
    // Do what the menu item calls for
    //--------------------------------------
    public static void dispatch(int choice)
    {
        mart = new ShoppingCart();
        String name= new String();
        double price;
        int quantity;
        switch(choice)
        {
            case 0: 
                System.out.println("End your shopping spree :(");
                break;
            case 1:
                System.out.println("Enter the Item name, quantity, and price");
                System.out.print("Add: ");
                scan.nextLine();
                name = scan.nextLine();
                quantity = scan.nextInt();
                price = scan.nextDouble();
                mart.addToCart(name,price,quantity);
                break;
            case 2:
                mart.print();
                // Call break to avoid falling through to the default case.
                break;
            default:
                System.out.
                	println("Sorry, invalid choice");
        }
    }
 
    //----------------------------
    // Print the user's choices
    //----------------------------
    public static void printMenu()
    {
        System.out.println("\n****SHOPING CART****");
        System.out.println("0: Quit");
        System.out.println("1: Add an Item to the list");
        System.out.println("2: Print the list");
        System.out.print("\nEnter your choice: ");
    }
}

You didn't ask a question or explain what was wrong with the program. Surely if you've worked on it you know an area where your code doesn't work.

just post the code you want us to review here, not in attachment.
as far as I understand from your description, you've got an array of some sort, containing objects of a class (for instance, Product)

to print those: iterate over the elements of the array, and print each element. make sure you have a toString() method in your Product class, though

From a quick look at your code you have this:

case 2:
                mart.print();
                // Call break to avoid falling through to the default case.
                break;

It is suppose to print the shopping cart. Are you having problems with that?

From a quick look at your code you have this:

case 2:
                mart.print();
                // Call break to avoid falling through to the default case.
                break;

It is suppose to print the shopping cart. Are you having problems with that?

Yes, that is exactly the problem, thankfully our teacher gave us one more day. Can you explain to me why it wont print the cart? I'm confused.

Oh, I think it's printing the cart just fine.

The problem is that your menu choice handler creates a new cart every time it's called.

I only see one logical place for it, given the structure of your ShoppingSim driver program. Where do you think it should go? Where would it make sense to create a new cart?

I only see one logical place for it, given the structure of your ShoppingSim driver program. Where do you think it should go? Where would it make sense to create a new cart?

YES!!! I put the new operator outside of the main method, and it worked!!! I'll get a 100 now! Thank you so much for identifying the prob
But one more problem, this one worth 60/300 points. I have to do the SAME thing, but with Arraylists.

You shouldn't have any problems doing that. The API for ArrayList is pretty straightforward. You don't have to manage the size of it yourself and you have methods to add(), get(), and remove() items. If you run into difficulties, post the code here along with your questions or error messages.

No, you replace your array with an instance of an ArrayList and use it's methods to manage the items.

No, you replace your array with an instance of an ArrayList and use it's methods to manage the items.

I have a prob with original, whenever item quantity is more than 1, it mulitiplys the whole thing by that num

No, you replace your array with an instance of an ArrayList and use it's methods to manage the items.

Ok, that wasn;t the prob, the prob is that whenever I use option 2 it doubles the total. I have no idea why

This article has been dead for over six months. Start a new discussion instead.