Sahil-Pattni
2/21/2017 - 1:20 AM

Lab | Address Book

Lab | Address Book

import java.util.Scanner;

/**
 * The AddressBookClient simulates the functionality of the AddressBook.
 */
public class AddressBookClient {

    /**
     * Command line interface for simulating the AddressBook.
     *
     * @param args Unused
     */
    public static void main(String[] args) {
        AddressBook ab = new AddressBook();
        Scanner s = new Scanner(System.in);
        Contact c;
        String name, email;
        boolean added;
        long number;
        int selection = 0;

        System.out.println("Welcome to the Address Book!");

        do {
            System.out.println("Select an option from the menu below:");
            System.out.println("1 - Add a new Contact");
            System.out.println("2 - Remove an existing Contact");
            System.out.println("3 - Get details about a Contact");
            System.out.println("4 - Print Address Book");
            System.out.println("5 - Exit");
            selection = s.nextInt();

            if (selection == 1) {
                int subSelection = 0;
                do {
                    System.out.println("Select an option below: ");
                    System.out.println("1 - New Contact with name");
                    System.out.println("2 - New Contact with name and number");
                    System.out.println("3 - New Contact with name, number, and address");
                    subSelection = s.nextInt();
                } while (subSelection < 1 && subSelection > 3);
                s.nextLine();
                System.out.println("Enter the name of the Contact to add/update: ");
                name = s.nextLine();
                if (subSelection == 1) {
                    added = ab.addContact(new Contact(name));
                } else if (subSelection == 2) {
                    System.out.println("Enter the number of the Contact: ");
                    number = s.nextLong();
                    added = ab.addContact(new Contact(name, number));
                } else {
                    System.out.println("Enter the number of the Contact: ");
                    number = s.nextLong();
                    System.out.println("Enter the address of the Contact: ");
                    s.nextLine();
                    email = s.nextLine();
                    added = ab.addContact(new Contact(name, number, email));
                }
                if (added) {
                    System.out.printf("%s was added to the Address Book.%n", name);
                } else {
                    System.out.printf("Information for existing Contact %s updated.%n", name);
                }
            } else if (selection == 2) {
                System.out.println("Enter the name of the Contact you would like to remove:");
                s.nextLine();
                c = new Contact(s.nextLine());
                if (ab.removeContact(c)) {
                    System.out.printf("%s remove from the Address Book.%n", c.getName());
                } else {
                    System.out.printf("%s was not in the Address Book.%n", c.getName());
                }
            } else if (selection == 3) {
                System.out.println("Enter the name of the Contact:");
                s.nextLine();
                name = s.nextLine();
                c = ab.getContactByName(name);
                if (c != null) {
                    System.out.printf("Name: %s%n", c.getName());
                    System.out.printf("Number: %d%n", c.getNumber());
                    System.out.printf("Address: %s%n", c.getAddress());
                } else {
                    System.out.printf("%s was not found in the Address Book.%n", name);
                }
            } else if (selection == 4) {
                ab.printAddressBook();
            } else if (selection != 5) {
                System.out.println("Invalid selection, try again.");
            }
            System.out.println();
        } while (selection != 5);

        s.close();
    }
}
import com.sun.deploy.util.ArrayUtil;

import java.util.Arrays;

public class AddressBook
{
    private int totalContacts;
    private int maxContacts;
    private Contact[] contacts;

    public AddressBook()
    {
        maxContacts = 10;
        contacts = new Contact[maxContacts];
    }
    public boolean addContact(Contact contact)
    {
        boolean wasAdded = false;
        boolean wasUpdated = false;

        if (totalContacts > 0)
        {
            for (int i = 0; i < totalContacts; i++)
            {
                if (contacts[i].equals(contact))
                {
                    wasUpdated = true;
                    wasAdded = false;
                    if (contact.getNumber()!=0)
                        contacts[i].setNumber(contact.getNumber());
                    if (contact.getAddress() != null)
                        contacts[i].setAddress(contact.getAddress());
                    break;
                }
            }
            if (!wasUpdated)
            {
                if (totalContacts + 1 == maxContacts)
                {
                    maxContacts *=2;
                    this.contacts = Arrays.copyOf(contacts, maxContacts);
                }
                //Adding the contact
                contacts[totalContacts] = contact;
                totalContacts++;
                wasAdded = true;
            }
        }
        else
        {
            contacts[totalContacts] = contact;
            totalContacts++;
            wasAdded = true;
        }

        return wasAdded;
    }

    public boolean removeContact(Contact contact) {
        boolean wasRemoved = false;
        for (int i = 0; i < totalContacts; i++)
        {
            if (contacts[i].equals(contact)) {
                totalContacts--;
                for (int l = i+1; l < totalContacts+2; l++)
                {
                    contacts[l-1] = contacts[l];
                    contacts[l] = null;
                }
                wasRemoved = true;

                break;
            }
        }
        return wasRemoved;
    }

    public Contact getContactByName(String name) {
        int idPoint = -1;
        for (int i = 0; i < totalContacts; i++)
        {
            if (contacts[i].getName().equals(name))
            {
                idPoint = i;
                break;
            }
        }

        if (idPoint >= 0)
            return contacts[idPoint];
        else
            return null;
    }

    public boolean contains(Contact contact) {
        for (Contact contact1 : contacts) {
            if (contact1.equals(contact))
                return true;
        }

        return false;
    }

    public void printAddressBook() {
        for (int i = 0; i < this.totalContacts; i++) {
            System.out.printf("Name: %s%n", this.contacts[i].getName());
            System.out.printf("Number: %d%n", this.contacts[i].getNumber());
            System.out.printf("Address: %s%n%n", this.contacts[i].getAddress());
        }
    }

}
/**
 * Created by Sahil Pattni on 18-Feb-17.
 */
public class Contact
{
    private String name;
    private long number;
    private String address;

    public Contact(String name){this.name = name;}
    public Contact(String name, long number)
    {
        this.name = name;this.number = number;
    }
    public Contact(String name, long number, String address)
    {
        this.name = name;
        this.number = number;
        this.address = address;
    }
    public void setName(String name){this.name = name;}
    public String getName(){return this.name;}
    public void setNumber(long number){this.number = number;}
    public long getNumber(){return this.number;}
    public void setAddress(String address){this.address = address;}
    public String getAddress(){return this.address;}
    public boolean equals(Contact contact)
    {
        return name.equals(contact.name);
    }
    public static void main(String[] args) {
        Contact c1 = new Contact("Jeff", 5044544, "ab");
        Contact c2 = new Contact("Jeff", 5044544,"ab");

        System.out.println(c1.equals(c2));
    }

}