I am working on a address book program and I'm running into some issues. I'm really new to java especially ArrayLists/File I/O and for some reason methods with parameters confuse me a lot.
So here is my issue...Right now I'm working on allowing the user to delete a contact but it doesn't seem to be removed. After removing the contact I "view" the address book all the contacts are printing. I realized I have to rewrite to the text file after the contact is removed but that's not working wither. I think it has to do with how I'm writing to the text file and how I'm adding the contact but I'm really new to ArrayLists/File IO and for some reason methods with parameters get me very confused. Thanks for any help I'm pulling my hair out looking for answers.
I put all my code so you can see how it's set up and put ****'s around each part I think the issue may be coming from. (Contact class is your basic getter and setter and constructors.)
public class AddressBook implements Serializable{
private ArrayList<String> newBook = new ArrayList<String>();
private String dataFile;
private ArrayList<Contact> card =new ArrayList<Contact>(50);
private Contact[] contacts;
private int size = 0;
private int capacity = 0;
private String firstName;
private String lastName;
Scanner scan = new Scanner(System.in);
private Contact temp;
public static void main(String[] args) {
AddressBook AB = new AddressBook();
AB.addressBookMenu();
}
//Method for address book menu
public void addressBookMenu() {
String option = "";
System.out.println("PLEASE SELECT ONE OF THE FOLLOWING OPTIONS: ");
System.out.println("\t add --> Add a new contact ");
System.out.println("\t find --> Find a contact ");
System.out.println("\t edit --> Edit an existing contact ");
System.out.println("\t view --> View the current address book");
System.out.println("\t save --> Save the current address book");
System.out.println("\t quit --> quit");
System.out.println();
option = scan.nextLine();
while(!(option.equalsIgnoreCase("quit"))) {
Contact con = new Contact(firstName, lastName);
if(option.equalsIgnoreCase("add")) {
System.out.println("Enter First Name: ");
String tempFirst = scan.nextLine();
System.out.println("Enter Last Name: ");
String tempLast = scan.nextLine();
con.setFirstName(tempFirst);
con.setLastName(tempLast);
card.add(con);
writeContact();
}
******************************************************************************
//View address book
if(option.equalsIgnoreCase("view")) {
System.out.println("ADDRESS BOOK: " + "\n" +
"==========================================");
readContact();
Collections.sort(card);
printContacts();
}
******************************************************************************
//Find and view a contact.
if(option.equalsIgnoreCase("find")) {
findContact(temp);
}
//Edit contacts
if(option.equalsIgnoreCase("edit")) {
editContact();
}
System.out.println();
System.out.println("\n\nPLEASE SELECT ONE OF THE FOLLOWING OPTIONS: ");
System.out.println("\t add --> Add a new contact ");
System.out.println("\t find --> Find a contact ");
System.out.println("\t edit --> Edit an existing contact ");
System.out.println("\t view --> View the current address book");
System.out.println("\t save --> Save the current address book");
System.out.println("\t quit --> quit");
System.out.println();
option = scan.nextLine();
}
}
******************************************************************************
public void writeContact() {
try (FileOutputStream out = new FileOutputStream("addressbook.txt")) {
ObjectOutputStream os = new ObjectOutputStream(out);
os.writeObject(card);
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
******************************************************************************
public void readContact() {
try (FileInputStream in = new FileInputStream("addressbook.txt")) {
ObjectInputStream is = new ObjectInputStream(in);
card = (ArrayList<Contact>)is.readObject();
is.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//Print out contacts
public void printContacts() {
for(Contact temp : card) {
System.out.println(temp);
}
}
public Contact findContact(Contact t) {
System.out.println("Enter the LAST NAME of the contact you'd like to find: ");
String tempLastName = scan.nextLine();
for(Contact temp : card) {
if(temp.getLastName().equalsIgnoreCase(tempLastName)) {
System.out.println("\nCONTACT FOUND:");
System.out.println(temp);
}
else if(temp.getLastName() == null || temp.getLastName().equalsIgnoreCase(tempLastName)) {
System.out.println("Sorry but " + tempLastName + " is not located in this address book");
}
}
return temp;
}
******************************************************************************
public void editContact() {
String editOption;
System.out.print("HOW WOULD YOU LIKE TO EDIT A CONTACT: ");
System.out.println("\tdelete --> Delete a contact");
System.out.println("\tchange --> edit or replace a contact");
editOption = scan.nextLine();
if(editOption.equalsIgnoreCase("delete")) {
findContact(temp);
card.remove(temp);
writeContact();
******************************************************************************
}
}
}
EXAMPLE add Enter First Name: scott Enter Last Name: smith
add
Enter First Name:
Bob
Enter Last Name:
jones
edit
delete
Enter Last Name of Contact to delete:
jones
view
ADDRESS BOOK:
==========================================
FIRST NAME: Bob LAST NAME: Jones
FIRST NAME: Scott LAST NAME: Smith
^^^^Still showing removed contact^^^^^
**EXPECTED OUTPUT**
view
ADDRESS BOOK:
==========================================
FIRST NAME: Scott LAST NAME: Smith
findContact(temp);should beContact contact = findContact(temp);, although I'd question the need to usetempat all, asfindContactnever makes use of itfindContactto do just that, finding a contact ;)