• Post Reply Bookmark Topic Watch Topic
  • New Topic

JLabel - issues displaying the total Inventory value  RSS feed

 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy ~

I am working on my Inventory Program part 4, which is to add a GUI that displays all the inventory information. I have the GUI working, but the only thing that is not working is the Total value of books in stock JLabel. It is calling a method from the Inventory class and it displays on the GUI but is showing LBooks;@16c0663d. and it still displays the value in the console.

Do I need to create a for loop in the InventoryLauncher class instead? I am not supposed to edit the Book or Inventory classes at all. Just the InventoryLauncher.
We are to use JLabel with FlowLayout for the GUI.

Here's the code. Any help is appreciated.

InventoryLauncher.java


Inventory.java


Books.java
 
Claude Moore
Ranch Hand
Posts: 862
8
IBM DB2 Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Method totalInventoryValue returns an array of inventory, not the sum over items. You assigned its retuned value to labelText variable, that's a string, so you get default object tostring() method output.
 
Amie Mac
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Claude Moore wrote:Method totalInventoryValue returns an array of inventory, not the sum over items. You assigned its retuned value to labelText variable, that's a string, so you get default object tostring() method output.


Yes, I figured it was outputting the string format... but I am still unsure of how to fix it. I am new to java programming... and am trying to wrap my head around all these concepts...
 
Campbell Ritchie
Sheriff
Posts: 53779
128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a Stream. Only works in Java8.

  • Start by creating a Stream; probably easiest with the static overloaded stream method of the Arrays class.
  • That returns an IntStream and IntStream has a sum() method.

  • int i = Arrays.stream(myIntArray).sum()
     
    Campbell Ritchie
    Sheriff
    Posts: 53779
    128
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A few minutes ago, Stephan van Hulst wrote:… accept that arrays and collections are not compatible, and to avoid accommodating the use of arrays.
    It was on this thread.
    See if you can't change the array to a List<Integer>.


    You would need the mapToInt() method to make sure the compiler knows you are dealing with Integers. The i -> i.intValue() bit is because you know you are dealing with Integers and you know they have an intValue() method. There are probably much neater ways to do that conversion.
     
    Amie Mac
    Ranch Hand
    Posts: 55
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:Use a Stream. Only works in Java8.

  • Start by creating a Stream; probably easiest with the static overloaded stream method of the Arrays class.
  • That returns an IntStream and IntStream has a sum() method.

  • int i = Arrays.stream(myIntArray).sum()


    hmmm... the textbook we have covers JavaSE7... I am not sure that this would be accepted by the instructor since it's not covered in the book???

    Are there any other options with Java7? do I need to create a new method in the InventoryLauncher for the Total Inventory Value? Besides it printing to the console as well, why am I not able to use the method that I have in the Inventory class? Is there a way to convert it back to double from string? I tried to do this with the decimal format variable, but that didn't work either.
     
    Amie Mac
    Ranch Hand
    Posts: 55
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:
    A few minutes ago, Stephan van Hulst wrote:… accept that arrays and collections are not compatible, and to avoid accommodating the use of arrays.
    It was on this thread.
    See if you can't change the array to a List<Integer>.


    You would need the mapToInt() method to make sure the compiler knows you are dealing with Integers. The i -> i.intValue() bit is because you know you are dealing with Integers and you know they have an intValue() method. There are probably much neater ways to do that conversion.


    Since this is a cost value, wouldn't I need to be dealing with doubles or floats? and not Integers?
     
    Campbell Ritchie
    Sheriff
    Posts: 53779
    128
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Amie Mac wrote: . . . Since this is a cost value, wouldn't I need to be dealing with doubles or floats? . . .
    Don't use doubles for money. Don't use floats for anything, unless some other code requires floats. Look at this old post and the two links therein for what you should use for money. Be sure to read the whole of the threads for both those links.

    As an alternative, denominate the money in pence, cents, centimes, or similar and use integer arithmetic.
     
    Campbell Ritchie
    Sheriff
    Posts: 53779
    128
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you have to write in Java7, you use a loopI trust we shan't see that sort of code in your GUI classes. That is application code which should be in different classes. You should have the application working before you try putting a GUI atop it. You must use the = operator because BigDecimal is immutable.
     
    Amie Mac
    Ranch Hand
    Posts: 55
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:I trust we shan't see that sort of code in your GUI classes. That is application code which should be in different classes. You should have the application working before you try putting a GUI atop it. You must use the = operator because BigDecimal is immutable.


    So what you are saying is that BigDecimal is what I should have used in my Inventory class to calculate the total inventory value?
     
    Campbell Ritchie
    Sheriff
    Posts: 53779
    128
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If the value is in money, yes.

    Read the old threads which show how BigDecimals work and a few things which can go wrong. Don't pass a double to a BigDecimal constructor.
     
    Amie Mac
    Ranch Hand
    Posts: 55
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:If the value is in money, yes.

    Read the old threads which show how BigDecimals work and a few things which can go wrong. Don't pass a double to a BigDecimal constructor.


    OK. I will have to read up on this and try it out later on tonight. Work is getting in the way of my programming class right now. ;)

    Thanks
     
    Amie Mac
    Ranch Hand
    Posts: 55
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I guess I still don't understand. I tried to change my total inventory value to use BigDecimal in the Inventory class, but it does the same thing and shows LBooks;@16c0663d in the output.

    Everything works other than that... how does that get converted to show the correct number? I also don't want it to print to the console window...

    I've changed my code back to the original code posted here.

    But here is the code with BigDecimal that I tried out.
     
    Campbell Ritchie
    Sheriff
    Posts: 53779
    128
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You are not returning the sum, but the array of inventory. You have shown the printout wrongly; there should be a [ at its beginning. You are supposed to return the BigDecimal sum and if you print it out you get a number. What you are doing is printing the array and arrays don't have an overridden toString method. So you get the same as with Object#toString.
    I trust your values from the inventory are not in double format, because otherwise using BigDecimal you will simply immortalise the imprecision. Either change the inventory to use a precise form of number or round the values.
    sum = sum.add(new BigDecimal(theInventory[i].inventoryValue()).round(...));
    Why are you using a for loop rather than a for‑each loop?
     
    Amie Mac
    Ranch Hand
    Posts: 55
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:You are not returning the sum, but the array of inventory. You have shown the printout wrongly; there should be a [ at its beginning. You are supposed to return the BigDecimal sum and if you print it out you get a number. What you are doing is printing the array and arrays don't have an overridden toString method. So you get the same as with Object#toString.
    I trust your values from the inventory are not in double format, because otherwise using BigDecimal you will simply immortalise the imprecision. Either change the inventory to use a precise form of number or round the values.
    sum = sum.add(new BigDecimal(theInventory[i].inventoryValue()).round(...));
    Why are you using a for loop rather than a for‑each loop?


    So I got it working using double instead of Inventory []. I also got it working using BigDecimal but then the number was very long. At this point, for my assignment, I am not worried about the precision, but it will be something to look into because it will be important to be precise when I write code about money.

    I didn't think I could use a for each loop because I need to add together just the inventoryValue() of each entry in the array. How would a for each loop work for this?
     
    Campbell Ritchie
    Sheriff
    Posts: 53779
    128
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I showed you a for‑each loop earlier. You simply need a total.
    What do you mean about the number being too long? If it had about 40 places after the decimal point, that is because you have tried to pass the values directly from doubles without taking the precaution of rounding.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!