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.
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 ?
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?)
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