How Can I Track the Total Sales in My Mini Store Program?

0
12
Asked By TechieGadget42 On

I'm a first-year computer science student working on a project for my Java class, where I need to create a mini store sales tracker. Initially, it worked by processing one product at a time, but I added a loop so I could input multiple products. The issue I'm facing is that I can only get a grand total for each product separately. I want to instead calculate a single grand total that reflects all the products sold together. I'm not sure how to approach this, and I could really use some guidance on how to implement this functionality properly.

2 Answers

Answered By CodeMaster98 On

It sounds like you need to store the grand total for all products sold during the session. You can do this by declaring a variable outside the loop that keeps a running total. Every time you calculate a grand total for an item, just add that to your main total. Here’s a quick idea: Before your while loop, initialize a variable like `double overallTotal = 0;`. Then, after calculating each product's grand total, add it to `overallTotal` like `overallTotal += grand_total;`. Finally, once the user decides to stop entering products, print out `overallTotal`. That way, you'll have a combined total of all sales! Good luck!

StudentCoder21 -

Could you give an example of how to implement that? I’m still a bit lost!

TechieGadget42 -

Thanks a lot for the suggestion! I'll try that out.

Answered By DevGuru99 On

To clarify, you're looking for a single grand total of everything sold, right? Right now, you're getting the total for each item, which is great. What you want to do is take each item's total (after tax) and add it to a running total. Like the previous commenter said, declare a variable for the overall total before the loop, and add to that variable as you calculate each product's grand total. At the end, when you quit the program, just print that overall total. It might also help to keep an array or a list if you want to store the details of each product, but that’s optional for just tracking totals.

TechieGadget42 -

Yes, that makes sense! I think I’ll start with the overall total first before expanding to an array.

StudentCoder21 -

Thanks for the explanation! That helps clarify things a lot.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.