This program is a GUI shopping menu with text fileds next to item descriptions. My question is about the add method in ShippingCart class. It is called every time an action occurse in a text field, from a GUI class not shown here, where quantity requests are entered. So when a selection of 5 stickers is made 5 ItemOrder objects are added to an array list called my_order_list. Changing an order from 5 to 3 sends 3 ItemOrder objects to the list. This new quantity has to replace the old one in the array list. ItemOrder can return a string representation of the order requests. I use this string to compare ItemOrders in the arraylist. If a new request's string matches one already in the list I over write it with the new one else I add the new request.
OK so here is my question. Is this line 100% A OK java goodness? if (the_order.getItem().equals(my_order_list.get(i).getItem()))
Do i need to override some thing or use some other method instead of equals? Am I comparing the right things( strings or hash codes)? Im not sure of the inner workigns of java that this may relate to so I dont know if the above line is correct.
There are two other GUI classes and the homework assignment pdf can befound here http://students.washington.edu/cmartin0/ , but the Code I have done for the assignment is below.
import java.util.ArrayList;
import java.util.List;
/**
* <code>ShoppingCart</code> holds a collection of <code>ItemOrder</code> objects
* in an array list. The list is used to calculate total cost and to add new
* purchases requests.
*
* @author Christopher Martin
* @version 1.0
*
*/
public class ShoppingCart
{
/**
* Constant used to compute the 10% discount.
*/
private static final double DISCOUNT = 0.9;
/**
* Array list for holding ItemOrder objects that contain order name and
* quantity.
*/
private List <ItemOrder> my_order_list;
/**
* A flag for determining if an overall discount should be applied to the
* final total.
*/
private boolean my_discount;
/**
* Create an empty array list to hold purchases requests in the form of
* ItmeOrder objects. There are no parameters on purpose.
*/
public ShoppingCart()
{
my_order_list = new ArrayList<ItemOrder>();
}
/**
* Goes through the array list and replaced old ItemOrders with the new one.
* If there is no previous entry an new one is added with out replacement.
*
* @param the_order A new request of purchase for an item.
*/
public void add(final ItemOrder the_order)
{
for (int i = 0; i < my_order_list.size(); i++)
{
//compare the string representation of Items in the list to the param Item.
if (the_order.getItem().equals(my_order_list.get(i).getItem()))
{
//replace and exit method
my_order_list.set(i, the_order);
return;
}
}
//add if not in the list already
my_order_list.add(the_order);
}
/**
* Turn on or off the discount to be applied to all items.
* @param the_discount Indicator to determine if discount should be applied.
*/
public void setDiscount(final boolean the_discount)
{
my_discount = the_discount;
}
/**
* Goes through the array list of ItemOrders calculating the total including
* a discount when applicable.
*
* @return total cost of all items based on their quantity selected
*/
public double getTotal()
{
double sum = 0.0;
for (ItemOrder temp_list : my_order_list)
{
sum += temp_list.getPrice();
}
if (my_discount)
{
sum *= DISCOUNT;
}
return sum;
}
}
/**
* When a valid number is entered to purchase an item a new ItemOrder object is
* created to hold the item being selected and the quantity being purchased.
*
* @author christopher martin
* @version 1.0
*/
public class ItemOrder
{
/**
* The reference to an <code>Item</code> object.
*/
private Item my_item;
/**
* The number of items being purchased.
*/
private int my_quanity;
/**
* Constructs <code>ItemOrder</code> object with <code>Item</code> reference
* and the amount of items being purchased.
*
* @param the_item Reference to an <code>Item</code> object.
* @param the_quantity The amount of items being purchased.
*/
public ItemOrder(final Item the_item, final int the_quantity)
{
my_item = the_item;
my_quanity = the_quantity;
}
/**
* Returns the total cost of the items being purchased including
* bulk discount.
*
* @return price for item including bulk discount if it applies.
*/
public double getPrice()
{
return my_item.priceFor(my_quanity);
}
/**
*
* @return Reference to current <code>Item</code>.
*/
public Item getItem()
{
return my_item;
}
}
/**
* Item class holds normal price for a product, product name, bulk price and
* bulk quantity requirement.
*
* @author christopher Martin
* @version 1.5
*
*/
public class Item
{
/**
* The name of an item.
*/
private String my_name;
/**
* The regular price of an item.
*/
private double my_price;
/**
* The amount of items need to qualify for bulk price.
*/
private int my_bulk_quantity;
/**
* The bulk discount for a bulk quantity of items.
*/
private double my_bulk_price;
/**
* Constructs an <code>Item</code> with specified name and price.
*
* @param the_name name of the item being purchased.
* @param the_price price of the item being purchased.
*/
public Item(final String the_name, final double the_price)
{
my_name = the_name;
my_price = the_price;
}
/**
* Constructs an <code>Item</code> with specified name single price,
* bulk amount and bulk price.
*
* @param the_name name of the item being purchased.
* @param the_price price of the item being purchased.
* @param the_bulk_quantity how many of the items need to be bought for bulk discount.
* @param the_bulk_price the discount price for buying in bulk.
*/
public Item(final String the_name, final double the_price, final int the_bulk_quantity,
final double the_bulk_price)
{
this(the_name, the_price);
my_bulk_quantity = the_bulk_quantity;
my_bulk_price = the_bulk_price;
}
/**
* Computes and returns the total cost of the items being purchased including
* bulk discount.
*
* @param the_quantity the number of the <code>Item</code> being bought
* @return the cost of items
*/
public double priceFor(final int the_quantity)
{
/**
* Temporary storage for holding the computed total cost for items.
*/
double total;
if ((my_bulk_price == 0.0) || (my_bulk_quantity == 0))
{
total = my_price * the_quantity;
}
else
{
// intentional truncations to get the amount qualifying for the bulk rate.
final int int_division_holder = (the_quantity / my_bulk_quantity);
total =
int_division_holder * my_bulk_price + (the_quantity % my_bulk_quantity) * my_price;
}
return total;
}
/**
* @return Item name and price including bulk quantity and price if applicable
* printed in currency format.
*/
public String toString()
{
final NumberFormat nf = NumberFormat.getCurrencyInstance();
final StringBuilder sb = new StringBuilder();
sb.append(my_name);
sb.append(", ");
sb.append(nf.format(my_price));
if ((my_bulk_price > 0.0) && (my_bulk_quantity > 0))
{
sb.append(" (");
sb.append(my_bulk_quantity);
sb.append(" for ");
sb.append(nf.format(my_bulk_price));
sb.append(")");
}
return sb.toString();
}
}