• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Writing a sum to a file

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to make a shopping list, i've succeded to create a file, print out every objects
name and price into my file. But the thing im working with now is to write the sum of the prices at the bottom of the shopping list.

How can i reach and add all of the prices of all objects to a string?

I've got these three clases:











Anyone got an idea of how to do it? I've tryed to make a method in the class Varor that returns a string that adds upp the prices, but it doesint seam to work.
 
Marshal
Posts: 26493
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start with the simplest possible thing: how can you get the price of one object?

Next, write a loop which gets the price of each object and sums them up.
 
Johannes Thorén
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would use something like
v.vara to get vara and v.pris to get the price. But the thing is that, then i will only get the name and price from the last object i added.
I need to get the name and the price (vara, pris) from each object in the list. Is there a way to get it, something like varor[i].pris and varor[i].vara ?
 
Paul Clapham
Marshal
Posts: 26493
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Johannes Thorén wrote:I need to get the name and the price (vara, pris) from each object in the list. Is there a way to get it, something like varor[i].pris and varor[i].vara ?


Something like that, yes. But you don't have an array, so not exactly like that, no.

It seems to me you have a problem keeping concepts separate, and another problem combining concepts. You have an Iterator which gives you one Varor object after another. And you know how to get the price from a Varor object. So combine those two concepts and now you have a way to get the prices of all the Varor objects.

(By the way isn't "Varor" a plural word? If you look at the names of other Java classes you have used, are their names singular or plural? String -- singular -- it represents a string. File -- singular -- it represents a file. ArrayList -- singular -- it represents a kind of list. Varor -- plural -- what does it represent?)
 
Johannes Thorén
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesint work when i put every object from the ArrayList to an array eather.

I tryed two things:
first to make an integer, totalprice where i want to add everything, and another method to put every object into an array and get the price from there.



Your right, Varor is a plural word, means something like shopitems, since im adding several to a list.>
 
Johannes Thorén
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fixed the problem now. Made a method called returnPrice in class Varor that returns the price
and after i added added an object in my main class im adding the price to an int and then i got a total price when im done.

Thanks for the help tho
 
You will always be treated with dignity. Now, strip naked, get on the probulator and hold this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic