Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Ronin

Thread: Issue with calculating total

  1. #1
    Modulus is offline Member
    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0

    Default [SOLVED] Issue with calculating total

    Hello, I am on the first phase of making this inventory program for school and everything seems to be working ok, except when it prints the total of the inventory. This is not expected to handle user-input at this time so I just created an object to test it out. All of the lines print as expected, but when it gets to the last line it reads "The total value of the inventory is: $0.00" when it should read $1999.80. I assume there is something I did incorrectly on the getTotal() and/or setTotal() methods. Here is my code:

    Java Code:
    public class InventoryProduct 
    {
    	private int itemNum;
    	private String itemName;
    	private double itemPrice;
    	private int itemInStock;
    	private double inventoryTotal;
    	
    	public InventoryProduct(int itemNum, String itemName, 
    			double itemPrice, int itemInStock)
    	{
    		this.itemNum = itemNum;
    		this.itemName = itemName;
    		this.itemPrice = itemPrice;
    		this.itemInStock = itemInStock;
    	}
    	
    	public int getNum()
    	{
    		return itemNum;
    	}
    	
    	public String getName()
    	{
    		return itemName;
    	}
    	
    	public double getPrice()
    	{
    		return itemPrice;
    	}
    	
    	public int getStock()
    	{
    		return itemInStock;
    	}
    	
    	public void setTotal()
    	{
    		inventoryTotal = itemPrice * itemInStock;
    	}
    	
    	public double getTotal()
    	{
    		return inventoryTotal;
    	}
    }
    Main class:

    Java Code:
    public class Inventory 
    {	
    	public static void main (String[] args)
    	{
    		
    		System.out.println("Welcome! \nThis program will keep" +
    				" track of software programs in stock.\n");
    		
    		InventoryProduct myInventory = new InventoryProduct(1,
    				"FL Studio", 99.99, 20);
    		
    		System.out.printf("Product ID number: %d%n", myInventory.getNum());
    		System.out.printf("Product name: %s%n", myInventory.getName());
    		System.out.printf("The item's price is: $%.2f%n", myInventory.getPrice());
    		System.out.printf("Amount in stock: %d%n%n", myInventory.getStock());
    		
    		System.out.printf("The total value of inventory is: $%.2f", myInventory.getTotal());
    	}
    }
    btw, I apologize for posting full code in a previous response I made. I have reviewed the forum rules and will not do it again...
    Last edited by Modulus; 02-17-2013 at 10:59 PM.

  2. #2
    Ronin is online now Senior Member
    Join Date
    Oct 2010
    Posts
    384
    Rep Power
    5

    Default Re: Issue with calculating total

    Hi Modulus,

    At no time is the method setTotal() called so your total is not updated.

    Regards.

  3. #3
    Modulus is offline Member
    Join Date
    Feb 2013
    Posts
    11
    Rep Power
    0

    Default Re: Issue with calculating total

    Ok, thank you Ronin!

    It took me a minute to figure it out and I have been trying for HOURS. After I added it here it works:

    Java Code:
    {
    	this.itemNum = itemNum;
    	this.itemName = itemName;
    	this.itemPrice = itemPrice;
    	this.itemInStock = itemInStock;
    	setTotal();
    }
    I hope that is what you were talking about...

    Appreciate the help for the rookie mistake

  4. #4
    Ronin is online now Senior Member
    Join Date
    Oct 2010
    Posts
    384
    Rep Power
    5

    Default Re: Issue with calculating total

    You're welcome. That was exactly what I was refering to.
    We all make mistakes from time to time, even rookie ones .
    Modulus likes this.

Similar Threads

  1. Calculating Total?
    By ethemartian in forum New To Java
    Replies: 6
    Last Post: 02-21-2011, 04:09 AM
  2. Total column issue again
    By ninjalord918 in forum AWT / Swing
    Replies: 39
    Last Post: 08-06-2010, 06:50 AM
  3. Replies: 2
    Last Post: 12-10-2009, 12:06 PM
  4. Total noob
    By J_Walker in forum New To Java
    Replies: 9
    Last Post: 04-24-2009, 03:10 AM
  5. total beginner needs little help
    By asambasamba in forum New To Java
    Replies: 1
    Last Post: 06-18-2008, 05:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •