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.