David Needham
David Needham

Reputation: 119

Address book program in Java

I'm just doing a small program. It's an address book which has four options:

  1. insert new contact
  2. search contact by last name
  3. delete contact by name
  4. show all contacts
  5. exit program

Just wondering how to get the insert contact part and how to store it. I've hardcoded one contact to test it.

Here is my code I have started

package addressbook;
import java.util.Scanner;

public class addressbooks
{


    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);

        //create a table to hold information

        String[][] addressbooks = new String[100][8];

        addressbooks[0][0]="Mobile Number";
        addressbooks[0][1]="First Name";
        addressbooks[0][2]="Last Name";
        addressbooks[0][3]="Address";
        addressbooks[0][4]="City";
        addressbooks[0][5]="County";
        addressbooks[0][7]="Telephone Number";

        //pre-populate address book for testing purposes and records

        addressbooks[1][0]="1";
        addressbooks[1][1]="David";
        addressbooks[1][2]="Needham";
        addressbooks[1][3]="Sraheens, Achill";
        addressbooks[1][4]="Galway";
        addressbooks[1][5]="Mayo";
        addressbooks[1][6]="086-1581077";
        addressbooks[1][7]="098-45368";

        addressbooks[2][0]="2";
        addressbooks[2][1]="Mc";
        addressbooks[2][2]="lovin";
        addressbooks[2][3]="Hawaii";
        addressbooks[2][4]="Hawaii";
        addressbooks[2][5]="Hawaii";
        addressbooks[2][6]="12345";
        addressbooks[2][7]="412-555-1234";

        //menu options
        System.out.print("Welcome to my Address book!");
        System.out.print("\n");
        System.out.print("\n1 - Insert a New Contact \n2 - Search Contact by Last Name \n3 - Delete Contact \n4 - Show All Contacts \n5 - Exit " );
        System.out.print("\n");
        System.out.print("\nChoose your option: ");

        int option = input.nextInt();

        if (option ==1)
        {
            System.out.print("\nPlease enter your First Name : ");
        }
        if (option ==2)
        {
        }

        if (option ==3)
        {
        }

        if (option ==4)
        {
        System.out.println(addressbooks[1][0]+
        "\t"+addressbooks[1][2]+ ", "+addressbooks[1][1]+
        "\n\t"+addressbooks[1][3]+
        "\n\t"+addressbooks[1][4]+ ", "+addressbooks[1][5]+ " "+addressbooks[1][6]+
        "\n\t"+addressbooks[1][7]);
        }

        if (option ==5)
        {
        }

    }
}

Upvotes: 0

Views: 24180

Answers (3)

duffymo
duffymo

Reputation: 308858

I'd start like this:

package model;

public class Person {
    private String firstName;
    private String lastName;
    // What else means something to your problem?  Birthday?
    // Constructors, getters (make them immutable), equals/hashCode
}

public class Address {
    private String street;
    private String city;
    private String county;
    private String postalCode;
    // Constructors, getters (make them immutable), equals/hashCode
}

public class AddressBook {
    private Map<Person, Address> contacts = new ConcurrentHashMap<Person, Address>();

    public void addContact(Person p, Address a) {
        this.contacts.put(p, a);
    }

    public void removeContact(Person p) {
        this.contacts.remove(p);
    }

    public Collection<Person> findAllContacts() {
        return new Collections.unmodifiableCollection(this.contacts.keySet());
    }

    public boolean hasContact(Person p) {
        return this.contacts.contains(p);
    }
    // etc.
}

I would recommend separating all that stuff about text-based IO out away from the fundamentals of your problem. If you get it right, the next step is to write a web UI. Most of the code will be reusable if you do it correctly.

Think about layering your app:

view->services->persistence

Model classes might be used in all three layers.

The answer recommending JDBC isn't wrong. If you write your service and persistence classes as interfaces, you'll find it easy to swap out an in-memory Map of Contacts for a database version that uses JDBC:

package persistence;

public interface ContactDao {
    Collection<Contact> find();
    Contact find(Long id);
    Collection<Contact> find(String lastName);
    Collection<Contact> find(Address address);
    Long save(Contact c);
    void update(Contact c);
    void delete(Contact c);
}

There are lots of ways to do things. I've already changed my mind: I've introduced a Contact class:

package model; 

public class Contact {
    private Person;
    private Address;
}

Upvotes: 3

daniel gratzer
daniel gratzer

Reputation: 53891

I actually created a similar program, I would recommend simply constructing a database, this also allows you to save data in between uses of the program. Using JBDC is quite simple, here is the site i used: http://www.zentus.com/sqlitejdbc/

Upvotes: 0

Woot4Moo
Woot4Moo

Reputation: 24336

The use of objects will greatly help you here:

class ADdressBook  
{  
   List<Contact> contacts;  

   function addContact(Contact contact)  
   {
         contacts.add(contact);  
   }    
}

You will want to use the methods that are exposed as part of the List/Collection API to make this easier on you. Implementing the Contact class is an exercise for you.

Upvotes: 1

Related Questions