請問各位高手關於ArrayList的問題

寫一個程式使用變數型態Contact的陣列去儲存contacts的資料庫。

Contact class應該儲存contact的第一(first)和最後(last)的名字

、電話號碼(phone number)、和email地址。在過程中,加入適當的

get和set method。你的資料庫必須呈現一個menu讓使用者可以增加

一個contact、展示所有的contacts、尋找一個特定的contact並且

展示他、或者尋找一個特定的contact並且給使用者選擇是否要刪除

他。The searches should find any contact where any member

variable contains a target search string.

For example,if "elmore" is the search target then any

contact where the first name,last name,phone number,or email

address contains "elmore" should be return for display or

deletion. Use "for each loop" to iterate through the ArrayList.

因為後面翻不太順,所以打原文!!

拜託各位大大幫幫忙喔

謝謝=ˇ=

Update:

怎麼改呢??

1 Answer

Rating
  • 1 decade ago
    Favorite Answer

    請把原文貼上來

    謝謝

    2008-05-28 11:57:37 補充:

    public class Contact {

    // LAST NAME資料

    private String firstName;

    // FIRST NAME資料

    private String lastName;

    // TELEPHONE NUMBER資料

    private String telephoneNumber;

    // EMAIl資料

    private String emailAddress;

    //建構子

    public Contact() {

    }

    //建構子

    public Contact(String firstName, String lastName, String telNo, String email) {

    this.firstName = firstName;

    this.lastName = lastName;

    this.emailAddress = email;

    this.telephoneNumber = telNo;

    }

    // FIRST NAME資料的get method

    public String getFirstName() {

    return firstName;

    }

    // FIRST NAME資料的set method

    public void setFirstName(String firstName) {

    this.firstName = firstName;

    }

    // LAST NAME資料的get method

    public String getLastName() {

    return lastName;

    }

    // LAST NAME資料的set method

    public void setLastName(String lastName) {

    this.lastName = lastName;

    }

    // TELEPHONE NUMBER資料的get method

    public String getTelephoneNumber() {

    return telephoneNumber;

    }

    // TELEPHONE NUMBER資料的set method

    public void setTelephoneNumber(String telephoneNumber) {

    this.telephoneNumber = telephoneNumber;

    }

    // EMAIl資料的get method

    public String getEmailAddress() {

    return emailAddress;

    }

    // EMAIl資料的set method

    public void setEmailAddress(String emailAddress) {

    this.emailAddress = emailAddress;

    }

    //印出資料

    public void printContact() {

    System.out.println("FIRST NAME: " + this.getFirstName());

    System.out.println("LASt NAME: " + this.getLastName());

    System.out.println("TELEPHONE NO.: " + this.getTelephoneNumber());

    System.out.println("EMAIl ADDRESS: " + this.getEmailAddress());

    }

    }

    2008-05-28 11:58:12 補充:

    import java.io.BufferedReader;

    import java.io.InputStreamReader;

    import java.util.ArrayList;

    public class Search {

    private ArrayList list;

    public Search() {

    list = new ArrayList();

    2008-05-28 11:58:32 補充:

    try {

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

    while (true) {

    this.printMenu();

    String action = in.readLine();

    2008-05-28 11:59:55 補充:

    if ("1".equals(action)) {

    System.out.println("現有資料" + list.size() + "筆");

    } else if ("2".equals(action)) {

    System.out.print("輸入FIRST NAME: ");

    String firstName = in.readLine();

    this.searchFirstName(firstName);

    }

    2008-05-28 12:00:08 補充:

    else if ("3".equals(action)) {

    System.out.print("輸入LAST NAME: ");

    String lastName = in.readLine();

    this.searchLastName(lastName);

    } else if ("4".equals(action)) {

    System.out.print("輸入TEL NO: ");

    String telNo = in.readLine();

    this.searchTelNo(telNo);

    }

    2008-05-28 12:00:25 補充:

    } else if ("5".equals(action)) {

    System.out.print("輸入TEL NO: ");

    String telNo = in.readLine();

    this.searcheEmail(email);

    } else if ("6".equals(action)) {

    list.add(this.addContact(in));

    } else if ("7".equals(action)) {

    System.out.println("結束程式");

    break;

    }

    2008-05-28 12:00:40 補充:

    }

    } catch (Exception e) {

    System.out.println("輸入錯誤");

    }

    }

    2008-05-28 12:01:07 補充:

    private void searchFirstName(String firstName) {

    boolean hit = false;

    if (list == null || list.size() == 0) {

    System.out.println("資料庫中沒有資料");

    }

    2008-05-28 12:01:22 補充:

    Object [] array = list.toArray();

    for (int i = 0; i < array.length; i++) {

    Contact con = (Contact) array[i];

    if (con.getFirstName().equals(firstName)) {

    con.printContact();

    hit = true;

    break;

    }

    }

    2008-05-28 12:01:37 補充:

    if (!hit) {

    System.out.println("資料庫中沒有資料");

    }

    }

    2008-05-28 12:02:12 補充:

    private void searchLastName(String lastName) {

    boolean hit = false;

    if (list == null || list.size() == 0) {

    System.out.println("資料庫中沒有資料");

    }

    2008-05-28 12:02:19 補充:

    Object [] array = list.toArray();

    for (int i = 0; i < array.length; i++) {

    Contact con = (Contact) array[i];

    if (con.getLastName().equals(lastName)) {

    con.printContact();

    hit = true;

    break;

    }

    }

    if (!hit) {

    System.out.println("資料庫中沒有資料");

    }

    }

    2008-05-28 12:02:33 補充:

    private void searchTelNo(String telNo) {

    boolean hit = false;

    if (list == null || list.size() == 0) {

    System.out.println("資料庫中沒有資料");

    }

    2008-05-28 12:02:45 補充:

    Object [] array = list.toArray();

    for (int i = 0; i < array.length; i++) {

    Contact con = (Contact) array[i];

    if (con.getTelephoneNumber().equals(telNo)) {

    con.printContact();

    hit = true;

    break;

    }

    }

    if (!hit) {

    System.out.println("資料庫中沒有資料");

    }

    }

    2008-05-28 12:02:58 補充:

    private void searcheEmail(String email) {

    boolean hit = false;

    if (list == null || list.size() == 0) {

    System.out.println("資料庫中沒有資料");

    }

    2008-05-28 12:04:29 補充:

    Object [] array = list.toArray();

    for (int i = 0; i < array.length; i++) {

    Contact con = (Contact) array[i];

    if (con.getEmailAddress().equals(email)) {

    con.printContact();

    hit = true;

    break;

    }

    }

    if (!hit) {

    System.out.println("資料庫中沒有資料");

    }

    }

    2008-05-28 12:04:51 補充:

    private void printMenu() {

    System.out.println("1. 查詢現有資料數");

    System.out.println("2. 使用FIRST NAME來查詢資料");

    System.out.println("3. 使用LAST NAME來查詢資料");

    System.out.println("4. 使用TELEPHONE NUMBER來查詢資料");

    2008-05-28 12:05:02 補充:

    System.out.println("5. 使用EMAIL ADDRESS來查詢資料");

    System.out.println("6. 增加新資料");

    System.out.println("7. 退出");

    System.out.println("輸入你想要做的事情(1-7): ");

    }

    2008-05-28 12:05:31 補充:

    private Contact addContact(BufferedReader in) throws Exception {

    System.out.print("輸入FIRST NAME: ");

    String firstName = in.readLine();

    System.out.print("輸入LAST NAME: ");

    String lastName = in.readLine();

    2008-05-28 12:05:40 補充:

    System.out.print("輸入TELEPHONE NUMBER: ");

    String telNo = in.readLine();

    System.out.print("輸入EMAIL ADDRESS: ");

    String email = in.readLine();

    2008-05-28 12:05:53 補充:

    if (this.isNullorBlank(firstName) || this.isNullorBlank(lastName) ||

    this.isNullorBlank(telNo) || this.isNullorBlank(email)) {

    throw new Exception();

    }

    2008-05-28 12:06:06 補充:

    Contact contact = new Contact();

    contact.setFirstName(firstName);

    contact.setLastName(lastName);

    contact.setTelephoneNumber(telNo);

    contact.setEmailAddress(email);

    return contact;

    }

    2008-05-28 12:06:17 補充:

    private boolean isNullorBlank (String in) {

    if (in == null || in.trim().length() == 0) {

    return true;

    }

    return false;

    }

    public static void main(String [] args) {

    Search app = new Search();

    }

    }

    2008-05-28 12:07:35 補充:

    沒注意到要用for each

    不過只要稍微改一下就ok了

    Source(s): 自己, 自己, 自己, 自己, 自己, 自己, 自己, 自己, 自己, 自己, 自己, 自己, 自己, 自己, 自己, 自己, 自己, 自己, 自己, 自己, 自己, 自己, 自己, 自己
Still have questions? Get your answers by asking now.