I am trying to add some names and phone numbers created from the constructor of the PhoneBookEntry class, store them inside of an ArrayList inside of the PhoneBook class, and then be able to print out the array list.
public class Application {
public static void main(String[] args) {
PhoneBookEntry name1 = new PhoneBookEntry("Cameron", "1-425-415-7157");
PhoneBookEntry name2 = new PhoneBookEntry("Mike", "1-748-142-2341");
PhoneBookEntry name3 = new PhoneBookEntry("Riles", "1-471-648-1782");
PhoneBookEntry name4 = new PhoneBookEntry("Tom", "1-427-145-6471");
PhoneBookEntry name5 = new PhoneBookEntry("Billy", "1-718-545-5715");
}
}
import java.util.ArrayList;
public class PhoneBookEntry {
PhoneBook book = new PhoneBook();
public PhoneBookEntry(String name, String phoneNumber) {
book.add(name, phoneNumber);
}
public void printEntries() {
for(int i = 0; i < names.size(); i++) {
System.out.println("Name #"+(i + 1)+": "+names.get(i));
}
}
}
import java.util.ArrayList;
public class PhoneBook {
ArrayList<String> names = new ArrayList<String>();
ArrayList<String> phoneNumbers = new ArrayList<String>();
public void add(String name, String phoneNumber) {
names.add(name);
System.out.println(name + " added to the arraylist!");
System.out.println(names.size());
phoneNumbers.add(phoneNumber);
}
public void print() {
for (int i = 0; i < names.size(); i++) {
System.out.println("Name #" + (i + 1) + ": " + names.get(i));
}
for (int i = 0; i < phoneNumbers.size(); i++) {
System.out.println("Phone Number #" + (i + 1) + ": " + phoneNumbers.get(i));
}
}
}
Current output:
Cameron added to the arraylist!
1
Mike added to the arraylist!
1
Riles added to the arraylist!
1
Tom added to the arraylist!
1
Billy added to the arraylist!
1
ArrayListdeclared?Map(probablyTreeMap). Utilise parametrisation with<String, String>unless you feel it would be easier to construct a new wrapper for phone numbers (probably calledPhoneNumber:P)