hi how can i validate the postal code based o this condition
9999 may not be used for US province .if the street provience is maxico or argitina or brazil the streEt postal code must be 9999
The proper process is to write the requirements BEFORE you write the code; not during or afterward.
US postal codes must be either 5 or 9 digits. The code you are using would allow '123', 'ABC', '$(#' and a lot of invalid values.
So even for a US 5 digit value, at a minimum, you need to check the length and that all characters are digits.
Your 'setStreetPostcode' method should include validation of the value. Any code calling your method could set ANY value they want and there is no guarantee that it will ever be validated.
Since the 'setStreetPostcode' should include validation it needs to know the Province as well. Either pass the province as a parameter or ensure that the province has been set properly before trying to set or validate the postal code.
That also means that if the province is changed you need to either clear or revalidate the postal code.
A static Factory method is often used to implement co-dependencies like that. The factory method would accept postal code and province parameters and would only create a valid instance of the class if both parameters were valid.
In my experience this is a pointless exercise.
The value can be well formed and still be invalid. That is why there are services to look up postal codes.
And if you really attempt to do it for many countries then you must keep up to date on all the possible changes that might occur. As one example Canada allows numeric alphas and spaces.
And despite all of that it might still be wrong. If you actually need to post something then you should invest in a verification system and spend your time on that. If that isn't a realistic need then just let the service people type in what they are given.
Then spend you time on some other business functionality that is really needed.
am not validating us postal code that was just a use case,i just what to know whats wrong with my flow logic it seem is showing error even if there is right values/country
Then you are just wasting everyone's time.
All of my comments still apply - you need to write requirements BEFORE you write code and you need to validate data BEFORE you store it.
starting to sound ever more like a "do my homework for me because I can't be bothered to put in effort of my own" question.