• Post Reply Bookmark Topic Watch Topic
  • New Topic

Having issues with calling a Sorting Method & creating a method to total the inventory value  RSS feed

 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Afternoon,

I am now working on the Inventory Part 2 program for my Java Programming class.
I am having issues and I am hoping to get help/guidance here.

The instructions are:
/**
* Modify the Inventory Program so the application can handle multiple items. Use an array to store the items.
* The output should display the information one product at a time, including the item number, the name of the product,
* the number of units in stock, the price of each unit, and the value of the inventory of that product.
* In addition, the output should display the value of the entire inventory.
*
* Create a method to calculate the value of the entire inventory.
* Create another method to sort the array items by the name of the product.
*/

I have the sorting method in the Inventory class (which was given by the instructor) but I can't figure out how to call it from the InventoryLauncher. I also need to create a method in the Inventory class that is called from the InventoryLauncher that adds together the inventoryValues for each inventory item. I am not sure how to do this since the array is in the InventoryLauncher and the value that needs to be added is one of the elements(?) in the array.

I hope this makes sense... I've been working on this all day and getting frustrated that I don't understand this...

Here is my code... Thanks in advance for any assistance.

Inventory.java

InventoryLauncher.java
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inventory - line 6 - itemNumber should be an integer not a double.
Inventory - line 8 - not necessary. inventoryValue is calculated on the fly.
Launcher - Line 7 - not necessary.
Launcher - Line 10 - should be "books" with a lower case 'b'. This is the Java convention.
Inventory - line 75 - probably should have been declared 'static'. Returning 'Inventory[]' seems pointless as the sorting is done in-place and will modify the content of 'theinventory'.
Inventory - line 26 & 36 - could have been implemented as a toString() method in Inventory and then called here with System.out.println( Books[counter] );
Calling sort at line 32 just involves calling the Sortinventory() (again, should start with lower case) method. If you change the method to static then you need the class name, dot (.), method name. You'll have to pass in your array of Inventory as an argument. You could assign the return value back to your 'Books' variable, but as I mentioned, that doesn't really do anything given the way the sort was written.

Edit:
'itemStock' should probably be an integer also.
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Inventory - line 6 - itemNumber should be an integer not a double.
Inventory - line 8 - not necessary. inventoryValue is calculated on the fly.
Launcher - Line 7 - not necessary.
Launcher - Line 10 - should be "books" with a lower case 'b'. This is the Java convention.
Inventory - line 75 - probably should have been declared 'static'. Returning 'Inventory[]' seems pointless as the sorting is done in-place and will modify the content of 'theinventory'.
Inventory - line 26 & 36 - could have been implemented as a toString() method in Inventory and then called here with System.out.println( Books[counter] );
Calling sort at line 32 just involves calling the Sortinventory() (again, should start with lower case) method. If you change the method to static then you need the class name, dot (.), method name. You'll have to pass in your array of Inventory as an argument. You could assign the return value back to your 'Books' variable, but as I mentioned, that doesn't really do anything given the way the sort was written.

Edit:
'itemStock' should probably be an integer also.


Thank you for the suggestions. I will give these a go and see how it goes.
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Inventory - line 6 - itemNumber should be an integer not a double.
Inventory - line 8 - not necessary. inventoryValue is calculated on the fly.
Launcher - Line 7 - not necessary.
Launcher - Line 10 - should be "books" with a lower case 'b'. This is the Java convention.
Inventory - line 75 - probably should have been declared 'static'. Returning 'Inventory[]' seems pointless as the sorting is done in-place and will modify the content of 'theinventory'.
Inventory - line 26 & 36 - could have been implemented as a toString() method in Inventory and then called here with System.out.println( Books[counter] );
Calling sort at line 32 just involves calling the Sortinventory() (again, should start with lower case) method. If you change the method to static then you need the class name, dot (.), method name. You'll have to pass in your array of Inventory as an argument. You could assign the return value back to your 'Books' variable, but as I mentioned, that doesn't really do anything given the way the sort was written.

Edit:
'itemStock' should probably be an integer also.


OK.

First thing I did was take out inventoryValue from my variables and tried to compile it and got the below errors. You stated it wasn't necessary because it's created on the fly. What do you mean that it's created on the fly?

C:\Users\Amie\Documents\IT215JavaProgramming\Week6\InventoryPart2_v2\Inventory.java:80: error: cannot find symbol
inventoryValue = itemStock * itemPrice;
^
symbol: variable inventoryValue
location: class Inventory
C:\Users\Amie\Documents\IT215JavaProgramming\Week6\InventoryPart2_v2\Inventory.java:81: error: cannot find symbol
return inventoryValue;
^
symbol: variable inventoryValue
location: class Inventory
2 errors


 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Inventory - line 6 - itemNumber should be an integer not a double.
Inventory - line 8 - not necessary. inventoryValue is calculated on the fly.
Launcher - Line 7 - not necessary.
Launcher - Line 10 - should be "books" with a lower case 'b'. This is the Java convention.
Inventory - line 75 - probably should have been declared 'static'. Returning 'Inventory[]' seems pointless as the sorting is done in-place and will modify the content of 'theinventory'.
Inventory - line 26 & 36 - could have been implemented as a toString() method in Inventory and then called here with System.out.println( Books[counter] );
Calling sort at line 32 just involves calling the Sortinventory() (again, should start with lower case) method. If you change the method to static then you need the class name, dot (.), method name. You'll have to pass in your array of Inventory as an argument. You could assign the return value back to your 'Books' variable, but as I mentioned, that doesn't really do anything given the way the sort was written.

Edit:
'itemStock' should probably be an integer also.


Second question I have is if I change itemNumber and itemStock to integers in my variables, do I also need to change them to integers through the rest of the code? such as in the constructor? I would assume yes.
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Second question I have is if I change itemNumber and itemStock to integers in my variables, do I also need to change them to integers through the rest of the code? such as in the constructor? I would assume yes.


Nevermind on this one. I changed them all to integers from doubles and the code runs fine. Thanks

Still working on the other suggestions...
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inventory - line 70 - just return stock * price. Don't store it.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amie Mac wrote:I changed them all to integers from doubles and the code runs fine.

Because monetary values usually contain a decimal portion, I would leave price as a double. You will have to make allowance for the fact that an integer multiplied by a double yields a double.
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Inventory - line 70 - just return stock * price. Don't store it.


Thanks. Got it.


So I made all the changes that you recommended, but I am still having issues with calculating the total inventory value. I have put some code in place, but it's not working. I think I am missing some sort of for loop... but I am not sure how to go about getting each inventoryValue out of the populated arrays to add them together.

Here is my updated code (with the messed up totalInventoryValue()

Inventory.java

InventoryLauncher.java
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Amie Mac wrote:I changed them all to integers from doubles and the code runs fine.

Because monetary values usually contain a decimal portion, I would leave price as a double. You will have to make allowance for the fact that an integer multiplied by a double yields a double.


Oh... yep. I kept that as a double.
Thanks
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your getItemStock() method returns a double; what should it return? Ditto for getItemNumber().
Inventory - line 95 - you are using %f in some places where you have an integer value.
Line 28 - you changed the sort method to be declared 'static', therefore to call it you shouldn't use an Inventory object (ie books[0]), instead use Inventory.sortInventory().
Line 102 - you need to pass in an Inventory[]. The Inventory class knows nothing about your array unless you pass it in. As you guessed, you'll need a loop to go through the array to sum up the inventory values.

 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For loop at line 41 no longer necessary, just use the value returned from totalInventoryValue().
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Your getItemStock() method returns a double; what should it return? Ditto for getItemNumber().
Inventory - line 95 - you are using %f in some places where you have an integer value.
Line 28 - you changed the sort method to be declared 'static', therefore to call it you shouldn't use an Inventory object (ie books[0]), instead use Inventory.sortInventory().
Line 102 - you need to pass in an Inventory[]. The Inventory class knows nothing about your array unless you pass it in. As you guessed, you'll need a loop to go through the array to sum up the inventory values.



- Changed the return values to int
- changed the %f to %i
Once I did this ^ then I was getting runtime illegal format conversion errors - changed them all back to doubles. Edit: I am not sure how the printf would get formatted with the ints instead of doubles.
- calling the sort method with Inventory.sortInventory()

- still working on the total inventory value method. will that method look like the sort method with the Inventory[]?
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amie Mac wrote:
Carey Brown wrote:Your getItemStock() method returns a double; what should it return? Ditto for getItemNumber().
Inventory - line 95 - you are using %f in some places where you have an integer value.
Line 28 - you changed the sort method to be declared 'static', therefore to call it you shouldn't use an Inventory object (ie books[0]), instead use Inventory.sortInventory().
Line 102 - you need to pass in an Inventory[]. The Inventory class knows nothing about your array unless you pass it in. As you guessed, you'll need a loop to go through the array to sum up the inventory values.



- Changed the return values to int
- changed the %f to %i
Once I did this ^ then I was getting runtime illegal format conversion errors - changed them all back to doubles.
- calling the sort method with Inventory.sortInventory()

- still working on the total inventory value method. will that method look like the sort method with the Inventory[]?

Integers are formatted with %d.
Inventory.sortInventory(books);
will that method look like the sort method with the Inventory[]? -- yes
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Amie Mac wrote:
Carey Brown wrote:Your getItemStock() method returns a double; what should it return? Ditto for getItemNumber().
Inventory - line 95 - you are using %f in some places where you have an integer value.
Line 28 - you changed the sort method to be declared 'static', therefore to call it you shouldn't use an Inventory object (ie books[0]), instead use Inventory.sortInventory().
Line 102 - you need to pass in an Inventory[]. The Inventory class knows nothing about your array unless you pass it in. As you guessed, you'll need a loop to go through the array to sum up the inventory values.



- Changed the return values to int
- changed the %f to %i
Once I did this ^ then I was getting runtime illegal format conversion errors - changed them all back to doubles.
- calling the sort method with Inventory.sortInventory()

- still working on the total inventory value method. will that method look like the sort method with the Inventory[]?

Integers are formatted with %d.
Inventory.sortInventory(books);
will that method look like the sort method with the Inventory[]? -- yes


Thanks for all the help! I will implement these changes and work on the total inventory value method more tomorrow. For now, I am very tired and need some sleep.

And, yes, I had: Inventory.sortInventory(books); I just forgot to type the books in there.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good night )
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ugh... I feel like I am very close... am I just missing a string conversion? Like a String toString()? If so, I am not sure how are where to put this for the output on the Total Inventory Value.

In the output I get:
Total Inventory Value: [LInventory;@5f048099

Here is my updated code:
Inventory.java

InventoryLauncher.java
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change

to

And forget the loop in Launch.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Change

to

And forget the loop in Launch.


SORRY, FORGET THAT.

 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Replace
System.out.println("Total Inventory Value: " + theInventory);
With
System.out.println("Total Inventory Value: " + total );
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Replace
System.out.println("Total Inventory Value: " + theInventory);
With
System.out.println("Total Inventory Value: " + total );


YAY! It works!

I also found that I calculated the wrong value and had to replace getItemPrice() with inventoryValue()

Thanks so much for your assistance! It is greatly appreciated!

Now I get to move on to Inventory Program Part 3, which is also due on Sunday. I may be back on the Ranch for assistance with that one.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!