Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Help with ArrayList, and toString method in the ArrayList class.

807603Nov 8 2007 — edited Nov 9 2007
I'm implementing a shopping cart using the ArrayList class. A file Item.java I've created contains the definition of a class named Item that models an item one would purchase. An item has name, price, and quantity. I also have a file Shop.java in which I have declared and instantiated a variable cart to be an empty ArrayList that can hold Item objects.

I am having an extremely difficult time with the Do loop you will see in the below code. I need to add an item to the cart and print the contents in the Item object in the following order: item, unit price, quantity, and total with tab as a separator. I know in order to get the total price, I need to use getPrice and getQuantity methods in my Item class I've created.

I know part of why my loop isn't working is because I need to use the toString method in the ArrayList class to add an item to the cart and print contents in the Item object.

Someone please help.
// ***************************************************************
//   Shop.java
//
//   Uses the Item class to create items and add them to a shopping
//   cart stored in an ArrayList.
// ***************************************************************

// imports ArrayList
import java.util.ArrayList;
import java.text.NumberFormat;
import java.util.Scanner;

public class Shop {
	public static void main(String[] args) {
		
		//Declare and instantiate a variable cart to be an empty ArrayList that can hold Item objects
                ArrayList cart = new ArrayList();
		
		Item item;
		String itemName;
		double itemPrice;
		int quantity;
		double totalPrice;

		System.out.println("Welcome to Shopper's Paradise");
		System.out.println();

		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
                        cart.add ("item" + itemName + itemPrice + quantity);

			totalPrice = 0;
			System.out.println("\nCurrent Cart");
			
			// *** print the contents of the cart object
                        System.out.println ("The contents of the cart are: " + cart.get(0));

			// *** print the total price of the cart
                        System.out.println ("Total " + getPrice * getQuantity);

			System.out.println();
			System.out.print("Continue shopping (y/n)? ");
			scan.nextLine();
			keepShopping = scan.nextLine();
		} while (keepShopping.equals("y"));

	}//end of main method
}//end of Shop class
//***************************************************************
//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 item;
		if (name.length() >= 8)
			item = name + "\t";
		else
			item = name + "\t\t";
		return (item + "   " + fmt.format(price) + "\t    " + quantity 
				+ "\t\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;
	}
}//end of class Item  
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 7 2007
Added on Nov 8 2007
17 comments
2,538 views