I'm trying to make two methods. One to add people, and another to view them. This is what I've got so far:
Contacts contactObj = new Contacts();
private void viewContact() {
System.out.println("Here are your contacts: ");
contactObj.getNames().forEach(System.out::println);
}
private void addContact() {
System.out.println("You're about to add " + firstName + " " + lastName + " to your contacts");
System.out.println("Are you sure? [y]es or [n]o");
String confirmation = fetchContactDetails.next().toLowerCase();
if (confirmation == "y" || confirmation == "yes") {
contactObj.addName(firstName + lastName);
contactObj.addName("connor template");
}
if (confirmation == "n" || confirmation == "no") {
firstName = null;
lastName = null;
}
}
I also have a Contact file that looks like this:
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class Contacts {
private List<String> names;
public Contacts() {
this.names = new ArrayList<>();
}
//add a name to list
public void addName(String name) {
if (!Objects.nonNull(names)) {
this.names = new ArrayList<>();
}
this.names.add(name);
}
//get the name attribute
public List<String> getNames() {
if (!Objects.nonNull(names)) {
this.names = new ArrayList<>();
}
return this.names;
}
}
So theoretically I should be able to call addContact() and it should ask me their name and last name. I should then be able to call viewContact() it should display a list of people. Unfortunately it says "Here are your contacts: " with no contacts. Thanks for any help :)