在地址簿中创建,搜索,插入和删除联系人

时间:2014-03-29 12:42:10

标签: java

我是java的新手,并且自过去两周以来一直保持联系。我怎样才能创建新的联系人,搜索和删除。

修改

从答案中使用了建议,并学到了一些新东西。我现在能够使用查询使用数据库存储,搜索,删除任何联系人。我需要数据库连接。

驱动:

import java.util.Scanner;

public class Driver {

    public static void main(String [] args) {
        Scanner in = new Scanner(System.in);

        AddressBook adbook = new AddressBook();

        System.out.print("Type your first name: ");
        adbook.fname = in.nextLine();

        System.out.print("Type middle name: ");
        adbook.mname = in.nextLine();

        System.out.print("Type last name: ");
        adbook.lname = in.nextLine();

        System.out.print("Type National Identity Card Number: ");
        adbook.nic = in.nextLine();

        System.out.print("Type Home Adress: ");
        adbook.homead = in.nextLine();

        System.out.print("Type Home Phone Number: ");
        adbook.homeph = in.nextLine();

        System.out.print("Type Business Address: ");
        adbook.busadd = in.nextLine();

        System.out.print("Type Business Phone: ");
        adbook.busph = in.nextLine();

        System.out.print("Type Cell Number: ");
        adbook.cellnum = in.nextLine();

        System.out.print("Type Fax Number: ");
        adbook.faxnum = in.nextLine();

        adbook.showDetails();
    }
}

地址簿类

public class AddressBook {

    String fname;
    String mname;
    String lname;
    String nic;
    String homead;
    String homeph;
    String busadd;
    String busph;
    String faxnum;
    String cellnum;

    public AddressBook(String f_name, String m_name, String l_name,
        String n_ic, String home_ad, String home_ph,
        String bus_ad, String bus_ph, String fax_num, String cell_num) {
        fname = f_name;
        mname = m_name;
        lname = l_name;
        nic = n_ic;
        homeph = home_ph;
        homead = home_ad;
        busadd = bus_ad;
        busph = bus_ph;
        faxnum = fax_num;
        cellnum = cell_num;
    }

    AddressBook() {
    }

    public String getFname() {
        return fname;
    }

    public String getMname() {
        return mname;
    }

    public String getLname() {
        return lname;
    }

    public String getNic() {
        return nic;
    }

    public String getHomead() {
        return homead;
    }

    public String getHomeph() {
        return homeph;
    }

    public String getBusad() {
        return busadd;
    }

    public String getBusph() {
        return busph;
    }

    public String getCellnum() {
        return cellnum;
    }

    public String getFaxnum() {
        return faxnum;
    }

    public void showDetails() {
        System.out.println();
        System.out.println("Your Address Book Goes Here");
        System.out.println("Your Full Name is: " + fname + " " + mname + " " + lname);
        System.out.println("Natinal Identity Card Number: " + nic);
        System.out.println("Home Address: " + homead);
        System.out.println("Phone Number: " + homeph);
        System.out.println("Business Adress: " + busadd);
        System.out.println("Business Number: " + busph);
        System.out.println("Cell Number: " + cellnum);
        System.out.println("Fax Number: " + faxnum);
    }
}

1 个答案:

答案 0 :(得分:2)

每次调用其构造函数时,都会创建一个新联系人。

    AddressBook b1=new AddressBook(parameters...);
    AddressBook b2=new AddressBook(parameters...);
    .
    .

这意味着您的数据将暂时保存在各自的对象中。 因此,拥有一个用户界面和一个文件或数据库来永久存储数据会更好。在DB中删除很容易。