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 4 of 4

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

     
    public class InventoryDriver
    {
     
    	public static void main(String[] args)
    	{
    	InventoryItem item1 = new InventoryItem("d-145-blk", "Black Case-it Large Capacity 3-Inch Zipper Binder", 5.50, 65);
     
    	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);
    	item1.equals();
    	System.out.println();
    	System.out.println(item2);
    	item2.equals();
     
     
    	System.out.println("\n****Display Inventory for item2 after 3 items are purchased");
    	item2.receiveOrderedItems(3);
    	System.out.println("Quantity Available: " + item2.getQuantityOnHand());
    	System.out.println("Number on order: " + item2.getNumberOnOrder());
    	item2.getCurrentInventoryValue();
    	System.out.println("Cost: " + item2.getCost());
     
    	System.out.println("\n****Display Inventory for item1 after item number is changed and 40 items are purchased");
    	item1.receiveOrderedItems(40);
    	item1.setItemNumber("k-456-kdn");
    	System.out.println("Item number: " + item1.getItemNumber());
    	item1.getItemNumber();
    	System.out.println("Quantity Available: " + item1.getQuantityOnHand());
    	System.out.println("Number on order: " + item1.getNumberOnOrder());
    	item1.getCurrentInventoryValue();
    	System.out.println("Cost: " + item1.getCost());
     
    	System.out.println("\n****Display inventory for item2 after description is changed and 2 more items are purchased");
     
    	item2.setDescription("Purple Purse");
    	System.out.println("Description: " + item2.getDescription());
    	item2.receiveOrderedItems(2);
    	System.out.println("Quantity Available: " + item2.getQuantityOnHand());
    	System.out.println("Number on order: " + item2.getNumberOnOrder());
    	item2.getCurrentInventoryValue();
    	System.out.println("Cost: " + item2.getCost());

    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(String itemNumber, String description, double cost, int reorderLevel)
    	{
    		this.itemNumber = itemNumber.toUpperCase();
    		this.description = description;
    		this.cost = cost;
    		this.reorderLevel = reorderLevel;
     
    	}
    //	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.toUpperCase();
    		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)
    	{
    		this.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)
    	{
    		this.quantityOnHand = quantityOnHand - numberReceived;
    		this.numberOnOrder = numberOnOrder + numberReceived;
    	}
    //	Method that takes in numberSold and updates that the number of items on hand.
    	public void sellItems(int numberSold)
    	{
    		this.quantityOnHand = quantityOnHand - numberSold;
    	}
    //	Method that returns the total cost of the item that is on hand.
    	public void getCurrentInventoryValue()
    	{
    		this.cost = cost * numberOnOrder;
    	}
    //	Equal method comparing two instances
    	public void equals()
    	{
     
    		if (itemNumber.equalsIgnoreCase(itemNumber))
     
    		System.out.println("item number's are the same.");
     
    		else 
    		System.out.println("item number's are not the same.");
     
    		if (description.equalsIgnoreCase(description))
     
    		System.out.println("item description's are the same.");
     
    		else
    		System.out.println("item description's are not the same.");
     
    		if (itemNumber.equalsIgnoreCase(description))
    		System.out.println("item description and description are not the same.");
     
    	}
    //	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;
    	}
     
    }

    When I run this program, my cost variable displays the correct cost the first time it is ran, but every time after that I get a price that is not correct. Why isn't the variable multiplying like I want it to? Help, please.
    Thanks.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,154
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: help with setters and getters

    Can you copy the console from when you execute the program and post it here. Add some comments showing what is wrong with the output.

    Why isn't the variable multiplying
    Where is cost being used? When does it get the correct value and when the wrong value?
    Try debugging the code by adding some println statements that print out the value of cost every time it is changed. The print out will show you where it is getting the wrong value.

    //	Method that returns the total cost of the item that is on hand.
    	public void getCurrentInventoryValue()
    The return type of void means that the method does NOT RETURN anything.
    Either the name is wrong (get implies the method will return something) and the name should be changed
    or the return type should be changed and the method should return something as the comments say.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: help with setters and getters

    Quote Originally Posted by Norm View Post
    Can you copy the console from when you execute the program and post it here. Add some comments showing what is wrong with the output.

    Where is cost being used? When does it get the correct value and when the wrong value?
    Try debugging the code by adding some println statements that print out the value of cost every time it is changed. The print out will show you where it is getting the wrong value.

    //	Method that returns the total cost of the item that is on hand.
    	public void getCurrentInventoryValue()
    The return type of void means that the method does NOT RETURN anything.
    Either the name is wrong (get implies the method will return something) and the name should be changed
    or the return type should be changed and the method should return something as the comments say.
    In my post I entered the question wrong, what I am trying to ask is why when I getCost from my driver, instead of the currentInventoryValue resetting to zero, it instead does numberOnOrder * numberOnOrder * cost.

     
    public static void main(String[] args)
    	{
    	InventoryItem item1 = new InventoryItem("d-145-blk", "Black Case-it Large Capacity 3-Inch Zipper Binder", 5.50, 65);
     
    	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);
    	item1.equals();
    	System.out.println();
    	System.out.println(item2);
    	item2.equals();
     
     
    	System.out.println("\n****Display Inventory for item2 after 3 items are purchased");
    	item2.receiveOrderedItems(3);
    	System.out.println("Quantity Available: " + item2.getQuantityOnHand());
    	System.out.println("Number on order: " + item2.getNumberOnOrder());
    	item2.getCurrentInventoryValue();
    	System.out.println("Cost: " + item2.getCost());
     
    	System.out.println("\n****Display Inventory for item1 after item number is changed and 40 items are purchased");
    	item1.receiveOrderedItems(40);
    	item1.setItemNumber("k-456-kdn");
    	System.out.println("Item number: " + item1.getItemNumber());
    	item1.getItemNumber();
    	System.out.println("Quantity Available: " + item1.getQuantityOnHand());
    	System.out.println("Number on order: " + item1.getNumberOnOrder());
    	item1.getCurrentInventoryValue();
    	System.out.println("Cost: " + item1.getCost());
     
    	System.out.println("\n****Display inventory for item2 after description is changed and 2 more items are purchased");
     
    	item2.setDescription("Purple Purse");
    	System.out.println("Description: " + item2.getDescription());
    	item2.receiveOrderedItems(2);
    	System.out.println("Quantity Available: " + item2.getQuantityOnHand());
    	System.out.println("Number on order: " + item2.getNumberOnOrder());
    	item2.getCurrentInventoryValue();
    	System.out.println("Cost: " + item2.getCost());
     
     
     
     
     
    	}
    }
    when ran I get this as a result:

    Item Number: D-145-BLK
    Description: Black Case-it Large Capacity 3-Inch Zipper Binder
    Cost: 5.5
    Quantity Available: 0
    Re-order level: 65
    Number on order: 0
    item number's are the same.
    item description's are the same.

    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
    item number's are the same.
    item description's are the same.

    ****Display Inventory for item2 after 3 items are purchased
    Quantity Available: 120
    Number on order: 3
    Cost: 59.91

    ****Display Inventory for item1 after item number is changed and 40 items are purchased
    Item number: K-456-KDN
    Quantity Available: -40
    Number on order: 40
    Cost: 220.0

    ****Display inventory for item2 after description is changed and 2 more items are purchased
    Description: Purple Purse
    Quantity Available: 118
    Number on order: 5
    Cost: 299.54999999999995

    What I want to show up is:

    ****Display inventory for item2 after description is changed and 2 more items are purchased
    Description: Purple Purse
    Quantity Available: 118
    Number on order: 5
    Cost: 99.85

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,154
    Thanks
    49
    Thanked 2,158 Times in 2,131 Posts

    Default Re: help with setters and getters

    instead of the currentInventoryValue resetting to zero, it instead does numberOnOrder * numberOnOrder * cost.
    If you know what the problem is, can you explain what problems you are having fixing that problem?

    Did you try this:
    Try debugging the code by adding some println statements that print out the value of cost every time it is changed. The print out will show you where it is getting the wrong value.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. help with setters and getters
    By takira in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 26th, 2013, 04:10 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, 05: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, 10:44 PM

Tags for this Thread