PDA

View Full Version : Arraylists



JPHamlett
01-20-2011, 03:50 PM
I know how to do arrays, but I am required to do an arraylist for a programing class.



Here's the assignment, http://www.district196.org/evhs/academics/apcomputerscience/labs/ch6/AShoppingCartUsingtheArraylistClass1.htm


I can't figure out how to do this part



Modify the program to correct these problems by replacing the print statement with a loop that does the following:


a.gets each item from the cart and prints the item


b.computes the total price of the items in the cart (you need to use the getPrice and getQuantity methods of the Item class). The total price should be printed after the loop.


I just can't figure out how to get each item individually.


My code:


import java.util.ArrayList;
import cs1.Keyboard;

public class Main
{
public static void main (String[] args)
{
ArrayList cart = new ArrayList();

Item item;
String itemName;
double itemPrice;
int quantity;

String keepShopping = "y";


do
{

System.out.print ("Enter the name of the item: ");
itemName = Keyboard.readString();

System.out.print ("Enter the unit price: ");
itemPrice = Keyboard.readDouble();

System.out.print ("Enter the quantity: ");
quantity = Keyboard.readInt();

item = new Item(itemName, itemPrice, quantity);

cart.add(item);


for(int i=0; i < cart.size(); i++){

}


System.out.print ("Continue shopping (y/n)? ");
keepShopping = Keyboard.readString();
}
while (keepShopping.equals("y"));

}
}


The Item class:



/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package shoppingwitharraylist;

/**
*
* @author 729484
*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
// ************************************************** *************
// 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;
}
}

nielsie95
01-20-2011, 03:55 PM
cart.get(i) ?

Nava2
01-20-2011, 03:59 PM
Extend the ArrayList class and add methods for Items?

Sorry, but this seems fairly straight forward.

JPHamlett
01-20-2011, 04:03 PM
cart.get(i) ?



I need to be able to do something similar to this




for (int i = 0; i < cart.size(); i++){
Item carti = cart.get(i);
System.out.println(carti.getName());
}


I can't get that to work using cart.get(i) .

nielsie95
01-20-2011, 04:09 PM
You'll have to cast it to an Item class first.

(Item) cart.get(i)

or declare cart as ArrayList<Item>.

pyroryan
01-20-2011, 08:03 PM
Item[] itemArray = (Item[]) cart.toArray();
for (int i = 0; i < cart.size(); i++) {
Item carti = (Item) cart.get(i);
System.out.println(carti.getName());
}


Two options. Both shown above.
1) You can convert your ArrayList to an Array. Then since you already know arrays, you can use your strong point!
2) Basically what nielsie said.

Method
01-20-2011, 11:55 PM
Using the list's iterator would be appropriate as well.

n3ss3s
01-23-2011, 07:16 PM
Arraylist<Item> items = new ArrayList<Item>();

items.get(i) will work den