元素未添加到我的数组列表中

时间:2020-06-30 04:06:55

标签: java

我正在挑战,我需要为手机创建简单的功能。该程序应:

  1. 打印联系人列表
  2. 添加新联系人
  3. 修改现有联系人
  4. 删除联系人
  5. 允许用户搜索联系人
  6. 允许用户退出程序。

对我不起作用的唯一部分是添加新联系人。 我认为问题出在addContacts()方法中的Mobile Phone类中。当我呼叫contacts.addName(scanner.nextLine());contacts.addPhoneNumber(scanner.nextLine());时,由于某种原因,正在键盘中键入的值未添加到我在联系人中初始化的“名称”数组列表和“ phoneNumber”数组列表中类。该代码将运行并提示用户输入要添加的联系人的姓名和电话号码。但是,当用户要求提供联系人列表时,程序始终会打印“零名称和零电话号码”。即使用户只是添加了具有姓名和电话号码的新联系人。非常感谢您的帮助,这是我的代码。

主类:

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");
  }
}

手机类别:

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");
    }
  }
}

联系人类别:

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

1 个答案:

答案 0 :(得分:1)

您定义了两个Contacts不同的对象,一个在Main类中,另一个在MobilePhone中。在添加和打印时,您需要重用同一对象。您可以将Contacts传递给MobilePhone构造函数

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

并通过Main类传递它

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