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;
}
}
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;
}
}