Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 9 123 ... LastLast
Results 1 to 25 of 209

Thread: Address Book Program Issues

  1. #1
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Address Book Program Issues

    I am creating an Address Book, these are the requirements

    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.

    I have created the name class that holds all the name variables, gets them and sets them.
    I have created the postcode class which sets and gets the postcode and tests to check it is valid
    The postcode class is linked into the address class which holds the rest of the address information.
    The contact class is linked to the address class and the name class. It extends the name class and links to the contact.

    I have begun working on the addressbook class but am having some issues. Initially when i created this i used a textui class and linked that directly to one single class that held the name and address.

    My thought is that the addressbook class will hold the contact class as an array and have methods to create an entry, delete an entry, search entries by last name and list all the entries, while the textui just displays the menu and links to the addressbook class. I have my list all entries and search entries by last name working but i am struggling with the add contacts as i don't know how to link it.

    Any help be much appreciated

    Contact Class:

    ublic class Contact extends Name
    {
        private Address address; 
        private Name name;
     
        public Contact()
        {
            super();
            this.address = new Address();
        }
     
     
        public Contact(String first, String middle, String last)
        {
            super(first, middle, last);
            this.address = new Address();
        }
     
        public Contact(String first, String middle, String last, Address address)
        {
            super(first, middle, last);
            this.address = new Address();
     
        }
     
        public Address getAddress()
        {
            return address;
        }
     
        public void setAddress(Address address) 
        {
    		this.address = address;
    	}
     
     
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Contact other = (Contact) obj;
            if (address == null) {
                if (other.address != null)
                    return false;
            } else if (!address.equals(other.address))
                return false;
     
            if (name == null) {
                if (other.name != null)
                    return false;
            } else if (!name.equals(other.name))
                return false;
            return true;
        }
     
        @Override
        public String toString() {
            return super.toString() + "\n" + address + "\n" + name + "\n";
        }
     
        public Contact copy() {
            Contact person = new Contact();
            person.changeName(this.getFirstName(), this.getMiddleName(), this.getLastName());
            person.setAddress(this.getAddress());
            return person;
        }
    }

    Address Class:

    import java.io.*;
        import java.util.Scanner;
        public class AddressBook {  
                //index of the last entry
                private int top = 0;
                //constant number that indicates the maximum
                //number of entries in the address book
                private static final int MAXENTRIES = 10;
                //array of Address Book Entries
                private Contact[] list;
     
            /**
             * Constructor - creates a new, empty address book
             */    
            public AddressBook()
            {
                list = new Contact[MAXENTRIES];
            }
     
                /**
                 * Add a new contact to the collection
                 */
                public void addEntry()
                {
                    BufferedReader keyIn = new BufferedReader(new InputStreamReader(System.in));
                    String first = "";
                    String second = "";
                    String last = "";
                    String street ="";
                    String city = "";
                    String county = "";
                    String country = "";
                    String post = "";
                    if(top == MAXENTRIES){
                        System.out.println("Address Book is full");
                        return;
                    }
                    //asks the user for the data of the address book
                    try{
                        System.out.print("First Name: ");
                        first = keyIn.readLine();
                        System.out.print("Second Name: ");
                        second = keyIn.readLine();
                        System.out.print("Last Name: ");
                        last = keyIn.readLine();
                        System.out.print("Street: ");
                        street = keyIn.readLine();
                        System.out.print("City: ");
                        city = keyIn.readLine();
                        System.out.print("County: ");
                        county = keyIn.readLine();
                        System.out.print("Country: ");
                        country = keyIn.readLine();
                        System.out.print("Postcode: ");
                        post = keyIn.readLine();
     
                    }catch(Exception e){
                        System.out.println(e);
                        System.exit(0);
                    }
                    Contact entry = new Contact();
                    list[top] = entry;
                    top++;
            }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,583
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Address Book Program Issues

    am struggling with the add contacts as i don't know how to link it.
    Are you referring to the addEntry() method?
    What do you mean by "how to link it"?

  3. The Following User Says Thank You to Norm For This Useful Post:

    Gamb1t (August 25th, 2011)

  4. #3
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    Yeah i'm talking about the addEntry() method, Well the variables all need to come all the other classes and get stored in the contact class i'm not to sure if this is doing it.
    I'm not that great with this and been struggling for a while so i'm always unsure :/

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,583
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Address Book Program Issues

    the variables all need to come all the other classes and get stored in the contact class
    What variables are you talking about here?
    It looks like the addEntry method gets its values from the user's input.
    You should use all the data values that the user enters in the call to the Contact class's constructor. Your call to the constructor doesn't use any of the values that the user entered.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Gamb1t (August 25th, 2011)

  7. #5
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    Yeah your right, the values need to be user input but need to be stored within the contact class. I'm not sure how to do this in the addEntry() method though

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,583
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Address Book Program Issues

    Look at the Contact class's constructors. There are several and you could add more if you need them.
    Use one of them with all the values that the constructor needs to build a Contact object.
    Something like this:
    AClass anObj = new AClass(arg1, value2, parm3, andMore, AndMore);

  9. The Following User Says Thank You to Norm For This Useful Post:

    Gamb1t (August 25th, 2011)

  10. #7
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    So are you saying i need to have

    Address address = new Address(Address Street, Address city, ....);

    and one for the name inside of one constructor and get rid of the others.

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,583
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Address Book Program Issues

    Something like that. You should pass ALL the data you need to define the class in the constructor.

  12. The Following User Says Thank You to Norm For This Useful Post:

    Gamb1t (August 25th, 2011)

  13. #9
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    Yeah how does it work with contact class extending name class

    i have this at the moment

    public class Contact extends Name
    {
        private Address address;   
        private Name name;  
     
        public Contact()  
        {  
            super();
            Address address = new Address(Address Street, Address city, Address county, Address country, Postcode post);
        }

  14. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,583
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Address Book Program Issues

    Why does the Contact class extend the Name class?
    Where does the Contact class get the data to create the Address object?
    There should be a constructor for the Contact class that takes a name and Address as arguments.

    The syntax of the call to the Address constructor is wrong. You don't put datatype names in the call. Datatype names are used in the definition of the constructor or method.
    Why are you creating an Address object in the Contact class? The Address object should be created elsewhere and given to the Contact class either in its constructor or via a setAddress method.

  15. The Following User Says Thank You to Norm For This Useful Post:

    Gamb1t (August 25th, 2011)

  16. #11
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    I don't know why it was extending the name class i'm changed that now.
    I understand what your saying i just haven't a clue how to do it thats all, it's been a while since i have done this and i haven't time to sit down and read it all again i need to finish this and then after i can go back and work it all out.

  17. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,583
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Address Book Program Issues

    i need to finish this and then after i can go back
    That's doing it backwards.
    You need to understand how to create classes and use them.
    Take the list of classes you are supposed to create and start at the top.
    Which ones have you defined?
    What is the next one you are working on?

  18. The Following User Says Thank You to Norm For This Useful Post:

    Gamb1t (August 25th, 2011)

  19. #13
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    Okay first class it asks me to create is a name class - to hold the family name and others

    Name class:

    public class Name
    {
        private String forename;
        private String surname;
     
        /**
         * Default Constructor
         */
        public Name ()
        {
            forename = "";
            surname = "";
        }
     
        public Name (String first, String middle, String last)
        {
            changeName(first, last);
        }
     
     
        /**
         *  changes all variables of name
         */ 
        public void changeName(String first, String last)
        {
           forename = first;
           surname = last;
        }
     
        /**
         * returns the variable first name
         */
        public String getForename()
        {
           return forename;
        }
        /**
         * returns the variable last name
         */
        public String getSurname()
        {
           return surname;
        }
     
        @Override
    	public boolean equals(Object obj) {
    		if (this == obj)
    			return true;
     
    			if (obj == null)
    			return false;
     
    			if (getClass() != obj.getClass())
    			return false;
     
    			Name other = (Name) obj;
     
    			if (forename == null) {
    			if (other.forename != null)
    				return false;
    		} else if (!forename.equals(other.forename))
    			return false;
     
     
    				if (surname == null) {
    			if (other.surname != null)
    				return false;
    		} else if (!surname.equals(other.surname))
    			return false;
     
    		return true;
    	}
     
        @Override
    	public String toString() {
    		return String.format(
    				"Forename: %s,  Surname: %s", forename, surname);
    	}
     
    }

    2nd class to create is an address class - to hold address details as a string together with a postcode
    3rd class is postcode class which holds typical British postcodes of certain format.

    Postcode class:
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class Postcode
    {
        private String post;
     
        public Postcode(String post)
        {
           this.post = post;        
        }
     
        public void checkValidation()
        {
            String regexp="^([A-PR-UWYZ](([0-9](([0-9]|[A-HJKSTUW])?)?)|([A-HK-Y][0-9]([0-9]|[ABEHMNPRVWXY])?)) [0-9][ABD-HJLNP-UW-Z]{2})$";
     
             Pattern pattern = Pattern.compile(regexp);
     
             Matcher matcher = pattern.matcher(post.toUpperCase());
     
            if (matcher.matches()) 
            {
                System.out.println("This is a valid UK Postcode.");
            } 
            else 
            {
                System.out.println("This is not a valid UK Postcode.");
            }
     
        }
     
        public String getPost() 
        {
            return post;
        }
     
        public void setPost(String post) 
        {
            this.post = post; 
        }
     
        public String toString()
        {
           return (post);
        }
     
    }

    And Address class:

    public class Address
    {
        private String street;
        private String city;
        private String county;
        private String country;
        private Postcode post;
     
        public Address()
        {
        }
     
        public Address(String street, String city, String county, String country, Postcode post)
        {
            this.street = street;
            this.city = city;
            this.county = county;
            this.country = country;
            this.post = post;
        }
     
     
     
        public String getStreet() 
        {
            return street;
        }
        public void setStreet(String street) 
        {
            this.street = street;
        }
     
     
        public String getCity() 
        {
            return city;
        }
        public void setCity(String city) 
        {
            this.city = city;
        }
     
     
     
        public String getCounty() 
        {
            return county;
        }
        public void setCounty(String county) 
        {
            this.county = county;
        }
     
     
     
         public String getCountry() 
        {
            return country;
        }
        public void setCountry(String country) 
        {
            this.country = country;
        }
     
     
     
        public void setPost(Postcode post)  
        {  
            this.post = post;  
        }
        public Postcode getPost()
        {
            return post;
        }
     
     
     
        @Override
    	public boolean equals(Object obj) {
    		if (this == obj)
    			return true;
     
    			if (obj == null)
    			return false;
     
    			if (getClass() != obj.getClass())
    			return false;
     
    			Address other = (Address) obj;
     
    			if (city == null) {
    			if (other.city != null)
    				return false;
    		} else if (!city.equals(other.city))
    			return false;
     
    			if (county == null) {
    			if (other.county != null)
    				return false;
    		} else if (!county.equals(other.county))
    			return false;
     
    				if (country == null) {
    			if (other.country != null)
    				return false;
    		} else if (!country.equals(other.country))
    			return false;
     
    			if (street == null) {
    			if (other.street != null)
    				return false;
    		} else if (!street.equals(other.street))
    			return false;
     
    			if (post == null) {
    			if (other.post != null)
    				return false;
    		} else if (!post.equals(other.post))
    			return false;
     
    		return true;
    	}
     
        @Override
    	public String toString() {
    		return String.format(
    				"Street Address: %s, City: %s, County: %s, Country %s, Postcode: %s", street, city, county, country, post);
    	}
     
    	public Address copy() {
    		Address address = new Address();
    		address.setStreet(this.getStreet());
    		address.setCity(this.getCity());
    		address.setCounty(this.getCounty());
    		address.setCountry(this.getCountry());
    		address.setPost(this.getPost());
    		return address;
    	}
     
        public void print() {
            System.out.println(this);
        }
     
    }

    Now working on my contact class which holds a name and an address.

  20. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,583
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Address Book Program Issues

    Those classes look normal. One thing I see:
      public Name (String first, String middle, String last)     {
    You ignore the middle name.

  21. The Following User Says Thank You to Norm For This Useful Post:

    Gamb1t (August 25th, 2011)

  22. #15
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    Ah yeah i had the middle name in to begin with then took it out and obviously left that in.
    So all that seems okay then?

  23. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,583
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Address Book Program Issues

    It looks reasonable.

  24. The Following User Says Thank You to Norm For This Useful Post:

    Gamb1t (August 25th, 2011)

  25. #17
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    Hah well i can live with that .
    So it's onto this contact class. I understand the principle of whats needed to happen i just can't work it out with the code.

    I need the contact class linked to the other classes so i have as follows, the address class links through to the postcode class so thats fine. Do i have another variable which then holds the combination of those two classes.

    private Address address;
    private Name name;

  26. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,583
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Address Book Program Issues

    The Contact class holds references to a Name and to an Address.
    It doesn't have to create those objects. It just takes care of storing and updating the reference variables that point to the objects.

  27. The Following User Says Thank You to Norm For This Useful Post:

    Gamb1t (August 25th, 2011)

  28. #19
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    Okay so having

    private Address address;
    private Name name;

    enables the contact class to reference those classes ?

  29. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,583
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Address Book Program Issues

    Where are those variables defined?
    enables the contact class to reference those classes ?
    Are you talking about a problem compiling a class that uses those classes?
    What do you mean by "reference" those classes? How are those classes any different from the String class?
    Each of those classes needs to have methods that allow another class to work with their contents.

  30. The Following User Says Thank You to Norm For This Useful Post:

    Gamb1t (August 25th, 2011)

  31. #21
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    This what i have currently, you mentioned that i need to store and update reference variables that point to the objects

    public class Contact
    {
        private Address address;   
        private Name name;  
     
        public Contact()  
        {  
            this.address = address;
            this.name = name;
        }  
     
        public void setName(Name name)  
        {  
            this.name = name;  
        }
        public Name getName()
        {
            return name;
        }
     
        public void setAddress(Address address)  
        {  
            this.address = address;  
        }
        public Address getAddress()
        {
            return address;
        }  
     
    }

  32. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,583
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Address Book Program Issues

    Your constructor needs parameters. Look at your other classes. They all had constructors with parameters.

  33. The Following User Says Thank You to Norm For This Useful Post:

    Gamb1t (August 25th, 2011)

  34. #23
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    Yeah this is where i'm usure as i would do this but then when you invoke it it just asks for the address and the name obviously and not the individual variables or is this still correct
     public Contact(Address address, Name name)  
        {  
            this.address = address;
            this.name = name;
        }

  35. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,583
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

    Default Re: Address Book Program Issues

    Yes, that looks ok.

  36. The Following User Says Thank You to Norm For This Useful Post:

    Gamb1t (August 25th, 2011)

  37. #25
    Member
    Join Date
    Aug 2011
    Posts
    106
    Thanks
    81
    Thanked 0 Times in 0 Posts

    Default Re: Address Book Program Issues

    Okay so now i've added my overides too

    @Override  
        public boolean equals(Object obj) {  
            if (this == obj)  
                return true;  
            if (obj == null)  
                return false;  
            if (getClass() != obj.getClass())  
                return false;  
            Contact other = (Contact) obj;  
            if (address == null) {  
                if (other.address != null)  
                    return false;  
            } else if (!address.equals(other.address))  
                return false;  
     
            if (name == null) {  
                if (other.name != null)  
                    return false;  
            } else if (!name.equals(other.name))  
                return false;  
            return true;  
        }  
     
        @Override  
        public String toString() {  
            return super.toString() + "\n" + address + "\n" + name + "\n";  
        }

    Having issues with my Contact copy(), the first line it says it expects ')'

    public Contact copy() {  
            Contact person = new Contact(Address address, Name name);  
            person.changeName(this.getForename(), this.getSurname());  
            person.setAddress(this.getAddress());  
            return person;  
        }

Page 1 of 9 123 ... LastLast

Similar Threads

  1. Source code for Email address book/contacts importer
    By jega004 in forum Java Theory & Questions
    Replies: 4
    Last Post: November 23rd, 2012, 11:49 AM
  2. Issues with ascending number program
    By newtojava2011 in forum What's Wrong With My Code?
    Replies: 21
    Last Post: June 30th, 2011, 06:23 PM
  3. Address Book Help
    By clevel211 in forum Object Oriented Programming
    Replies: 4
    Last Post: November 21st, 2010, 08:42 PM
  4. How to get your computer name and IP address?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 6
    Last Post: November 11th, 2010, 04:06 AM
  5. Array of contacts (address book)
    By smush in forum Collections and Generics
    Replies: 11
    Last Post: April 29th, 2010, 03:08 AM