• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Designing an address book in Java

 
Marshal
Posts: 74048
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saikrishnan Srivatsan wrote: . . . I thought I should be creating my own custom exceptions here. . . .

Agree, but this is one of those occasions where there already is a ready‑made Exception class which means exactly what you want or




… at least I think so.

Whatever you have got, the differences of opinion are minor; some good stuff there. You have already exposed the Name class, by marking it public.
 
Ranch Hand
Posts: 540
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think if you can move the validations to the Name class, the Contact constructor won't have to throw exceptions anymore.
 
Campbell Ritchie
Marshal
Posts: 74048
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure there is a “right” and a “wrong” answer. You can always use the first name and last name text components on a GUI to instantiate a Name object.
Also, that way you have two parameters for the constructor rather than three. The more parameters, the more opportunity there is to get them in the wrong order.
 
Ranch Hand
Posts: 67
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. I'll use a "list" as value in my hash map to permit more than one contact being stored for a name. Suppose the user wants to change the phone number for one of the contacts, how should I implement that? Which class should contain the method to edit the phone number? My PhoneNumber class will have a setPhoneNumber() method; so can I just return a contact from the AddressBook class when the user does a searchByName(), and then if he wants to edit the phone number, he can do contact.phoneNumber.setPhoneNumber()?
 
Saikrishnan Srivatsan
Ranch Hand
Posts: 67
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've added a class for PhoneNumber (even though it only stores a String at the moment) and updated the Name and Contact classes. I've thrown IllegalArgumentException in a couple of places just to make the code self-explanatory when someone looks at it. Should I just document that instead?

Kindly also point out flaws in the design and suggest areas for improvement.
 
Campbell Ritchie
Marshal
Posts: 74048
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unchecked Exceptions which are expected should be described in the documentation comments.
No, you should always return true or false from a method validating an input. You probably still need to throw exceptions for invalid input, but that should occur elsewhere.
 
Saikrishnan Srivatsan
Ranch Hand
Posts: 67
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Unchecked Exceptions which are expected should be described in the documentation comments.

OK, so I'll remove the "throws" clauses I have for IllegalArgumentException and document them instead.

Campbell Ritchie wrote:No, you should always return true or false from a method validating an input. You probably still need to throw exceptions for invalid input, but that should occur elsewhere.

I don't think I understand this one. Have I done this in my code somewhere?
 
Ranch Hand
Posts: 99
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I don't think I understand this one. Have I done this in my code somewhere?



If I'm not wrong, Ritchie is taking about the validation methods, phoneNumber and validName.

@Ritchie, shouldn't we use a try catch resource instead of removing the new exception from the validation methods?
 
Campbell Ritchie
Marshal
Posts: 74048
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Saikrishnan Srivatsan wrote: . . . I don't think I understand this one. Have I done this in my code somewhere?

Name class, line 20.
 
Campbell Ritchie
Marshal
Posts: 74048
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aki Mohan wrote: . . . @Ritchie, shouldn't we use a try catch resource instead of removing the new exception from the validation methods?

try‑catch is a statement, not a resource.

No. If you have a method which validates something, that method can return true or false, and should usually not use Exceptions.
 
Saikrishnan Srivatsan
Ranch Hand
Posts: 67
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Campbell. Where else can I throw that exception from? The constructor receives the arguments, and passes them straight to my isValidName() method without doing any validation itself, so I am not able to figure out how I can throw this exception elsewhere.
 
Campbell Ritchie
Marshal
Posts: 74048
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you call that method from the constructor, throw the Exception in the constructor.
 
Saikrishnan Srivatsan
Ranch Hand
Posts: 67
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Campbell; I think I get your point.

  • My boolean method never returns false; it can only true or throw an exception. Is that why you're saying that the exception should be moved to the constructor?
  • In the contructor, I shouldn't include a 'throws' clause, correct? Unchecked exceptions should always be documented?
  • Does my AddressBook class look good?
  • You'd earlier said that the searchByPhoneNumber() method looked interesting and asked me how I'd implement it. Have I done it right?
  •  
    Campbell Ritchie
    Marshal
    Posts: 74048
    332
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Saikrishnan Srivatsan wrote:Thank you, Campbell . . .

    You're welcome

  • My boolean method never returns false; it can only true or throw an exception. Is that why you're saying that the exception should be moved to the constructor?
  • In the contructor, I shouldn't include a 'throws' clause, correct? Unchecked exceptions should always be documented?
  • Yes to both

    . . .

  • You'd earlier said that the searchByPhoneNumber() method looked interesting and asked me how I'd implement it. Have I done it right?
  • You are using a linear search, so that runs in linear time. There is a way of implementing an address book such that search by phone number runs in logarithmic time, or even in constant time. I hereby challenge you to work out how to do it in constant time.
     
    Saikrishnan Srivatsan
    Ranch Hand
    Posts: 67
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Can I use another hash map that uses phone number as key?
     
    Campbell Ritchie
    Marshal
    Posts: 74048
    332
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
     
    Saikrishnan Srivatsan
    Ranch Hand
    Posts: 67
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    Thank you Would you create a new data structure (dual key hash map?) that takes 2 keys and a value? Or would you just use 2 hash maps in the address book class?
     
    Campbell Ritchie
    Marshal
    Posts: 74048
    332
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I have never heard of a dual key Map. Try googling for Apache commons, who have all sorts of interesting classes, but even they might not have that. In which case you would use two Maps.
     
    Saikrishnan Srivatsan
    Ranch Hand
    Posts: 67
    1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you! I wanted to ask you in general as well if you have ever had to create your own data structure? What things do you consider when you do? I'd like to know if you've ever had to do so.

    For example, in this case, if there were a dual key map, I could have used that. Should I try to create one or just use 2 maps?
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic