aznishboy

Item

Mar 29th, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.53 KB | None | 0 0
  1. /**
  2.  *  Encapsulates and Inventory Item
  3.  *
  4.  * @author     G. Peck
  5.  * @created    July 18, 2002
  6.  */
  7.  
  8. public class Item implements Comparable
  9. {
  10.   private int myId;
  11.   private int myInv;
  12.  
  13.   /**
  14.    *  Constructor for the Item object
  15.    *
  16.    * @param  id   id value
  17.    * @param  inv  inventory value
  18.    */
  19.   public Item(int id, int inv)
  20.   {
  21.     myId = id;
  22.     myInv = inv;
  23.   }
  24.  
  25.   /**
  26.    *  Gets the id attribute of the Item object
  27.    *
  28.    * @return    The id value
  29.    */
  30.   public int getId()
  31.   {
  32.     return myId;
  33.   }
  34.  
  35.   /**
  36.    *  Gets the inv attribute of the Item object
  37.    *
  38.    * @return    The inv value
  39.    */
  40.   public int getInv()
  41.   {
  42.     return myInv;
  43.   }
  44.  
  45.   /**
  46.    *  Compares two Item objects by their Id (myId) fields
  47.    *
  48.    * @param  otherObject  Item object to compare to
  49.    * @return              positive int if myId > otherObject.myId
  50.    *                      0 if myId == otherObject.myId
  51.    *                      negative int if myId < otherObject.myId
  52.    */
  53.   public int compareTo(Object otherObject)
  54.   {
  55.     Item other = (Item) otherObject;
  56.  
  57.     return myId - other.myId;
  58.   }
  59.  
  60.   /**
  61.    *  Compares the Item to the specified object
  62.    *
  63.    * @param  otherObject  Item object to compare to
  64.    * @return              true if equal, false otherwise
  65.    */
  66.   public boolean equals(Object otherObject)
  67.   {
  68.     return this.compareTo(otherObject) == 0;
  69.   }
  70.  
  71.   public String toString()
  72.   {
  73.     return "Id=" + myId + ",Inv=" + myInv;
  74.   }
  75.   public int hashCode(){
  76.         return myId%599;
  77.   }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment