AddressBook

K

Kabal

Curious to thoughts on how to create an address book. Lets assume
first name, last name, and email address. Below is the code (very
basic) that I came up with. Anyone else have better, simplied or
whatever solution?

import java.util.ArrayList;
import java.util.Collections;

public class AddressBookTest {
public static void main(String[] args) {
AddressBook addressbook = new AddressBook();

addressbook.add(new Contact("test", "test",
"(e-mail address removed)"));
Contact johnDoe = new Contact("john", "doe",
"(e-mail address removed)");
addressbook.add(johnDoe);
addressbook.add(new Contact("test2", "test",
"(e-mail address removed)"));

Contact[] contacts = addressbook.contacts();
for (int i = 0; i < contacts.length; i++) {
System.out.println(contacts.toString());
}
System.out.println();

String search = "test";
boolean found = false;

contacts = addressbook.find(search);
for (int i = 0; i < contacts.length; i++) {
found = true;
System.out.println(contacts.toString());
}
if (!found) {
System.out.println("No contacts found using: " + search);
}
}
}

class AddressBook {
private static final int NOT_FOUND = -1; // indexOf tests
private ArrayList list = null;

public AddressBook() {
list = new ArrayList();
}

public void add(Contact contact) {
add(contact, true);
}

public void add(Contact contact, boolean checkForDup) {
boolean isDup = false;


// Found a duplicate entry
if (checkForDup && list.indexOf(contact) != NOT_FOUND) {
isDup = true;
}

// Don't add if duplicate entry was found
if (!isDup) {
list.add(contact);
}
}

public void remove(Contact contact) {
list.remove(contact);
}

public Contact get(Contact contact) {
int index = list.indexOf(contact);
return (index != NOT_FOUND ? (Contact) list.get(index) : new
Contact());
}

public Contact[] find(String find) {
ArrayList tmp = new ArrayList();
Contact[] contacts = contacts(list);

for (int i=contacts.length - 1; i >= 0; i--) {
Contact contact = contacts;

if (
contact.getFirstName().indexOf(find) != NOT_FOUND ||
contact.getLastName().indexOf(find) != NOT_FOUND ||
contact.getEmail().indexOf(find) != NOT_FOUND
) {
tmp.add(contact);
}
}

return contacts(tmp);
}

public Contact[] contacts() {
return contacts(list);
}

private Contact[] contacts(ArrayList list) {
Collections.sort(list);
return (Contact[]) list.toArray(new Contact[0]);
}
}

class Contact implements Comparable {
private String firstName, lastName, email;

public Contact(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}

public Contact() {
this("", "", "");
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = String.valueOf(firstName).trim();
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = String.valueOf(lastName).trim();
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = String.valueOf(email).trim();
}

public String toString() {
return String.valueOf(lastName + ", " + firstName + " [ " +
email + " ]");
}

public int compareTo(Object o) {
// Used for sorting
return toString().compareToIgnoreCase(o.toString());
}

public boolean equals(Object o) {
// Checking for duplicates
// Email should be unique
return email.equalsIgnoreCase(((Contact)o).getEmail());
}
}
 
I

Ike

Check out vCard/ vCal at http://www.imc.org/pdi/pdiproddev.html.

Whereas you wont get source code to do what you want, it WILL show you a
standard format used for address and appointments, so that what you create
can be imported and exported elsewhere. -Ike

Kabal said:
Curious to thoughts on how to create an address book. Lets assume
first name, last name, and email address. Below is the code (very
basic) that I came up with. Anyone else have better, simplied or
whatever solution?

import java.util.ArrayList;
import java.util.Collections;

public class AddressBookTest {
public static void main(String[] args) {
AddressBook addressbook = new AddressBook();

addressbook.add(new Contact("test", "test",
"(e-mail address removed)"));
Contact johnDoe = new Contact("john", "doe",
"(e-mail address removed)");
addressbook.add(johnDoe);
addressbook.add(new Contact("test2", "test",
"(e-mail address removed)"));

Contact[] contacts = addressbook.contacts();
for (int i = 0; i < contacts.length; i++) {
System.out.println(contacts.toString());
}
System.out.println();

String search = "test";
boolean found = false;

contacts = addressbook.find(search);
for (int i = 0; i < contacts.length; i++) {
found = true;
System.out.println(contacts.toString());
}
if (!found) {
System.out.println("No contacts found using: " + search);
}
}
}

class AddressBook {
private static final int NOT_FOUND = -1; // indexOf tests
private ArrayList list = null;

public AddressBook() {
list = new ArrayList();
}

public void add(Contact contact) {
add(contact, true);
}

public void add(Contact contact, boolean checkForDup) {
boolean isDup = false;


// Found a duplicate entry
if (checkForDup && list.indexOf(contact) != NOT_FOUND) {
isDup = true;
}

// Don't add if duplicate entry was found
if (!isDup) {
list.add(contact);
}
}

public void remove(Contact contact) {
list.remove(contact);
}

public Contact get(Contact contact) {
int index = list.indexOf(contact);
return (index != NOT_FOUND ? (Contact) list.get(index) : new
Contact());
}

public Contact[] find(String find) {
ArrayList tmp = new ArrayList();
Contact[] contacts = contacts(list);

for (int i=contacts.length - 1; i >= 0; i--) {
Contact contact = contacts;

if (
contact.getFirstName().indexOf(find) != NOT_FOUND ||
contact.getLastName().indexOf(find) != NOT_FOUND ||
contact.getEmail().indexOf(find) != NOT_FOUND
) {
tmp.add(contact);
}
}

return contacts(tmp);
}

public Contact[] contacts() {
return contacts(list);
}

private Contact[] contacts(ArrayList list) {
Collections.sort(list);
return (Contact[]) list.toArray(new Contact[0]);
}
}

class Contact implements Comparable {
private String firstName, lastName, email;

public Contact(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}

public Contact() {
this("", "", "");
}

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = String.valueOf(firstName).trim();
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = String.valueOf(lastName).trim();
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = String.valueOf(email).trim();
}

public String toString() {
return String.valueOf(lastName + ", " + firstName + " [ " +
email + " ]");
}

public int compareTo(Object o) {
// Used for sorting
return toString().compareToIgnoreCase(o.toString());
}

public boolean equals(Object o) {
// Checking for duplicates
// Email should be unique
return email.equalsIgnoreCase(((Contact)o).getEmail());
}
}
 
K

Kabal

Not really interested in the source, UI or in general a pre-existing
product. Just trying to see how people would go about creating a
simple addressbook type framework.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top