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

If you override equals() on a class, you MUST (even though the compiler doesn't enforce it) also override hashCode() on that same class.

You may never notice if you don't, but under specific conditions (most especially when using instances of the class inside a HashMap or Hashtable) it is needed for correct operation.

What I'd personally do is implement Comparable on Item, and maybe on ItemOrder as well.
That way you can easily sort your Lists, and do some other interesting things.
See the JavaDoc for Comparable for more information.

What do I do when I override HashCode()? I know when I override toString() I give it some text.

I haven't worked with Java collections before. I know how to use ArrayList and array but thats about it.

We talked about overriding equals in class so I had problem doing it the right way. Bellow is the updated code.

import java.text.NumberFormat;


/**
 * The Item class is used to generate the text on the GUI. The GUI passes Item
 * objects to ItemOrder class
 * Item class holds normal price for a product, product name, bulk price and 
 * bulk quantity requirement. The Item class also calculates cost for an item.
 * 
 * @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 a single item or multipule items at a bulk discount rate.
   */
  public double priceFor(final int the_quantity)
  {
    double total = 0.0;

    /*
    test to see if the item has no bulk info. this means the two param 
    constructor was called leaving the my_bulk_price and my_bulk_quantity at 
    default0
    */
    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.
      // this was done to get rid of a FindBugs warning.
      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;
  }


  /**
   * Give a string representation of the Item object.
   * 
   * @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();
  }
  
  /**
   * {@inheritDoc}
   */
  public boolean equals(final Object some_other)
  {
    boolean result = false;
    
    if (this == some_other)
    {
      result = true;
    }
    else if ((some_other != null) && (some_other.getClass() == getClass()))
    {
      final Item some_item = (Item) some_other;
     
      //compare all fields in item when comparing two objects. this will be true
      //only when all of the fields are the same.
      result = 
          ((some_item.my_bulk_price == my_bulk_price) && 
          (some_item.my_bulk_quantity == my_bulk_quantity) && 
          (some_item.my_price == my_price) && 
          (some_item.my_name.equals(my_name)));
    }
    
    return result;
  }
  
  /**
   * {@inheritDoc}
   */  
  public int hashCode()
  {
    return toString().hashCode();
  }
}

This check here

if ((some_other != null) && (some_other.getClass() == getClass()))

can be done with the instanceof operator

if (some_other instanceof Item)

. The null check is unnecessary because instanceof will return false if "some_other" is null. Note that you won't be able to use instanceof with generics though (i.e. instanceof List<T>) due to type erasure, but that isn't a concern for your Item class.

can u send me the coding, to find the rank of five students using arraylist in java

Comments
homework kiddo, thread hijacker, zombie master
For the annoying PMs and for hijacking an old thread

Nope. Things don't work that way around here.

Start a new thread, ask specific questions, and post what you have so far. If you haven't made any effort on it, don't expect much help.

This question has already been answered. Start a new discussion instead.