Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: help with setters and getters

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default help with setters and getters

    I am trying to make a program for my java course and it is supposed to hold basic inventory items for an office supply store. My instance variables are:
    String itemNumber;
    integer quantityOnHand;
    integer reorderLevel;
    double cost;
    integer numberOnOrder;
    String description;

    The methods are:
    A constructor that sets quantityOnHand and numberOnOrder to 0, and takes in data for the rest.
    A constructor that takes and sets all the data.
    Typical getters for all data.
    Setters for reorderLevel and description.
    A Special setter for itemNumber that guarantees the data is always stored in uppercase. Should be called from the constructors.
    Method called orderItems that takes in an integer variable numOrdered and updates the number of the items that are on order.
    Method called receiveOrderedItems that takes in an integer variable numberReceived and updates the number of the items on hand and the number still on order.
    Method called sellItems that takes in an integer variable numberSold and updates the number of items on hand.
    Method called getCurrentInventoryValue that returns the total cost of the item that is on hand.
    An equals method that compares 2 instances of the InventoryItem class. It will return true if the 2 instances are the same, false otherwaise.
    A toString method to display data about the instance of InventoryItem class. Format should match the following example:
    Item Number: D-145-BLK
    Description: Black Case-it Large Capacity 3-Inch Zipper Binder
    Cost: $19.97
    Quantity Available: 123
    Re-order level: 50
    Number on order: 0

    This is what I have so far

    public class InventoryItem
    {

    private String itemNumber;
    private int quantityOnHand;
    private int reorderLevel;
    private double cost;
    private int numberOnOrder;
    private String description;

    // Constructor that sets quantityOnHand and numberOnOrder to zero, and takes in data for the rest.
    public InventoryItem(int quantityOnHand, int numberOnOrder)
    {
    this.quantityOnHand = 0;
    this.numberOnOrder = 0;
    }
    // Constructor that takes and sets all the data
    public InventoryItem(String itemNumber, String description, double cost, int quantityOnHand, int reorderLevel, int numberOnOrder)
    {
    this.itemNumber = itemNumber;
    this.description = description;
    this.cost = cost;
    this.quantityOnHand = quantityOnHand;
    this.reorderLevel = reorderLevel;
    this.numberOnOrder = numberOnOrder;
    }
    // Special setter for itemNumber, data is always stored in upper-case.
    public void setItemNumber(String itemNumber)
    {
    itemNumber = itemNumber.toUpperCase();
    }
    // Getter for itemNumber.
    public String getItemNumber()
    {
    return itemNumber;
    }
    // Setter for reorderLevel.
    public void setReorderLevel(int reorderLevel)
    {
    this.reorderLevel = reorderLevel;
    }
    // Getter for reorderLevel.
    public int getReorderLevel()
    {
    return reorderLevel;
    }
    // Setter for description.
    public void setDescription(String description)
    {
    this.description = description;
    }
    // Getter for description.
    public String getDescription()
    {
    return description;
    }
    // Getter for quantityOnHand.
    public int getQuantityOnHand()
    {
    return quantityOnHand;
    }
    // Getter for cost.
    public double getCost()
    {
    return cost;
    }
    // Getter for numberOnOrder.
    public int getNumberOnOrder()
    {
    return numberOnOrder;
    }
    // Method that takes in numOrdered and updates that number of items that are on order.
    public void orderItems(int numOrdered)
    {
    numOrdered = numberOnOrder + numOrdered;
    }
    // Method that takes in numberReceived and updates that number of items on hand and number that is still on order.
    public void receiveOrderedItems(int numberReceived)
    {
    quantityOnHand = quantityOnHand - numberReceived;
    numberOnOrder = numberOnOrder + numberReceived;
    }
    // Method that takes in numberSold and updates that the number of items on hand.
    public void sellItems(int numberSold)
    {
    quantityOnHand = quantityOnHand - numberSold;
    }
    // Method that returns the total cost of the item that is on hand.
    public void getCurrentInventoryValue()
    {
    cost = cost * numberOnOrder;
    }
    // toString method that displays the data about the instance of InventoryItem class.
    public String toString()
    {
    return "Item Number: " + itemNumber + "\nDescription: " + description + "\nCost: " + cost +
    "\nQuantity Available: " + quantityOnHand + "\nRe-order level: " + reorderLevel +
    "\nNumber on order: " + numberOnOrder;
    }

    }

    In my application class I have

    public class InventoryDriver
    {

    public static void main(String[] args)
    {
    InventoryItem item1 = new InventoryItem(0,0);

    InventoryItem item2 = new InventoryItem("d-145-blk", "Black Case-it Large Capacity 3-Inch Zipper Binder", 19.97, 123, 50, 0);

    System.out.println(item1);
    System.out.println();
    System.out.println(item2);

    }
    }

    when ran, I get :

    Item Number: null
    Description: null
    Cost: 0.0
    Quantity Available: 0
    Re-order level: 0
    Number on order: 0

    Item Number: d-145-blk
    Description: Black Case-it Large Capacity 3-Inch Zipper Binder
    Cost: 19.97
    Quantity Available: 123
    Re-order level: 50
    Number on order: 0

    How do I make an equals method? and why doesn't my order item number show up in upper-case even though I set it?
    Can someone help me please?

    Thanks.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: help with setters and getters

    Welcome to the forum! Please read the Announcement topic at the top of the sub-forum to learn how to post code in code or highlight tags along with other useful info for new comers. It's very hard to read the code you've posted in its raw form.

    An equals method will compare any of the items necessary to determine if two objects are equal. That may involve comparing every instance variable of the two objects, or it may be some subset of the available instance variables. I didn't read the entire spec, so maybe it tells you in there what determines equality. If not, I would assume that all instance variables of the two objects must be equal in order for the two objects to be equal.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    takira (October 27th, 2013)

  4. #3
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: help with setters and getters

    How to code the equals method is very class specific. However, such a method already exists in the Object class (which is the superclass, directly or indirectly, of all java classes (well, all but Object and the interfaces. )


     
    @Override
     
    public boolean equals(Object yourClassObject)
    {
     
      if (yourClassObject instanceof YourClass)
      {
     
          YourClass obj = (YourClass) yourClassObject;
     
         // how you determine whether this YourClass object is equal to the YourClass object obj is up to you   
     
     
       }
     
       else
         return false;  // no use comparing apples and oranges 
     
     
     
    }



    ------------------ Update -------------------------

    I see that your item1 object is using the two argument constructor. That one does not set the value of itemNumber. Hence, it will be null.
    Last edited by GoodbyeWorld; October 26th, 2013 at 04:29 PM. Reason: Found the reason it's null

  5. The Following User Says Thank You to GoodbyeWorld For This Useful Post:

    takira (October 27th, 2013)

Similar Threads

  1. Replies: 7
    Last Post: September 16th, 2013, 07:47 PM
  2. Can anyone explain the Getters and setters in Java
    By sivasubramaniam in forum Java Theory & Questions
    Replies: 4
    Last Post: July 30th, 2013, 06:47 AM
  3. Encapsulation (getters and setters) Tips?
    By Robertgif in forum Java Theory & Questions
    Replies: 4
    Last Post: March 2nd, 2013, 06:36 AM
  4. Replies: 6
    Last Post: October 31st, 2012, 06:16 AM
  5. Problems with input/output and getters/setters
    By robbiep551 in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: January 5th, 2012, 11:44 PM

Tags for this Thread