Alexis Acosta
Alexis Acosta

Reputation: 165

Elements not being added to my Array Lists

I'm doing a challenge where I need to create simple functionality for a mobile phone. The program should:

  1. Print a list of contacts
  2. Add a new contact
  3. Modify an existing contact
  4. Remove a contact
  5. Allow the user to search for a contact
  6. Allow the user to quit the program.

The only part that isn't working for me is adding a new contact. I believe the problem is in the Mobile Phone class within the addContacts() method. When I call contacts.addName(scanner.nextLine()); and contacts.addPhoneNumber(scanner.nextLine()); the value that is being typed into the keyboard is for some reason not being added to the "names" Array List and the "phoneNumber" Array List which I initialized in the Contacts class. The code runs and prompts the user to enter the name and phone number of the contact to be added. However, when the user asks for the list of the contacts the program always prints "zero names and zero phone numbers." Even if the users just added a new contact with name and phone number. I would really appreciate any help, here is my code.

Main Class:

import java.util.Scanner;
public class Main {

  private static Scanner scanner  = new Scanner(System.in);
  private static Contacts contacts = new Contacts();
  private static MobilePhone phone = new MobilePhone();

  public static void main(String[] args) {
    boolean quit = false;
    int option = 0;
    printInstructions();
    while(!quit) {
      System.out.println("Enter the number of the option you want to execute: ");
      option = scanner.nextInt();
      scanner.nextLine();

      switch(option) {
        case 0:
          printInstructions();
          break;
        case 1:
          contacts.printContacts();
          break;
        case 2:
          phone.addContacts();
          break;
        case 3:
          phone.modifyContacts();
          break;
        case 4:
         phone.removeContact();
         break;
        case 5:
          phone.searchForContact();
          break;
        case 6:
          quit = true;
          break;
      }
    }
  }

  public static void printInstructions() {
    System.out.println("\nPress");
    System.out.println("\t 0 - To print options");
    System.out.println("\t 1 - To print the list of contacts");
    System.out.println("\t 2 - To add a new contact");
    System.out.println("\t 3 - To modify an existing contact");
    System.out.println("\t 4 - To remove a contact");
    System.out.println("\t 5 - To search for a contact");
    System.out.println("\t 6 - To exit the application");
  }
}

Mobile Phone Class:

import java.util.Scanner;
public class MobilePhone {
  private Scanner scanner = new Scanner(System.in);
  private Contacts contacts = new Contacts();

  public void addContacts() {
    System.out.print("Enter the name for the contact: ");
    contacts.addName(scanner.nextLine());

    System.out.print("Enter the phone number for the contact: ");
    contacts.addPhoneNumber(scanner.nextLine());
  }

  public void modifyContacts() {
    System.out.println("Enter current contact name: ");
    String contactName = scanner.nextLine();
    System.out.println("Enter replacement contact name: ");
    String newContactName = scanner.nextLine();
    contacts.modifyName(contactName, newContactName);

    System.out.println("Enter current contact phone number: ");
    String phoneNumber = scanner.nextLine();
    System.out.println("Enter replacement phone number: ");
    String newPhoneNumber = scanner.nextLine();
    contacts.modifyPhoneNumber(phoneNumber, newPhoneNumber);
  }

  public void removeContact() {
    System.out.println("Enter contact name: ");
    String contactName = scanner.nextLine();
    System.out.println("Enter phone number: ");
    String phoneNumber = scanner.nextLine();
    contacts.removeContacts(contactName, phoneNumber);
  }

  public void searchForContact() {
    System.out.println("Enter contact name to search for: ");
    String searchName = scanner.nextLine();
    System.out.println("Enter contact phone number to search for: ");
    String searchPhoneNumber = scanner.nextLine();
    if(contacts.onFile(searchName, searchPhoneNumber)) {
      System.out.println("Found " + searchName + " and their phone number " + searchPhoneNumber);
    } else {
      System.out.println("Did not find that contact, or name and phone number do not match");
    }
  }
}

Contacts Class:

import java.util.ArrayList;
public class Contacts {
  private ArrayList<String> names = new ArrayList<String>();
  private ArrayList<String> phoneNumbers = new ArrayList<String>();

  public void addName(String name) {
    names.add(name);
  }

  public void addPhoneNumber(String phoneNumber) {
    phoneNumbers.add(phoneNumber);
  }

  public void printContacts() {
    System.out.println("You have " + names.size() + " names and " + phoneNumbers.size() +
    " phone numbers in your contacts.");
    for(int i=0; i<names.size(); i++) {
      System.out.println((i+1) + ". " + names.get(i));
    }

    for(int j=0; j<phoneNumbers.size(); j++) {
      System.out.println((j+1) + ". " + phoneNumbers.get(j));
    }
  }

  public void removeContacts(String name, String phoneNumber) {
    int positionOfName = findName(name);
    int positionofPhoneNumber = findPhoneNumber(phoneNumber);
    if(positionOfName >= 0 && positionofPhoneNumber >= 0) {
      removeName(positionOfName);
      removePhoneNumber(positionofPhoneNumber);
    }
  }

  private void removeName(int position) {
    names.remove(position);
  }

  private void removePhoneNumber(int position) {
    phoneNumbers.remove(position);
  }

  private int findName(String name) {
    return names.indexOf(name);
  }

  private int findPhoneNumber(String phoneNumber) {
    return phoneNumbers.indexOf(phoneNumber);
  }

  public void modifyName(String currentName, String newName) {
    int position = findName(currentName);
    if(position >= 0) {
      modifyName(position, newName);
    }
  }

  private void modifyName(int position, String newName) {
    names.set(position, newName);
    System.out.println("Name " + (position+1) + " has been modified.");
  }

  public void modifyPhoneNumber(String currentNumber, String newNumber) {
    int position = findPhoneNumber(currentNumber);
    if(position >= 0) {
      modifyPhoneNumber(position, newNumber);
    }
  }

  private void modifyPhoneNumber(int position, String newNumber) {
    phoneNumbers.set(position, newNumber);
    System.out.println("Phone number " + (position+1) + " has been modified.");
  }

  public boolean onFile(String searchName, String searchNumber) {
    int positionName = findName(searchName);
    int positionPhoneNumber = findPhoneNumber(searchNumber);
    if(positionName >= 0 && positionPhoneNumber >= 0) {
      return true;
    } else {
      return false;
    }
  }
}

Upvotes: 0

Views: 72

Answers (1)

sidgate
sidgate

Reputation: 15244

You have two different objects of Contacts defined, one in Main class and another in MobilePhone. You need to reuse the same object while adding and printing. You can pass Contacts to MobilePhone constructor

public MobilePhone(Contacts contacts) {
    this.contacts = contacts;
}

and pass it from the Main class

private static Contacts contacts = new Contacts();
private static MobilePhone phone = new MobilePhone(contacts);

Upvotes: 1

Related Questions