Win a copy of High Performance Python for Data Analytics this week in the Python forum!

Prasanna Raman

Ranch Hand
+ Follow
since Sep 05, 2010
Cows and Likes
Cows
Total received
1
In last 30 days
0
Total given
0
Likes
Total received
5
Received in last 30 days
0
Total given
17
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Prasanna Raman

Correct, there is no difference, but I'm just trying to program for any country thay may have different formats! And see how I can make it work.
10 months ago
Ok, and what if the formatting also differs by the type - mobile or landline? USA may be the same, but assuming it's different.
10 months ago

Campbell Ritchie wrote:If you go back to an earlier suggestion that you have a prefix (area code, dialling code, etc.) and number stored as separate fields, can you separate a String into its constituent parts in the constructor?
What about a factory method requiring a non‑null locale as a parameter, returning subclasses of PhoneNumber? Then make all your constructors private.



Can you please elaborate on this? I do have a prefix and a phone number field for my PhoneNumber class, in addition to the PhoneNumberType field. I don't understand the parts about the constructor and the factory method
10 months ago

Campbell Ritchie wrote:There are several ways you can store a formatter object.

  • You can put it into a map as the “V”, but I shall leave it to you to work out what to use as the “K”.
  • You can have subclasses as I suggested here, with formatters being private static fields of each class.
  • If you are using Locales, you might be able to put formatting information into a resource bundle (not sure).
  • You can have it associated with the type enum. But I remain to be convinced that you need a type enum in the first place; do you have an intersection of the range of phone numbers anywhere so there is a landline and a mobile with the same number in the same country? Would you need an additional (?prefix) number to disambiguate them?
  • Ouch! How are you going to describe that in the documentation? Look at that awkward code with two dots in. Why doesn't the toString() method return the phone number correctly formatted? Also a plainToString() method which returns my phone number in the blank formatrather than correctly formatted like this?If you go back to an earlier suggestion that you have a prefix (area code, dialling code, etc.) and number stored as separate fields, can you separate a String into its constituent parts in the constructor?
    What about a factory method requiring a non‑null locale as a parameter, returning subclasses of PhoneNumber? Then make all your constructors private.

    I you are confining yourself to one country only, then a lot of this current post doesn't apply.



    I have the enum not because there is overlap, but because I want the user to be able to select the phone number type when storing, and when viewing. If including the type as part of the phone number is not the ideal way to do this, then please tell me how else this can be done. I probably have bad design here.

    As for storing in map, I do have that map in the PhoneFormatProvider class, and also the inheritance hierarchy for the formatters (based on locale) as suggested by you earlier; but what I don't know is how I can also include the type within the formatter. Should I have USMobileFormatter, USLandlineFormatter etc.?

    Unfortunately, I don't understand what you're trying to say with the toString() methods Can you please elaborate?
    10 months ago
    Now for the getFormatter(Locale locale, PhoneType type) method, how do I implement this if I need to provide formatters based on both locale and type?

    Even in the detault formatter, I will need to do conditional based on landline vs mobile! If a new locale gets added, then I need to get the correct formatter for the locale, and then somehow get the correct formatter for the type for the locale! I am probably confusing myself here, but it keeps getting complicated and I am not sure how to solve this.

    Upon thinking about this further, is this where I could do something like this?



    But even this doesn't solve the problem of how I write the logic to format based on locale and also type Unless I have a formatter for each subtype - as USMobileFormatter, USLandLineFormatter etc..
    10 months ago
    Is this better?

    10 months ago
    I've made some progress, not sure if this is ideal. Please review.







    Is this reasonable? Now, where and how do I implement that Map you were talking about?
    10 months ago
    Thank you, Junilu. I am struggling with how to implement the factory class. How should I proceed from here?



    I know I need to use the 2 parameters passed to getInstance in the factory class somehow, but not sure how I can create the instances.


    10 months ago

    Campbell Ritchie wrote:Yes, you can declare abstract methods in an enum, but you need to override them first(!) There is an example about arithmetic in the Java® Language Specification (=JLS), which I suggest you emulate. You can add things like country code to each of your enum constants, too. I recommend you read the whole JLS chapter about enums.



    Thank you, just looked at the arithmetic eval function in the link you provided. So, actually putting the format() method inside the enum is good design here? That's where I am getting confused!

    I have a PhoneFormat class which returns a PhoneFormat object from a factory method but not sure yet how to implement that method.
    10 months ago
    I feel as if I am going around in circles with this one unfortunately I played around with putting the format as an abstract method inside the Locale enum for each locale to override, but that doesn't seem to be a good idea either! Please help!
    10 months ago

    Junilu Lacar wrote:
    Or I might play around with a design where you have to write something like this instead:

    There are many ways you can design the API but the general idea is that the logic is encapsulated in a Formatter class and you have a Factory method that can give you back an appropriate instance of Formatter based on parameters you provide to it.



    Hello Junilu,

    I have the following to start with. Can you please tell me if this is reasonable? I am stuck on what to do after this though.



    I'll probably need to create an inheritance hierarchy based on locale, like the below? But I know you'd said this wasn't the approach you were suggesting.



    In this case, I'll have to create classes for each country still and then also separate classes for each phoneType?
    10 months ago
    It's only for a sample contact book application that I am creating. So, I don't anticipate too much data.
    10 months ago

    Campbell Ritchie wrote:Why do you need a type? Where I am it is very easy to tell landline numbers from premium numbers and mobile numbers. Mobiles begin 07, landlines begin 01 02 03 and premium numbers begin 09 (or some 08).



    If a user wants to see on the addressbook or save it as a 'mobile' or a 'landline' number, how can I support this operation without creating an explicit PhoneType class or enum?
    10 months ago
    I had briefly posted this in one of the other posts, but because this is probably a topic of its own, as pointed by one of the senior members, I've created a separate thread for this.

    What would be the simplest way to persist data between application runs for a Java application, that doesn't involve database?
    10 months ago
    No overlaps, but the formatter may do things like formatting landline numbers and mobile numbers differently.
    10 months ago