搜索子主题列表

Searching through a list of child subjects

所以我的问题是尝试在 main 方法中使用 getName() 和 getPhone() 方法(在 class Contact 中)。

我不知道如何在所有 3 个 classes 的上下文中使用它们。

Here is what the program is supposed to be doing

抱歉,如果这不是太简洁,我最近才开始编程。

public class LookupPhonebook{
    public static void main(String[] args) {
        Phonebook phonebook = new Phonebook("Sam Johnson");
        phonebook.addContact(new Contact("Kelly Wong", "(02) 12345678"));
        phonebook.addContact(new Contact("Richard Jackson", "(02) 87654321"));
        phonebook.show();

        String searchName = Input.askString("Enter a contact name: ");
        phonebook.findContactByName(searchName);
        if (searchName.equals(phonebook.getName())) {
            System.out.println("Phone number is " + phonebook.getPhone());
        }
        else {
            System.out.println(searchName + " not found");
        }
    }
}

import java.util.*;
public class Phonebook {
    private String owner;
    private ArrayList<Contact> contacts = new ArrayList<Contact>();

    public Phonebook(String owner) {
        this.owner = owner;
    }

    public void addContact(Contact contact) {
        contacts.add(contact);
    }

    public void show(){
        System.out.println(owner + "'s phonebook");
        for (Contact contact : contacts) {
            System.out.println(contact);
        }
    }

    public Contact findContactByName(String name) {
        for (Contact contact : contacts) {
            if (contact.getName().equals(name)) {
                return contact;
            }
            else {
                continue;
            }
        }
        return null;    
    }
}

public class Contact {
    private String name;
    private String phone;

    public Contact(String name, String phone) {
        this.name = name;
        this.phone = phone;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String toString() {
        return name + ": " + phone;
    }
}

此方法是 class 提供的唯一一种帮助练习的方法。

/**
 * Asks the user the given question, waits for the user to enter a
 * single character at the keyboard, and then returns this character.
 *
 * @param question the question to be printed
 * @return the character that the user entered as an answer to the question
 */
public static char askChar(String question) {
    System.out.print(question + " ");
    return scanner.nextLine().charAt(0);
}

您需要按照指令将 findContactByName 的结果存储到局部变量中。原程序只是把结果丢掉。

public static void main(String[] args) {
    Phonebook phonebook = new Phonebook("Sam Johnson");
    phonebook.addContact(new Contact("Kelly Wong", "(02) 12345678"));
    phonebook.addContact(new Contact("Richard Jackson", "(02) 87654321"));
    phonebook.show();

    String searchName = Input.askString("Enter a contact name: ");
    // 2. Search contact and store is local variable
    Contact result = phonebook.findContactByName(searchName);
    // 3. If we found contact
    if (result != null) {
        System.out.println("Phone number is " + result.getPhone());
    } else {
    // 4. No contact is found
        System.out.println(searchName + " not found");
    }
}