Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Decrement problem

 
p mayur
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a shopping cart example using spring and hibernate. I am trying to increment a quantity in stock, if a user decrements a quantity in cart.There should be
minimum one quantity in cart always. So i tried to check cart quantity like shown below. But what happens is, the stock count stops when cart quantity reaches
2. (ie) for example, if there are 5 quantities in cart, when i decrement it 1 by 1 , stock is updated until it reaches 3 and stops. It should update upto 4 and the
last one can be updated when user removes the product from cart. So how can i make it update upto 4?



This is my decrement part.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does cart.decrement(productName) influence the result of line.getQuantity()? If so, I suspect line 11 should read ">= 1" instead of "> 1". But line 11 still looks strange - if line 6 is successful in decrementing the count in the cart, then line 13 should be executed no matter what; in fact, those two should be an atomic operation - either both execute or neither.
 
p mayur
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i give >=1 it keeps on incrementing, because there will be always a quantiy in cart.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, but the decrement method should not return "void", it should return a boolean to indicate whether a decrement actually happened. If it did, and only if it did, line 13 should be executed no matter what, otherwise you'll have inconsistencies in the data.

As an aside, why does one piece of code use "cart.find", whereas the other runs a loop for that? That seems inconsistent.
 
p mayur
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to do that Sir? could you help me please.



Is this right?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would return true if -and only if- in line 7 you actually decremented the quantity. Otherwise you would return false.
 
p mayur
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it correct now.
 
p mayur
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any Help Please ?
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it work correctly now?
 
p mayur
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No sir, still it stops increment at 3 in stock
 
Henry Wong
author
Marshal
Pie
Posts: 21404
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that it is safe to say, if you are expecting someone to take your code, debug it, and give you an answer, that is unlikely to happen... especially, since you only provided a code snippet, that the code relies on a few frameworks (Spring and Hibernate), and people have real jobs that needs to be done.

The best option here, IMO, is to provide more information. Tell us what you did so far to debug the issue. Tell us what you are running into. And perhaps, we can provide something that you can use to further debug the issue.

Henry
 
p mayur
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At first i just wrote code for increment and decrement button code. Then i tried to connect it with stock. Increment operation working fine.
Then i tried to do it for decrement. I got the issue as i explained above.
increment/decrement code in controller


Cart.java



These are the related codes. Please let me know do i still need to provide more information.
Actually just check my previous thread, they helped me solve the issue and i did it on my own. I am not expecting an spoon feeding here.
 
Henry Wong
author
Marshal
Pie
Posts: 21404
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

As already mentioned... please tell us what you did to debug this issue? Have you at least place debugging statements into your code? Have you noticed any patterns from the debugging statements when your ran your code? meaning when you do X, you see Y, when you expect Z? etc.

Henry
 
p mayur
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No i didn't place any debug codes. I don't know to do that. I just now started learning.

 
Henry Wong
author
Marshal
Pie
Posts: 21404
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
p mayur wrote:No i didn't place any debug codes. I don't know to do that. I just now started learning.


Well, now is a good time, as any, to start.

You don't need a fancy debugger, or profiler, or even a logger. It can be as simple as placing System.out.println() calls liberally in your code. Just print out stuff, like the location in the code, and relevant variables, until you are able to see what is going on in your code, as you run some test cases.

And add / remove them as needed, and change the output to be printed as needed, all so that you can see what is going on.

Henry
 
p mayur
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The issue is when the cart quantity is 2 and i try to decrement , the stock stops updating, as the cart value is one now. So my validation is wrong. Thats what i am asking how i should do it.

Okay i will do and check that.
 
p mayur
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I checked what you said and i don't see any drastic change at least to my knowledge.
As i said earlier when i click decrement, when the cart value is 2, it changes to 1 and
stock stops updating , since the value is 1 in cart as per my validation.
 
p mayur
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what other information i need to give. I have clearly explained the
issue. Leave my method sir. Just give me an idea how i can implement what i want.
 
Tim Cooke
Sheriff
Pie
Posts: 3128
135
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello P Mayur, Please be mindful that we are all volunteers here and a bit of patience on your part goes a long way. We have a couple of articles discussing our policy on this that I invite you to read. Firstly -> EaseUp, secondly -> PatienceIsAVirtue. You've been getting some excellent help here so far so please sit tight, I'm sure that help will continue in due course.

In the meantime why don't you take the opportunity to read over again the posts from Ulf and Henry to see if there's something you missed. For example, the suggestion to put some System.out.println() statements in your code to print out the values of your variables at key points of your code flow will help you closely inspect whether you are getting the values you expect at those points in your program. It's a simple but effective technique.
 
Henry Wong
author
Marshal
Pie
Posts: 21404
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
p mayur wrote:I checked what you said and i don't see any drastic change at least to my knowledge.
As i said earlier when i click decrement, when the cart value is 2, it changes to 1 and
stock stops updating , since the value is 1 in cart as per my validation.


p mayur wrote:I don't know what other information i need to give. I have clearly explained the
issue. Leave my method sir. Just give me an idea how i can implement what i want.


I'm afraid it doesn't work that way. We gave you some help on how to debug your program. Keep in mind that it is your program, and it is your job to debug it. If you won't do it, it is highly unlikely that anyone else will.

To be blunt, debugging starts when you have something wrong, and it stops when you think you found it, and it pass all tests. And even then, you can't be sure to truly stop. Keep adding print lines until you get to the point of the break. Then back up and add more and more detail of print lines until you get to the root cause of the break. If you see nothing wrong, then you are not printing enough detail, as clearly, something breaks -- or you wouldn't be here.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21404
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

P Mayur,

If you felt that my last post was an attack on you, I sincerely apologize. It was never meant as an attack.

In my opinion, debugging is a skill that a programmer must have -- in fact, it is probably the number one skill that a programmer must have. If you are to do programming in Java in the future, you will have to develop this skill.


So, please... try to follow your code again. Don't give up on it. And if you get frustrated, feel free to come back here -- tell us what you did (in terms of where you place the print lines) and what you are actually seeing... but don't give up.

Unfortunately, there is no magic bullet here. We are not purposely hiding the answer from you. With no Spring and Hibernate setup with your configurations on our end, we can't debug it for it either (nor should we). You have to debug it -- and the best that we can do is help you do that.

Henry
 
p mayur
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I realize my mistake . No need to apologize. I am trying. I will not leave and yes i am learning debugging now and come back with issue for solution or with the solution if i do it on my own.
 
p mayur
Greenhorn
Posts: 28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At last got solution. As Ulf Dittmer said, there is no need for any if checks, i decrement cart quantity up to 1 and disable the button after it reaches one.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic