• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is there a more efficient way of summing up variables?  RSS feed

 
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have this small program that creates a sales opportunity and its related products, and then prints out the opportunity name, total quantity and total price



The code works as expected, and is successfully prints



That said, I feel the way the OpportunityCreator class sums up the quantities and prices (see the comments on the code) is very inefficient, and would only work if there were 3 or less products. How can I design this so that it sums the quantities and prices of all the Product objects that I create, regardless of how many of them are? I understand probably I'd need to redesign the whole thing but I'd like to get at least an idea of how this can be achieved.

Originally I was thinking of creating an ArrayList and adding all the prices/quantities there but wasn't sure how to do. I also thought of using a loop to loop through the different object references and sum the quantity/price but was also unsure on how to do it.

Any tips or suggestions on how to better design this would be very much appreciated. Thanks!
 
Bartender
Posts: 543
4
Java Netbeans IDE Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your original ideas are very good. Why don't you read up on them and try them out?
 
Pablo Jose Alvarez
Greenhorn
Posts: 17
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that's good to know. I will work on that then and see if I can redesign it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!