Results 1 to 4 of 4
Thread: Issue with calculating total
- 02-17-2013, 09:07 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 11
- Rep Power
- 0
[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:
Main class: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; } }
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...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()); } }Last edited by Modulus; 02-17-2013 at 10:59 PM.
- 02-17-2013, 09:29 PM #2
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
Re: Issue with calculating total
Hi Modulus,
At no time is the method setTotal() called so your total is not updated.
Regards.
- 02-17-2013, 09:57 PM #3
Member
- Join Date
- Feb 2013
- Posts
- 11
- Rep Power
- 0
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:
I hope that is what you were talking about...Java Code:{ this.itemNum = itemNum; this.itemName = itemName; this.itemPrice = itemPrice; this.itemInStock = itemInStock; setTotal(); }
Appreciate the help for the rookie mistake
- 02-17-2013, 10:09 PM #4
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
Similar Threads
-
Calculating Total?
By ethemartian in forum New To JavaReplies: 6Last Post: 02-21-2011, 04:09 AM -
Total column issue again
By ninjalord918 in forum AWT / SwingReplies: 39Last Post: 08-06-2010, 06:50 AM -
Issue With Finding Total Number of Blank Spaces in File
By Cod in forum New To JavaReplies: 2Last Post: 12-10-2009, 12:06 PM -
Total noob
By J_Walker in forum New To JavaReplies: 9Last Post: 04-24-2009, 03:10 AM -
total beginner needs little help
By asambasamba in forum New To JavaReplies: 1Last Post: 06-18-2008, 05:33 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
.

Bookmarks