Results 1 to 3 of 3

Thread: Shopping cart problems

  1. #1
    Join Date
    Apr 2005
    Posts
    1,333
    Rep Power
    0

    Default Shopping cart problems

    I have this assigment to work on. It involves a shoppiong cart. I have added the code to display the items in the cart, but the output doesnt make sense. It prints the following:

    Items in your shopping cart: [LItem;@ca0b6

    The code is as follows:

    Code:
    // ***************************************************************
    //   Shop.java
    //
    //   Uses the Item class to create items and add them to a shopping
    //   cart stored in an ArrayList.
    // ***************************************************************
    
    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class Shop
    {
        public static void main (String[] args)
        {
    
    	Item item[];
    	String itemName;
    	double itemPrice;
    	int quantity;
    	ArrayList cart = new ArrayList();
    
     	Scanner scan = new Scanner(System.in);
    
    	String keepShopping = "y";
    
    	do 
    	    {
    		System.out.print ("Enter the name of the item: "); 
    		itemName = scan.nextLine();
    
    		System.out.print ("Enter the unit price: ");
    		itemPrice = scan.nextDouble();
    
    		System.out.print ("Enter the quantity: ");
    		quantity = scan.nextInt();
    
    		// *** create a new item and add it to the cart
    		item = new Item[1];
    		System.out.print(item.toString());
    		
    		item.toString();
    		cart.add(item);
    		
    		// *** print the contents of the cart object using println
    		System.out.print("Items in your shopping cart: " + cart.toString());
    		
    		System.out.println("");
    		System.out.print ("Continue shopping (y/n)? ");
    		keepShopping = scan.nextLine();
    	    }
    	while (keepShopping.equals("y"));
    
        }
    }
    and

    Code:
    // ***************************************************************
    //   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();
    
    	return (name + "\t" + fmt.format(price) + "\t" + quantity + "\t"
    		+ fmt.format(price*quantity));
        }
    
        // -------------------------------------------------
        //   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;
        }
    }
    I tried hard coding the items instead of using the input values, but it gives the same error.

    Help please.
    The fox was probably right - they could have been sour grapes.

  2. #2
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    1. You created a variable to hold an array of Items, rather than an instance of Item

    2. You instantiated an array, instead of an instance of your class

    3. item.toString() does nothing here

    Code:
    // ***************************************************************
    //   Shop.java
    //
    //   Uses the Item class to create items and add them to a shopping
    //   cart stored in an ArrayList.
    // ***************************************************************
    
    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class Shop
    {
        public static void main (String[] args)
        {
    
    	//Item item[];
    	Item item;
    	String itemName;
    	double itemPrice;
    	int quantity;
    	ArrayList cart = new ArrayList();
    
     	Scanner scan = new Scanner(System.in);
    
    	String keepShopping = "y";
    
    	do 
    	    {
    		System.out.print ("Enter the name of the item: "); 
    		itemName = scan.nextLine();
    
    		System.out.print ("Enter the unit price: ");
    		itemPrice = scan.nextDouble();
    
    		System.out.print ("Enter the quantity: ");
    		quantity = scan.nextInt();
    
    		// *** create a new item and add it to the cart
    		//item = new Item[1];
    		item = new Item(name,itemPrice, quantity);
    		System.out.print(item.toString());
    		
    		//item.toString();
    		cart.add(item);
    		
    		// *** print the contents of the cart object using println
    		System.out.print("Items in your shopping cart: " + cart.toString());
    		
    		System.out.println("");
    		System.out.print ("Continue shopping (y/n)? ");
    		keepShopping = scan.nextLine();
    	    }
    	while (keepShopping.equals("y"));
    
        }
    }
    Cultured in Aggression and Koding like a Warrior!!
    “Common sense is instinct. Enough of it is genius.” - George Bernard Shaw.
    "The significant problems we face cannot be solved by the same level of thinking that created them." - Albert Einstein

  3. #3
    Join Date
    Apr 2005
    Posts
    1,333
    Rep Power
    0

    Default

    Thanks for the reply, I finally got it to work.
    The fox was probably right - they could have been sour grapes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •