import java.util.Scanner;
public class PhoneBook {
private Address[] addresses;
private String[] phoneNumbers;
private Person[] people;
public static String menu = ("Please Make A selection Below: \n1 - Add new Record"
+ " \n2 - Delete Record \n3 - Find Record By First Name "
+ "\n3 - Find Record By Telephone Number "
+ "\n4 - Find Record By First Name "
+ "\n5 - Find Record By Last Name"
+ "\n6 - Update a Record"
+ "\n7 - Exit");
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Scanner intScan = new Scanner(System.in);
// printing menu
System.out.println(menu);
//boolean quit = false;
Entry[] entry1 = new Entry[50];
int z = 0;
int selection;
do {
// next user input (of integer type) will be stored in the variable selection
selection = intScan.nextInt();
switch (selection) {
case 1:
System.out.println("Please enter a new record as John Michael West Doe, 574 Pole ave, St. Peter, MO, 63303, 3142752000");
// method to add that input to entry
String input = scanner.nextLine();
Entry entry = addEntry(input);
entry1[z] = entry;
z++;
System.out.println("Another entry?");
Scanner scan = new Scanner(System.in);
char cont = scan.next().charAt(0);
while(cont == 'y') {
System.out.println("Please enter a new record as John Michael West Doe, 574 Pole ave, St. Peter, MO, 63303, 3142752000");
scanner = new Scanner(System.in);
entry = addEntry(input);
entry1[z] = entry;
z++;
cont = scan.next().charAt(0);
}
System.out.println(menu);
break;
// create an array - of 100 spaces...
// add entry into array - for whatever number z is....
// z++ -
// make the method - as if I was to write it in the main method
// instead of having that every time -
// method just to create a class...
case 2:
selection = intScan.nextInt();
// method to delete record - remove
System.out.println("What record do you want to delete?");
//displayEntry(0, entry1);
String input2 = scanner.nextLine();
Entry[] entry2 = deleteEntry(entry1, input2);
// System.out.println("What now? ");
int num = scanner.nextInt();
Scanner scan2 = new Scanner(System.in);
char cont2 = scan2.next().charAt(0);
while(cont2 == 'y') {
System.out.println("What else would you like to delete?");
scanner = new Scanner(System.in);
System.out.println("Another entry?");
cont = scan2.next().charAt(0);
}
System.out.println(menu);
break;
case 3:
// search by telephone number
break;
case 4:
// find record by first name
break;
case 5:
// find record by last name
break;
case 6:
// update a record
break;
case 7:
//quit = true;
break;
default:
System.out.println("Invalid");
}
} while(selection != 7);
}
public static Entry addEntry(String input) {
String[] inputs = input.split(",");
String name, street, city, state, zipCode, phoneNumber;
name = "";
street = "";
city = "";
state = "";
zipCode = "";
phoneNumber = "";
name = inputs[0];
street = inputs[1].trim();
city = inputs[2].trim();
state = inputs[3].trim();
zipCode = inputs[4].trim();
phoneNumber = inputs[5].trim();
Name newName = createName(name);
Address address = createAddress(street, city, state, zipCode);
Phone phone = createPhone(phoneNumber);
Entry entry = new Entry(newName, address, phone);
return entry;
}
//method for deleting an entry
public static Entry[] deleteEntry(Entry[] entry, String input2) {
for (int i = 0; i < entry.length; i++) {
if (entry[i].equals(input2)) {
entry[i] = null;
break;
}
}
return entry;
}
//method for displaying entry
public static void displayEntry(int y, Entry[] entry) {
String fName = (String) entry[y].getName().getFirstName();
String mName = (String) entry[y].getName().getMiddleName();
String lName = (String) entry[y].getName().getLastName();
String name = fName + " " + mName + " " + lName;
String street = (String) entry[y].getAddress().getStreet();
String city = (String) entry[y].getAddress().getCity();
String state = (String) entry[y].getAddress().getState();
String zipCode = (String) entry[y].getAddress().getZipCode();
String address = street + " " + city + " " + state + " " + zipCode;
String area = (String) entry[y].getPhone().getArea();
String prefix = (String) entry[y].getPhone().getPrefix();
String line = (String) entry[y].getPhone().getLine();
String phone = "(" + area + ")" + "-" + prefix + "-" + line;
System.out.println( fName + " " + mName + " " + lName);
System.out.println();
System.out.println("Address: " + address);
System.out.println();
System.out.println("Phone: " + phone);
}
// method to create name based off the first 3 words from the scanner
public static Name createName(String name) {
String fName, mName, lName;
fName = "";
mName = "";
lName = "";
String[] names = name.split(" ");
fName = names[0];
lName = names[names.length-1];
for(int i =1; i < names.length - 1; i++) {
mName = mName + names[i];
if (i != names.length-1) {
mName += "";
}
}
// if to see if it's a middle name
// put into an array of entries.... entry[0] - breaks out name, address, phone number
Name name1 = new Name(fName, mName, lName);
return name1;
}
// method to create address
public static Address createAddress(String street, String city, String state, String zipCode) {
Address address1 = new Address(street, city, state, zipCode);
return address1;
}
// method to create a phone based off the user input
public static Phone createPhone(String input) {
String area, prefix, line;
area = input.substring(0, 3);
prefix = input.substring(3, 6);
line = input.substring(6);
Phone phone1 = new Phone(area, prefix, line);
return phone1;
}
// find record by telephone number:
// write down process - on white board -
}
// make methods in main program
// if 1 selected:
// Please enter new record a : John Michael West Doe, 574 Pole ave, St. Peter, MO, 63303, 3142752000
//**Show all records in asc order *note you will need to research sorting on an array.
I'm not sure why it won't let me delete a record after I input it.
Entry[] entry2 = deleteEntry(entry1, input2);
input 2 is the selection from the scanner and entry1 is the array that I created to store the records in. There's a hangup somewhere because I receive these errors:
Exception in thread "main" java.lang.NullPointerException
at PhoneBook.deleteEntry(PhoneBook.java:152)
at PhoneBook.main(PhoneBook.java:76)
I know in line 152, I'm trying to compare a string to an Entry[] object, but I don't know another way to pass in the record that I want deleted for that method.
I'm trying to just use arrays and no Lists or anything.