-
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:
Code :
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:
Code :
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++;
}
-
Re: Address Book Program Issues
Quote:
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"?
-
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 :/
-
Re: Address Book Program Issues
Quote:
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.
-
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
-
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);
-
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.
-
Re: Address Book Program Issues
Something like that. You should pass ALL the data you need to define the class in the constructor.
-
Re: Address Book Program Issues
Yeah how does it work with contact class extending name class
i have this at the moment
Code :
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);
}
-
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.
-
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.
-
Re: Address Book Program Issues
Quote:
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?
-
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:
Code :
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:
Code :
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:
Code :
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.
-
Re: Address Book Program Issues
Those classes look normal. One thing I see:
Code :
public Name (String first, String middle, String last) {
You ignore the middle name.
-
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?
-
Re: Address Book Program Issues
-
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;
-
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.
-
Re: Address Book Program Issues
Okay so having
private Address address;
private Name name;
enables the contact class to reference those classes ?
-
Re: Address Book Program Issues
Where are those variables defined?
Quote:
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.
-
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
Code :
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;
}
}
-
Re: Address Book Program Issues
Your constructor needs parameters. Look at your other classes. They all had constructors with parameters.
-
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
Code :
public Contact(Address address, Name name)
{
this.address = address;
this.name = name;
}
-
Re: Address Book Program Issues
-
Re: Address Book Program Issues
Okay so now i've added my overides too
Code :
@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 ')'
Code :
public Contact copy() {
Contact person = new Contact(Address address, Name name);
person.changeName(this.getForename(), this.getSurname());
person.setAddress(this.getAddress());
return person;
}