在地址簿中删除和修改

时间:2013-12-29 22:12:43

标签: java addressbook

最近我一直致力于地址簿,大部分都有这套操作:

  • 联系人的输入
  • 更改联系人
  • 显示联系人
  • 显示所有联系人
  • 删除联系人。

输入联系人并显示所有联系人正在运作,但我不知道如何完成其​​余的工作。

这是我到目前为止所做的: 主要课程:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class AddressBook {
    private static List<Data> contact = new ArrayList<Data>();

    public static void main(String[] args) {

        AddressBook addressBook = new AddressBook();


        Scanner sc = new Scanner(System.in);
        int menu;
        String choice;

        System.out.println(" 0. Exit. ");
        System.out.println(" 1. Add contact. ");
        System.out.println(" 2. Edit contact. ");
        System.out.println(" 3. Outprint contact. ");
        System.out.println(" 4. Outprint all contacts. ");
        System.out.println(" 5. Delete contact. ");
        menu = sc.nextInt();

        while (menu != 0) {

            switch (menu) {

            case 1:
                while (menu != 2) {
                    System.out.println("Enter First Name: ");
                    String firstName = sc.next();
                    System.out.println("Enter Last Name: ");
                    String lastName = sc.next();
                    System.out.println("Enter email: ");
                    String email = sc.next();
                    contact.add(new Data(firstName, lastName, email));// Creating a new object and adding it to list
                    System.out.println("Would you like to add someone else? 1: Yes, 2: No");
                    menu = sc.nextInt();
                }
                break;

            case 2:
                System.out.println("Enter First Name of contact that you would like to edit: ");
                int index = sc.nextInt();

                break;
            case 3:
                System.out.println("Enter First Name of contact that you would like to delete: ");
                choice = sc.next();
                break;
            case 4:
                System.out.println(addressBook.contact);
                break;
            case 5:
                System.out.println("Vpišite ime osebe, ki jo želite izbrisati: ");
                choice = sc.next();
                contact.remove(choice);
                break;
            }

            System.out.println(" 0. Exit. ");
            System.out.println(" 1. Add contact. ");
            System.out.println(" 2. Edit contact. ");
            System.out.println(" 3. Outprint contact. ");
            System.out.println(" 4. Outprint all contacts. ");
            System.out.println(" 5. Delete contact. ");
            menu = sc.nextInt();
        }
        System.out.println("Goodbye!");

    }

    private void addData(String firstName, String lastName, String email) {
        Data person = new Data(firstName, lastName, email);
        contact.add(person);
    }
}

和数据类:

public class Data {
    private String firstName = null;
    private String lastName = null;
    private String email = null;

    public Data(String firstName, String lastName, String email) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }


    public String toString() {
        return String.format(firstName+" "+lastName+" "+email);
    }
}

1 个答案:

答案 0 :(得分:0)

删除会像这样工作:

/**
 * Will work long as there are no two contacts with same first name. In that case
 * you'll need more data to find a unique contact.
 */
private void deleteByFirstName(String firstName) {
  for (Iterator<Data> iterator = contact.iterator(); iterator.hasNext();) {
    Data temp = iterator.next();

    // Add null checks for proper error handling.

    if (temp.getFirstName().equalsIgnoreCase(firstName)) {
      iterator.remove();
      return;
    }
  }

  System.out.println("No contact with first name " + firstName + " was found.");
}

您可以使用类似的逻辑来显示或编辑单个联系人。只需创建一个循环通过联系人的辅助方法,并返回匹配的联系人,您可以将它们用于这两个联系人。然后继续打印或编辑Data对象。