So my issue is trying to use the getName() and getPhone() methods (in class Contact) in the main method.
I don't know how to use them in this context with all 3 classes.
Here is what the program is supposed to be doing
Apologies if this is not too concise, I just started programming recently.
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;
}
}
This method is the only one used in a class provided to help with the exercise.
/**
* 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);
}
You need to store the result from findContactByName
to a local variable following the instruction. The original program just throw away the result.
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");
}
}