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++; }