Java通讯录.如何防止我的代码中出现重复的联系人? [英] Java address book. How to prevent duplicate contacts in my code?

查看:30
本文介绍了Java通讯录.如何防止我的代码中出现重复的联系人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

switch(menuChoice) {

case 1: 
    System.out.println("Enter your contact's first name:\n");
    String fname = scnr.next();
    System.out.println("Enter your contact's last name:\n");
    String lname = scnr.next();
    Necronomicon.addContact(new Person(fname, lname));
    break;

// main truncated here for readability

<小时>

import java.util.ArrayList;

public class AddressBook {

   ArrayList<Person> ArrayOfContacts= new ArrayList<Person>();

   public void addContact(Person p) {
      ArrayOfContacts.add(p);

 /* 
    for(int i = 0;  i < ArrayOfContacts.size(); i++) {
       if(ArrayOfContacts.get(i).getID() != p.getID())
           ArrayOfContacts.add(p);

    else 
       System.out.println("Sorry this contact already exists.");
    }       
  */

    }
}

<小时>

public class Person {

   private String fName = null;
   private String lName = null;
   private static int ID = 1000;

   public Person(String fName, String lName) {       // Constructor I'm using to try and increment the ID each time a Person object is created starting at 1001.

     this.fName = fName;
     this.lName = lName;
     ID = ID + 1;
   }
}

我正在尝试创建一个地址簿,其中每个联系人都有一个名字、姓氏和一个唯一的 ID.

I am trying to create an address book whereby each contact has a first name, last name and a unique ID.

我的问题是如何防止用户输入具有相同名字和姓氏的重复联系人?我应该在 addContact 方法中还是在 main 中实现某种检查?如何?

My question is how to I prevent a user from entering in duplicate contacts with the same first and last name? Should I implement some kind of check in the addContact method or right in main? How?

推荐答案

这是防止重复 ID 的代码.

Here is the code for keeping out duplicate ID's.

public void addContact(Person p) {

    for(int i = 0;  i < ArrayOfContacts.size(); i++) {
        Person contact = ArrayOfContacts.get(i);
        if(contact.getID() == p.getID()) {
            System.out.println("Sorry this contact already exists.");
            return; // the id exists, so we exit the method. 
        }
    }

    // Otherwise... you've checked all the elements, and have not found a duplicate
    ArrayOfContacts.add(p);

}

如果您想更改此代码以防止重复名称,请执行以下操作

If you would like to change this code to keep out duplicate names, then do something like this

public void addContact(Person p) {
    String pName = p.getFname() + p.getLname();
    for(int i = 0;  i < ArrayOfContacts.size(); i++) {
        Person contact = ArrayOfContacts.get(i);
        String contactName =  contact.getFname() + contact.getLname(); 
        if(contactName.equals(pName)) {
            System.out.println("Sorry this contact already exists.");
            return; // the name exists, so we exit the method. 
        }
    }

    // Otherwise... you've checked all the elements, and have not found a duplicate
    ArrayOfContacts.add(p);

}

这篇关于Java通讯录.如何防止我的代码中出现重复的联系人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆