aspose file tools*
The moose likes Java in General and the fly likes Decrement problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Decrement problem" Watch "Decrement problem" New topic
Author

Decrement problem

p mayur
Greenhorn

Joined: Jun 28, 2014
Posts: 28
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
Marshal

Joined: Mar 22, 2005
Posts: 42930
    
  68
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

Joined: Jun 28, 2014
Posts: 28
if i give >=1 it keeps on incrementing, because there will be always a quantiy in cart.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42930
    
  68
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

Joined: Jun 28, 2014
Posts: 28
How to do that Sir? could you help me please.



Is this right?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42930
    
  68
You would return true if -and only if- in line 7 you actually decremented the quantity. Otherwise you would return false.
p mayur
Greenhorn

Joined: Jun 28, 2014
Posts: 28
Is it correct now.
p mayur
Greenhorn

Joined: Jun 28, 2014
Posts: 28
Any Help Please ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42930
    
  68
Does it work correctly now?
p mayur
Greenhorn

Joined: Jun 28, 2014
Posts: 28
No sir, still it stops increment at 3 in stock
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
p mayur
Greenhorn

Joined: Jun 28, 2014
Posts: 28
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
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40


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

Joined: Jun 28, 2014
Posts: 28
No i didn't place any debug codes. I don't know to do that. I just now started learning.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

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

Joined: Jun 28, 2014
Posts: 28
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

Joined: Jun 28, 2014
Posts: 28
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

Joined: Jun 28, 2014
Posts: 28
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
Bartender

Joined: Mar 28, 2008
Posts: 1234
    
  67

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.


Tim Driven Development
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40

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
Sheriff

Joined: Sep 28, 2004
Posts: 19070
    
  40


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

Joined: Jun 28, 2014
Posts: 28
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

Joined: Jun 28, 2014
Posts: 28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Decrement problem