Hello everybody, so here is a bit of code for a homework I have for tomorrow. The problem I am having is that when I pick 9 products from the menu (Scenario 1), it gives me the total of what it cost. But when I pick only one product from the list (Check Scenario 2 & 3), it STILL gives me the total cost as if I had still picked the 9 products (Check last screenshot). Check images below to understand better.
I don't see the logic error, but I can give you some suggestions...
1) put in more System.out.println(); statements. You will take these out later, but these are GREAT for debugging. It will let you see what your code is really doing.
2) Simplify. Comment out all but (say) 2-3 choices. no need to clutter up your non-working code with more stuff that doesn't work. Get it working with 2-3, and then you can add in the remaining 7-8.
3) Consider using an array instead of 10 variables named nProductX. This kind of solution doesn't scale well. Imagine if you had 100 or 10,000 products.
4) Consider starting over. writing almost 200 lines of code only to find out it doesn't work is only going to lead to frustration. I NEVER write more than 2-3 before I recompile and TEST TEST TEST.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joined: Sep 29, 2013
Thanks for the reply,
Appreciate your idea of putting arrays in it. Makes it more easier instead of putting it one by one.
Could you be more specific on the system print and how does it find the bugs? I mean, I know what its for but didn't know it worked as a debugger.
Have re done this homework and always tested it one by one, but the error looks like its coming from the first IF statement. Have tried to incorporate another IF statement within it or an ELSE IF to see if that works. Even I tried to put constant variable and it also didn't work. The reason I put the whole work as if it was done is to show and seek guiding on where might be the mistake.
First, you ask for the item at the end of the loop -- and by the time you reach there, you processed every item on the list. So, you start off with every item purchased.
Second, the choice item number is incorrect. Choosing 1, or 2, or 3, or any number of the items on the list doesn't work. The check is by it's price, not by it's choice. So, if you want a gMac, you have to enter 1299, and not 6.