J
Jason C
Guest
Jason C Asks: Java Contact Book - Ability to save and Load
I am working on a project which allows a user to add a new contact, update an existing contact, remove a contact, list all contacts, save contacts and load contacts. The program I have created works for adding, listing, updating and removing. When implementing the save and l0oad classes I need to catch an IOExceptions and present to the user. Currently the code I have written is erroring for:
This error is presented in the ContactBook class for the save method.
Another error is:
Presented in the ContactBook class load method.
How do I refactor this code to remove these error and allow the program to compile?
Contact
ContactBook
ContactFileManager
ContactApp
I am working on a project which allows a user to add a new contact, update an existing contact, remove a contact, list all contacts, save contacts and load contacts. The program I have created works for adding, listing, updating and removing. When implementing the save and l0oad classes I need to catch an IOExceptions and present to the user. Currently the code I have written is erroring for:
exception IOException is never thrown in body of corresponding try statement
This error is presented in the ContactBook class for the save method.
Another error is:
Code:
incompatible types: Contact[] cannot be converted to Collection<? extends Contact>
Presented in the ContactBook class load method.
How do I refactor this code to remove these error and allow the program to compile?
Contact
Code:
import java.io.Serializable;
public class Contact implements Serializable {
private String name;
private String address;
private String phone;
private String email;
public Contact(String name, String address, String phone, String email) {
this.name = name;
this.address = address;
this.phone = phone;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
String s = new String().format("%-20s%-20s%-20s%-20s\n",name,address,phone,email);
return s;
}}
ContactBook
Code:
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class ContactBook {
private final ArrayList<Contact> entries;
private final ContactFileManager fileMan;
public ContactBook() {
entries = new ArrayList<>();
fileMan = new ContactFileManager();
}
public void add(Contact contact) {
entries.add(contact);
}
public void update(int index, Contact contact) {
entries.set(index, contact);
}
public void remove(int index) {
entries.remove(index);
}
public void save(String filename, Scanner sc) {
try {
fileMan.writeContacts(filename, entries);
} catch (IOException e) {
System.out.println("Unable to save: " + e.getMessage());
}
}
public void load (String filename, Scanner sc) {
try {
entries.clear();
entries.addAll(fileMan.readContacts(filename));
} catch (IOException | ClassNotFoundException e) {
System.out.println("Unable to load: " + e.getMessage());
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < entries.size(); i++) {
sb.append(i + 1).append(".\t").append(entries.get(i).toString()).append("\n");
}
return sb.toString();
}}
ContactFileManager
Code:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class ContactFileManager {
public static void writeContacts(String fileName, Contact[] contacts) throws IOException
{
FileOutputStream fos = new FileOutputStream(fileName);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(contacts);
oos.close();
fos.close();
}
public static Contact[] readContacts(String fileName) throws IOException,
ClassNotFoundException {
FileInputStream fis = new FileInputStream(fileName);
ObjectInputStream ois = new ObjectInputStream(fis);
Contact[] contacts = (Contact[]) ois.readObject();
ois.close();
fis.close();
return contacts;
}}
ContactApp
Code:
import java.util.Scanner;
public class ContactApp {
public static void main(String[] args) {
//Declaing variables
int choice;
String name, address, phone, email;
Scanner sc = new Scanner(System.in);
ContactBook book = new ContactBook();
choice = 0;
while (choice != 5) {
System.out.println("1. List all Contacts");
System.out.println("2. Add a new Contact");
System.out.println("3. Update an existing Contact");
System.out.println("4. Remove a Contact");
System.out.println("5. Save Contact Book");
System.out.println("6. Load Contact Book");
System.out.println("7. Quit");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
switch (choice) {
case 1: {
System.out.println(book.toString());
continue;
}
case 2: {
sc.nextLine();
System.out.print("Enter a name: ");
name = sc.nextLine();
System.out.print("Enter an address :");
address = sc.nextLine();
System.out.print("Enter a phone number :");
phone = sc.nextLine();
System.out.print("Enter an email address :");
email = sc.nextLine();
book.add(new Contact(name, address, phone, email));
continue;
}
case 3: {
System.out.println(book.toString());
System.out.print("Enter index of Contact to update: ");
int index = sc.nextInt();
sc.nextLine();
System.out.print("Enter a new name: ");
name = sc.nextLine();
System.out.print("Enter a new address :");
address = sc.nextLine();
System.out.print("Enter a new phone number :");
phone = sc.nextLine();
System.out.print("Enter a new email address :");
email = sc.nextLine();
book.update(index - 1, new Contact(name, address, phone, email));
continue;
}
case 4: {
System.out.println(book.toString());
System.out.print("Enter position number to delete: ");
int index = sc.nextInt();
book.remove(index - 1);
continue;
}
case 5: {
System.out.print("Enter the filename to save: ");
String filename = sc.nextLine();
book.save(filename, sc);
continue;
}
case 6: {
System.out.print("Enter the filename to load: ");
String filename = sc.nextLine();
book.load(filename, sc);
continue;
}
case 7: {
continue;
}
default: {
System.out.println("** Invalid Choice. Please try again.");
}
}
sc.close();
}
}}