Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Should I use IndexOf List in this case?

 
Ranch Hand
Posts: 717
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am now stuck at a use case where by before creating a new Order, I would need to check if the petId and the associate instance variable petStatus is sold or not in the class Order.

So, I'd like to know what is the best way to tackle a use case like this?

Should I use indexOf to go thru an existing pet list and get the petId first before checking if this petId is associate with the status sold, and therefore I need to just throw an exception.


Is it possible to create a generic function that I can call within a create Order method to check on the above ?

Should I use indexOf or HashMap or ?

Hope someone can give me some hints as to how to go about doing this kind of task.

Thanks alot.
 
Marshal
Posts: 71675
312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know. We might have more idea if you give us more information please.
 
tangara goh
Ranch Hand
Posts: 717
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Don't know. We might have more idea if you give us more information please.



Here's a my laughable code;



And then in my create Order method

 
Campbell Ritchie
Marshal
Posts: 71675
312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just my opinion:
Sounds like a case for a map. Iterate its key set and filter by status SOLD. That is easily done with a Stream.
 
Campbell Ritchie
Marshal
Posts: 71675
312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can have Maps linking status (SOLD, IN_TRAINING, etc.) to a List<Pet>. Or even a Map<Status, Map<ID, Pet>>. Go through the Java™ Tutorials section and see what you can find. It might be under “Map Implementations”. If status is an enum, that is one of the few occasions where it is permissible to use the == operator.
 
Saloon Keeper
Posts: 7611
68
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think using Map<Integer,Pet> where the Key is the Pet id would be the route to go. Barring that, having this simple method to find a Pet would make other tasks easier.
 
tangara goh
Ranch Hand
Posts: 717
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Carey,

It is not just finding the pet id I need to have one more criteria which is if it is SOLD then I can't proceed to create the Order to have this petId inside.

And also after reading the Oracle's tutorial on Collections on how to filter it with Streams I am still not getting it right.

Hope someone can let me know how should it be done such that it will let me know the id and the corresponding petStatus is SOLD



And then I also tried this when people advice me but is it still wrong



The error is : Type mismatch: cannot convert from Stream<Pet> to boolean and also the method findFirst() is undefined for the type PetStatus


Tks.
 
Campbell Ritchie
Marshal
Posts: 71675
312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Count your ()s; I think they have got unbalanced.
 
Carey Brown
Saloon Keeper
Posts: 7611
68
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

tangara goh wrote:It is not just finding the pet id I need to have one more criteria which is if it is SOLD then I can't proceed to create the Order to have this petId inside.


Then you'd combine the method I gave you:
With


 
Campbell Ritchie
Marshal
Posts: 71675
312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can happily use == if one of its poerands is an enum constant. It even says so in the JLS somewhere.
I would prefer the != null test elsewhere. I would prefer a mechanism to reassure me that nulls don't get into the Map in the first place.

Damn! That won't work, because get(K) returns null for a non‑existent “K”. You might need a containsKey() test.
 
Carey Brown
Saloon Keeper
Posts: 7611
68
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You can happily use == if one of its poerands is an enum constant. It even says so in the JLS somewhere.

I caught that right away. Thanks.

I would prefer the != null test elsewhere. I would prefer a mechanism to reassure me that nulls don't get into the Map in the first place.

Did I miss where in this thread the OP actually made the transition to a Map?
 
Campbell Ritchie
Marshal
Posts: 71675
312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:. . . Did I miss where in this thread the OP actually made the transition to a Map?

Maybe I'm seeing things. which aren't there.
 
tangara goh
Ranch Hand
Posts: 717
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:

tangara goh wrote:It is not just finding the pet id I need to have one more criteria which is if it is SOLD then I can't proceed to create the Order to have this petId inside.


Then you'd combine the method I gave you:
With





Hi Carey,

I think the infor I provided is not enough.

Please take a look at the Order class



and the Pet class



And then in my RestController I need to do a creation of Order with the Order as a parameter, I feel that no matter what I did, even with the right method, the order will get through ....

So, it seems there is no way to check the Order if it contains the petId with the item SOLD right since the paremeter use in the Rest Controller is using Order



So, I am stuck and really don't know how to handle this case.

Hope you will give me some hints how to handle it....I spent a few hours yesterday night trying to get this right but to no avail...and my company deems me very S L O  W....sob....left with only some days left....

Tks.

So,
 
Rancher
Posts: 4784
50
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That last bit of your code (the bit where you are trying to find the pet and whether it is sold) should be a call to the method Carey showed.



Of course that new method ought to be in a service layer rather than in the controller, but small steps...
 
tangara goh
Ranch Hand
Posts: 717
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:That last bit of your code (the bit where you are trying to find the pet and whether it is sold) should be a call to the method Carey showed.



Of course that new method ought to be in a service layer rather than in the controller, but small steps...



Thanks Dave.  It works.

But, when I perform the same feat on a GET request, i am not sure it doesn't work as in it is supposed to give me a 400 but when I enter a id that is not there or if it is already created, postman just won't send the request and it will not go into that endpoint I am testing....

I really don't know how to debug and am getting no way....feeling really depressed ... really hope you can give me some hints. tks.





so in my Controller  I have a findOrderId and with the path varaible id,

I use it like this

if(orderIdExist(id) == false{
throw error


But, instead of throwing error, it didn't get thru but just gives me a could not send request in postmean
 
Dave Tolls
Rancher
Posts: 4784
50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


And what is that method going to do?
Every time?


and this one won't compile, so no idea how you're getting it to run.
 
Marshal
Posts: 3349
492
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

tangara goh wrote:... when I perform the same feat on a GET request, i am not sure it doesn't work as in it is supposed to give me a 400 but when I enter a id that is not there or if it is already created


A 400 Bad Request  status code generally means that there was a problem at the protocol-level (missing parameter, parameter with wrong data type, etc.), not a failure due to an application-level condition.

If a client tried to create a resource which already exists, I would expect a 409 Conflict.
If a client requests a resource which does not exist, I would expect a 404 Not Found.


tangara goh wrote:when I enter a id that is not there or if it is already created, postman just won't send the request


I think that you are reading this scenario wrong.  How could Postman (or any other client) know the state of a resource (and refuse to send a request) without first communicating with the server?
 
tangara goh
Ranch Hand
Posts: 717
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:

tangara goh wrote:... when I perform the same feat on a GET request, i am not sure it doesn't work as in it is supposed to give me a 400 but when I enter a id that is not there or if it is already created


A 400 Bad Request  status code generally means that there was a problem at the protocol-level (missing parameter, parameter with wrong data type, etc.), not a failure due to an application-level condition.

If a client tried to create a resource which already exists, I would expect a 409 Conflict.
If a client requests a resource which does not exist, I would expect a 404 Not Found.


tangara goh wrote:when I enter a id that is not there or if it is already created, postman just won't send the request


I think that you are reading this scenario wrong.  How could Postman (or any other client) know the state of a resource (and refuse to send a request) without first communicating with the server?



Well, let me double check the swagger documentation but in the previous Controller,  that was the response code and my mentor didn''t say anything like it is wrong.


But, when I put a request forward - then it will be sent to the server - and how can the server not known since I have earlier created several Post of Order and now i just need to getItById ?

So, I hope with the above, it is more clear to all.

Please let me know if there is anything wrong with the code.

Tks.

And now, my Postman is down and I do not know how to activate it again...


 
Dave Tolls
Rancher
Posts: 4784
50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Re: Postman.
What OS are you on?

Is there a Postman instance running hidden?
("ps ax | grep Post" on Linux will show that up).
Happens sometimes, including to me this morning. If something died it can leave the Postman process running, which prevents you launching it.
 
tangara goh
Ranch Hand
Posts: 717
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:Re: Postman.
What OS are you on?

Is there a Postman instance running hidden?
("ps ax | grep Post" on Linux will show that up).
Happens sometimes, including to me this morning. If something died it can leave the Postman process running, which prevents you launching it.



I am using Windows.  Thank god I managed to recover it by searching it in the recycle bin.

@Dave

Could you let me know why is my delete by Id is not working ?

Is it necessary for me to compare the order Id if it contains a petId, not just the Order Id with the parameter in the method ?

Kindly confirm this.

Tks.
 
tangara goh
Ranch Hand
Posts: 717
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:

And what is that method going to do?
Every time?


and this one won't compile, so no idea how you're getting it to run.




Hi Dave,

I have changed my method to as follows but it is only good for one time use as in if I run Postman it will return 200ok if the id matches.  On second run, it will give me 400 if the id matches.  Is there anything wrong with the code ?



 
tangara goh
Ranch Hand
Posts: 717
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Where do you reset 'condition' back to false.



Isn't if(! condition){ is resetting back to false since it is not true?
 
Carey Brown
Saloon Keeper
Posts: 7611
68
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Carey Brown
Saloon Keeper
Posts: 7611
68
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
tangara goh
Ranch Hand
Posts: 717
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Nope. You'd need to put this somewhere:



Sorry I did have that just that I forgotten to write it here.

So, what do you think it is the problem for causing me all those issues?
 
Carey Brown
Saloon Keeper
Posts: 7611
68
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

tangara wrote:

Should be something like:
 
tangara goh
Ranch Hand
Posts: 717
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:

tangara wrote:

Should be something like:



Hello Carey,

Today, after I get my Ide up again I did several test and the old codes actually run successfully.  So, even I perform a 2nd selection, it still runs.

And now I am stuck if the old code is actually correct or wrong.  So, it means if the code is wrong it still able to run.

How do I verify in this case if it is the wrong code or the postman that is doing a bad job ?
 
Carey Brown
Saloon Keeper
Posts: 7611
68
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

tangara goh wrote:


The way you've written it it will only test the first Order in 'orders'. IF it equals id THEN it returns OK, ELSE it will throw an exception. Line 11 is never reached. Now this snippet may no longer match what you have, in which case, please post your updated code. Also, this doesn't have anything that resets 'condition'.

Edit: OR iit may be that you are entering the method with 'condition' already set to true.

P.S. Please fix your indentation. I had to hand indent this to make sure my point was clear.
 
Carey Brown
Saloon Keeper
Posts: 7611
68
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

tangara goh wrote:


After some consideration, whether this "works" or not depends on how the 'condition' variable is set and tested elsewhere in your code, of which we have no knowledge.
 
tangara goh
Ranch Hand
Posts: 717
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

tangara goh wrote:

Carey Brown wrote:

tangara wrote:

Should be something like:



Hello Carey,

Today, after I get my Ide up again I did several test and the old codes actually run successfully.  So, even I perform a 2nd selection, it still runs.

And now I am stuck if the old code is actually correct or wrong.  So, it means if the code is wrong it still able to run.

How do I verify in this case if it is the wrong code or the postman that is doing a bad job ?



There is a condition of false at the beginning of  this method.
I am very sure that there is something wrong with the Postman cos there was another person I asked who gave me the same code.
postman is really unreliable.  Unfortunately, at this point of time I do not know how to do the MVC Mock etc as the code now is not really using Spring way to do it and there was no service layer whatsoever and of course the tight timeline means I would never get to do it cos I still need to study it before I know how to do it.

Anyway, I just hope you could explain to me why there is a break in your code?

Is this the only way to write this method ? Is there anyway without using the break  and it will read the 2nd order and so forth?

 
Carey Brown
Saloon Keeper
Posts: 7611
68
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, the "break" was to stop the for-each loop from continuing once we have found a matching id.

Also, I'm guessing this might be closer to your needs, it uses a return instead of a break.
And based on your last comment, this doesn't handle any Postman errors. Not sure whether you would want a try/catch here for that or let a PostmanException propagate up the call chain. I don't know how you are going about detecting that error.
 
tangara goh
Ranch Hand
Posts: 717
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:First, the "break" was to stop the for-each loop from continuing once we have found a matching id.

Also, I'm guessing this might be closer to your needs, it uses a return instead of a break.
And based on your last comment, this doesn't handle any Postman errors. Not sure whether you would want a try/catch here for that or let a PostmanException propagate up the call chain. I don't know how you are going about detecting that error.



Hi Carey,

I need a not found response in my ResponseEntity.

How can I include that part if the Id is not found?
 
Carey Brown
Saloon Keeper
Posts: 7611
68
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 7 is the NOT FOUND instruction. I'm just putting there what you had. If that's not correct put what you want there. Line 7 is NOT passing 'o' so I was assuming that that constructor ended up with  'null' for the payload. Really, I'm just guessing based on very little information you've given and it's you who needs to be following the logic and filling in the gaps, or at least helping us to fill in the gaps with you.
 
tangara goh
Ranch Hand
Posts: 717
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Line 7 is the NOT FOUND instruction. I'm just putting there what you had. If that's not correct put what you want there. Line 7 is NOT passing 'o' so I was assuming that that constructor ended up with  'null' for the payload. Really, I'm just guessing based on very little information you've given and it's you who needs to be following the logic and filling in the gaps, or at least helping us to fill in the gaps with you.



Hi Carey,

So, there isn't a need to put that line condition is false like that right ?  Because I was told to do it that way so I am very confused by what is actually the right way to write the code.

Thankfully there is still this forum to ask

Please advise me.

And with the return new Responsibility ok for a orderId that is null, is a need to throw exception because the message from the ResponseEntity would have told you that it is a invalid orderId so if I am going to add in the exception handler then it is like redundant.  I am not sure about this part so please advise me. Tks.

I just want to make sure I am learning the right things...even though i am old already
 
Carey Brown
Saloon Keeper
Posts: 7611
68
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some opinions on that. Some insist that you should always place the returns at the bottom of the method, and this involves declaring a variable, initializing it, setting it, and testing it, to see what you're supposed to return at the end. It is my opinion that if the method is very small that this overhead obscures the clarity of the logic, and in the case of a very small method, finding a return in the middle should not be an issue. Yet you'll still get people who will argue with you. If your method is middle'n to large then stick with the additional variable.

Similar arguments and considerations surround the use of 'break' and 'continue'.

A question: In your original code you showed a return with an object reference, a return with no object reference, and a thrown exception, I couldn't tell which one of those you intended to use if the id is simply not found.

P.S. Whomever signs your paycheck (or your instructor) has the final say.
 
Always! Wait. Never. Shut up. Look at this tiny ad.
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic