• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Address Book Java Issues

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm been set this assignment as follows,

Problem: Create an addressbook that allows user to add, delete, search contacts by family name and list all contacts.

Requirements:
To use these classes:
1. name class - holds the family name and others.
2. address class - holds address details as string together with postcode
3. postcode class - holds typical british post codes of the form AA11 22BB
4. contact class - holds name and an address
5. addressbook class - holds collection of contacts
6. text ui - enable user to interact

To begin with i created a single class that held the name and address in an arraylist and linked it to a text ui class to get all the functionality working.
I need to split this single class up into these several classes and link them, I have created the name class and the address class. However i am a bit confused about what is required by the postcode class and i'm struggling to work out how to link the name and address classes into a contact class and then the addressbook class which will then link to the text ui.

Also do i keep it as an array list or do i need to create and store the data added into a file

Any help and guidance would be appreciated thanks
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just like a class can have String and Integer references, it can have a reference to a class you define.

Your specs tell you what to do. First, I'd create a Name and a Postcode class (Note that classes traditionally capitalize the first letter).

Then, create your address class. It will probably have several Strings - and here is the neat part - a Postcode reference. Something like:



Then you create a Contact class:



Then you create a AddressBook class that contains Addresses in some sort of collection - a Map might be a good choice so you can possibly use the lastname as a key to find the specific Address you want, but that may be more complex than you need right now.

Obviously the code above will need fleshing out - with various getters and setters, but that shouldn't be too hard.
 
Philip Stone
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have completed the Name class with the correct getters and setters

The address class is all complete bar this postcode as i'm unsure what the postcode class actually holds and then how it is used with the address class. Obviously i don't list the postcodes in the class as the user has to enter that, so is it to check that the postcode given is in the correct format?

And for the contact class all that is required is to create a contact and then it links to the name and address class to get the information?

Could you possible expand on the last point with the AddressBook class i understand it to some extent.

sorry for all the questions but it is really helpful,

thanks
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

3. postcode class - holds typical british post codes of the form AA11 22BB


Are you familiar with British postcode? I am not. In the United States, we have zip codes. They are always 5 digits, followed by a dash, followed by four digits. So, if I were creating a Zipcode class, I would need a way to store the data and validate it - just like i do with any other piece of data.

For your postcodes, you have to look at what the data can be. It is clearly not numeric, so you may want to hold it as a String. It looks like it is always four characters. I don't know if it's always 2 digits and 2 characters, but that may be something to validate (or it may not). also, it may be that the first two can't be digits like '33' or '97'. But basically, you need to need ways to store, set, update, and get that data.

Once you ahve the Postcode class written, you just put one in your Address class in the same way you'd put in a String to hold something. You will then need the appropriate getters/setters in the Address class. Something like



This is what is called "composition". It's like legos. You take smaller pieces and hook them together to make bigger pieces. If i had a Car class, it may look like this:



Engine is an entire class to itself. It has methods, getters, and setters. I can stick one in my Car, and then call its methods.


Your contact class would have, as its members, a Address and a Name. The code for the Contact class would most likely let you pass in a Name object and an Address object.


Finally, think of a real address book. When you first buy/create it, it is empty. You can add contacts in as you need to. You can search for contacts. You can delete a contact.

Your class should probably do the same thing. Therefore, you will need some way to hold more than one. The simplest way is to make an array of Contacts, but that has a few drawbacks:

1) You have to declare how many your AddressBook will hold ahead of time
2) There is not a good way to search for a specific one, like "Fred Rosenberger"
3) There is not a good way to delete one.

You can use an ArrayList, which will grow automatically if it needs to, which solves #1, but you still have problems with #2 and #3.

There is something called a Map, which lets you define a 'key' value you can use to lookup specific entries. It also grows as needed. However, it is a little more difficult to grasp as a concept than an Array. That's all.
 
Philip Stone
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay so my postcode class now let's you set the postcode and change the postcode, when you set the postcode it will bring up a message saying whether the postcode is valid or not. when you inital create the object and enter the postcode it doesn't seem to check so i need to just sort that out.

Now the commands inside the address class should be the same as the ones inside postcode or is it different.

As for the addressbook class i think currently all of the array list and the commands to add and delete the entries are in there so they just need moving across i think.


 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you definitely don't want to cut'n'paste the same code from your Postcode class (please tell me you named it Postcode and not postcode) inside your Address class - that would be silly. There is no reason to write the same code twice. The code inside Address should CALL the code of the Postcode class.

Why don't you post your code so we can all take a look at it? We probably should have done that a long time ago...Please when you post it use the 'code' tags. paste your source in, hi-light it, then clikc the 'code' button above. This will preserve your formatting and make it much easier for everyone to read.

 
Philip Stone
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Postcode class:


Address Class:



and so on with the other variables

 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So add the same methods for your Postcode



I do see a problem with your Postcode class. I can create a Postcode using its "public Postcode(String postcode)" constructor and pass it "This is not a valid Postcode". It will accept it and set it, even though we know it is not valid.

 
Philip Stone
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah i mentioned it previously, i've fixed that now so it works properly.

I also need the method get postcode i assume in the address class, i keep getting an error saying it finds postcode but expected java.lang.string which it is.

After this i think i can pretty much sort out addressbook and contact class

 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without seeing the code and the actual error message, it's virtually impossible to tell.

My guess is that your getPostcode gets a Postcode object (that's what I would expect). You then need to call that object's getpostcode to get a string.
 
Philip Stone
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thats what i have at the moment, wrong but i can't see why i thought it would follow like the set postcode method



 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What purpose does the Postcode object, postcode, serve passed as an argument to your method getPostcode()?


It's not used by the method. It almost looks like you're trying to write a toString() method, but it's incomplete.
 
Philip Stone
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So this is the current code in the address class, it compiles now but when you try and set the postcode i get an error - found java.lang.string but expected Postcode, the get postcode method seems to work fine but as i can't seem it set it i only return a null each time which is expected



Code in postcode class:

 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your parameter type for the set method and return type for the get method should be String.

You could consider dispensing with the set method and labelling the postcode field final. After all, the postcode for 123 High St will remain AB12 3CD for ever. [Not quite true ]

I would suggest you get the general design working and come back to set and get methods and that regex later.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I earlier wrote:Your parameter type for the set method and return type for the get method should be String. . . .

That is inside the Postcode class. In the address class, it would be of type Postcode.
 
Philip Stone
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so in the postcode class:


and address class:


are you suggesting to remove the set method from the address class?
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your getters don't need a parameter passed to them.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need a parameter for a get method.
More likely: . . . but having a field in the Postcode class called postcode can lead to confusion.
 
Philip Stone
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah i thought that and didn't have a parameter for the get method before i must of read something wrong and put it in for some reason. I've cleared it up now so it's less confusing with the postcode class and the type.

Still having this issue in my address class though when typing in a postcode
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Comment out the regular expression for the time being. Get it working without and trust yourself to enter correct postcodes.

You should never try to write an application. You write 5 lines, see whether they work, and then try 5 more lines. Much easier like that.
 
Philip Stone
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay i'll leave postcode and address class alone, just get the address class working like before and work on the contact class now.

For the contact class i just have the members:



then just need to have a createContact method i guess
 
Philip Stone
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just need some clarification when i'm going about this right

This is what i have so far for my contact class



And this is what i have so far for my addressbook class

 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would suggest your addContact method should have this heading: public void addContact(Contact c)

I suggest you get your Contact class working first. No, second. Start with the Name and Address classes. Design from centre to periphery, and code from the periphery towards the centre. Create a ContactDemo class, which tests the methods of the Contact class, and NameDemo, etc. Give the Contact class one constructor and one only, which matches the class' structure. The contact class has two fields, so give the constructor two parameters. You will need Name and Address classes too.
Don't do all this lot in one stage, but write one method and one only, then compile the code. Then test it. Then write your next method.Run it like this:Override the toString(), equals(java.lang.Object) and hashCode() methods in Name, and repeat the procedure.

Now do the same sort of thing for the Address and Postcode classes.>
 
Philip Stone
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'll really sorry i feel like an idiot but i don't understand what you mean :/
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Back to your Postcode class. Write it out (probably identical to your most recent version). Test it until you are sure it is working.Run it like this:The last is, believe it or not, a valid postcode

Once you have got that lots working, look for the 3rd chapter of Effective Javaâ„¢ by Joshua Bloch. You might find the older edition here. That will tell you about the hashCode, equals and toString methods. Consider whether you need to override them; notice Bloch says always to override toString. Then repeat the PostCodeDemo run, with the new version of Postcode.
You will notice an old post of mine with examples of hashCode and equals methods in; you can probably easily adapt those for the Postcode class.

Once you are happy that is all working correctly, same for the Address class and Name class. You will have more fields to access for the equals() and hashCode() methods, but you will find the principles the same.

You are putting the application together in little bits. Like building a house, one brick at a time. Only here the bits are different from one another, and need to be inspected individually.
 
Philip Stone
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks i'll have a read, are these methods essential for this to work then because this is the first i've seen any of this. Also nothing happens when you call the void main(String[]args) method from the postcodedemo
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you will find toString() very useful for displaying code; it will make testing your application very useful. And you will probably find toString() easy to override.
If you are going through your contacts, looking for matching names, you will need an equals() method, at least in the Name class.
 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What have you got in your main method?
What command line arguments are you passing? I gave a set of examples of command-line arguments for my examples, because they will produce no output without command-line input.
 
Philip Stone
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what i have at the moment:

Postcode class:



Name class:


Address class:


Contact class:

 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a simpler way to deal with nulls in an equals() method:Three additional comments about that:
  • 1: Because of the return, this will only work for the last statement in the method. You can join several :? expressions with &&, but you would have to enclose the :? expression in () for reasons of operator precedence.
  • 2: It is bad style to write if (...) return true; else return false;
  • 3: The "this." in that statement is redundant, and can be omitted.
  • A far better way to deal with nulls is not to let them into your code in the first place.Now, you see, you are not going to get any nulls into your Postcode class. You can do the same with other classes. Beware with addresses: there are some fields which may legitimately be empty, eg Grange Farm, Little Normanby. That is a valid address, but it has no street in. You will have to work out whether to use null or the empty String in those cases.
     
    Philip Stone
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Okay i'll look to changing, thanks

    I just need help now with my addressbook class and the text ui, I still have the text ui i was using at the start when it linked to one other class.

    This is what i believe should be in my addressbook class, the texui is underneath

    addressbook class:





    TextUi:

     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    And does it work? What happens when you use it? I haven't got the time to go through all your code.

    You realise you can read a char from the keyboard and use a switch-case block to choose the options? That might be better than the many if-elses. If you use Java7, you can even switch on the Strings input.
    I would suggest you need a parameter for the address book constructor, representing how many entries you will accept in your address book. Remember you need to initialise your Contact[] before you try to use it. You must also remember that array will contain null references until it is completely full. So you must be aware of them and make sure to avoid reading them.

    I presume you will reinstate the regular expression checking later, when you have the rest of the application working.
     
    Philip Stone
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I'm just trying to get the AddressBook class working, i only really need the menu in the text ui i think.

    The search by last name and print all contacts work fine but looking at the add entry which was taken from the old it doesn't link at all with the other classes, I really need help sorting this am getting a bit flustered as i only have a couple days left to finish, Please help

     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You are still not thinking object-oriented.

    When you are trying to add a Contact, you need to create a Contact object with all those data in, and add it to the array. Your Contact class ought to have Name and Address fields.
    So you create a new Address with the street and town and postcode (the latter via a Postcode object). Similarly you create a Name object. Now you create a Contact object with that Name and Address. Now you can add it to the array.
    And I would suggest your contact class should have a method like isSameName() or isSameLastName().
     
    Philip Stone
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hey,
    I have the majority of the program working now with all the classes.

    Currently working on the addEntry() method within the AddressTextUi.
    All the classes compile and i am able to show the menu, when you enter the right option it goes to add entry
    it then asks for each of the data variables to be entered forename, surename, street address so on. However once it gets to the end on the last input i get an error message

    "java.lang.String.NullPointerException: null"

    and it points to this

    AddressTextUi Class:






    Address Class:



     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The list array must be null at that point. Where do you initialise it?
     
    Philip Stone
    Greenhorn
    Posts: 19
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I believe i initalise the list array in my AddressBook class :



    How do i go about making it null?
     
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    That's the Contact[] list of AddressBook that you're initializing.

    However the error seems to be that the AddressBook[] list in AddressTextUi is null.

    Those are two different things even if they have the same variable name.
     
    Campbell Ritchie
    Marshal
    Posts: 79153
    377
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    When I said "must" I meant "must" in the sense that there is evidence that the list reference points to null. I didn't mean that it ought to point to null.

    Two different meanings of the word "must".
     
    reply
      Bookmark Topic Watch Topic
    • New Topic