This week's book giveaway is in the NodeJS forum. We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line! See this thread for details.
I think you went too far with coding exercise and lost the track somewhere way back.
I usually solve problems in small chunks, and those chunks being represented by methods. In normal conditions they are small. About 5 lines long each.
In order to be able start with methods, you need to know what is the exercise about, so you could devide the task into many smaller tasks, those smaller tasks into smaller again, until you have pieces which are managable to solve.
Would you like to describe what is the coding exercise about?
1. You keep starting new threads but you're asking about the same problem. Please stop doing that as you're likely to get on the wrong side of a moderator and you don't want that to happen.
2. Understand that nobody gets paid to answer questions here; this is a volunteer site and people here help out and answer questions for many reasons but none of them are about money because there is none.
3. You seem to be trying to tackle a problem that is out of the range of your current knowledge of Java and programming constructs. You might want to go back and review some of the concepts that you've been taught and work on understanding each one first.
That said, I'll post some followups to walk through some issues that are making it hard for people to give you advice. Again, some of that goes back to your apparent lack of understanding of certain aspects of programming that are needed to write the program you seem to want to write.
First thing that's making it difficult for people to help you: your code formatting and indentation is atrocious. Reading code that is not formatted properly is very difficult because it's harder to understand the organization and flow of the logic. Also, you have way too much code in main(). That's bad. Good programs are broken down into small logical chunks of functionality where each chunk does one and only one thing. Ideally, your main() method should consist of only a few lines of code to get the program started and then to shut it down gracefully.
Here's your code formatted properly. It took me just a few seconds to do this since all I had to do was to copy it all, then paste it into my IDE and hit the keyboard shortcut to reformat the code. You should do this too, going forward. If you keep posting poorly formatted code, people will be less inclined to read it and therefore less inclined to help you. That's probably one of the main reasons you're not getting as many responses.
If I were to solve this problem you're working on, I'd probably try to have at least two classes to help: a Product class and an OrderItem class.
The Product class would help manage information about each product you want to sell, its description, and perhaps its retail price. An OrderItem would help manage information about a product being purchased, the quantity and the cost to purchase, based on the product's price.
To Liu's earlier point about breaking up your program into small chunks, here's what a decent main() method might look like:
This is what the execute() method might look like:
As you can see, there's no detail in this method. It's just composed of calls to other methods that do specific tasks. Reading this code, however, can give a good idea of the flow of the program logic and what's being done overall.
This is called functional decomposition of programs and it's an important concept to follow if you're going to write any sort of non-trivial program. Otherwise you'll drown yourself in details and complexity.
For one, it's all hard-coded. There are product names and prices in there. What happens if you decide to change the price of a camera? You'd have to change your code in multiple places to make what you display agree with the calculations being made when the user chooses to buy a camera. Having to reflect a change in a product's price in multiple places in your code is a bad thing and a potential source of bugs. Ideally, you would use a Product object to manage that information and just use it where you need to. That way, you'd only need to tell a product object to update its price and no other piece of code needs to be edited but the program would still adjust to the new price and work correctly.
Also, that line of code really should be broken up and put into a method like showMenu():
is again not very good. I would replace it with something like this:
Notice how that little piece of code speaks for itself and can stand on its own, obviating the need for a redundant comment like "// shows the user products chose".
And this is how you might write the showCart() method:
Do you see any similarity of this to some other method I showed above? When you break your program down into small chunks, little patterns of symmetry like this become more obvious and your program starts getting easier to write and maintain.
Finally, to get what you've been asking about all along, this is the skeleton of the code you might write:
Of course, you'd have to implement the logic of the OrderItem.getCost() method but that's a simple multiplication of the Product.price and the OrderItem.quantity.
Again, the point of all this is to manage complexity by breaking things down into such small chunks of work that each chunk become trivial to write.