EmptyNameException.java
public class EmptyNameException extends Exception{
public EmptyNameException(String msg)
{
super(msg);
}
}
ContactNotFoundException.java
public class ContactNotFoundException extends Exception{
public ContactNotFoundException(String msg)
{
super(msg);
}
}
DuplicateContactException.java
public class DuplicateContactException extends Exception{
public DuplicateContactException(String msg)
{
super(msg);
}
}
Contact.java
public class Contact {
private String firstName, lastName, email, phone;
public Contact()
{
this.firstName = this.lastName = this.email = this.phone =
"";
}
public Contact(String firstName, String lastName) throws
EmptyNameException
{
if(firstName.equals("") || firstName.trim().equals("") ||
lastName.equals("") || lastName.trim().equals(""))
throw new EmptyNameException("First/Last name cannot be
empty!");
this.firstName = firstName;
this.lastName = lastName;
this.email = this.phone = "";
}
public Contact(String firstName, String lastName, String email,
String phone) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.phone = phone;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString()
{
return("Name: " + this.firstName + " " + this.lastName + ", Email:
" + this.email + ", Phone: " + this.phone);
}
public boolean equals(Contact other)
{
return (this.firstName.equalsIgnoreCase(other.getFirstName())
&&
this.lastName.equalsIgnoreCase(other.getLastName()));
}
}
AddressBook.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class AddressBook {
private ArrayList<Contact> contacts;
private static final String FILE = "contacts.dat";
public AddressBook()
{
this.contacts = new ArrayList<>();
// reads input file and populate all contacts to the list
Scanner fileReader;
try
{
fileReader = new Scanner(new File(FILE));
while(fileReader.hasNextLine())
{
String line = fileReader.nextLine().trim();
String[] data = line.split(",");
String firstName = data[0];
String lastName = data[1];
String email = data[2];
String phone = data[3];
this.contacts.add(new Contact(firstName, lastName, email,
phone));
}
fileReader.close();
}catch(FileNotFoundException fnfe){
System.exit(1);
}
}
public void addContact(Contact newContact) throws
EmptyNameException, DuplicateContactException, IOException
{
if(newContact.getFirstName().equals("") ||
newContact.getFirstName().trim().equals("")
|| newContact.getLastName().equals("") ||
newContact.getLastName().trim().equals(""))
throw new EmptyNameException("First/Last name cannot be
empty!");
for(Contact con : this.contacts)
{
if(con.equals(newContact))
throw new DuplicateContactException(("Duplicate contact
found!"));
}
this.contacts.add(newContact);
saveContactsToDisk();
}
private void saveContactsToDisk() throws IOException
{
FileWriter fw;
PrintWriter pw;
fw = new FileWriter(new File(FILE));
pw = new PrintWriter(fw);
for(Contact con : this.contacts)
{
pw.write(con.getFirstName() + "," + con.getLastName() + "," +
con.getEmail() + "," + con.getPhone()
+ System.lineSeparator());
}
pw.flush();
fw.close();
pw.close();
}
public void deleteContact(String firstName, String lastName) throws
EmptyNameException, ContactNotFoundException, IOException
{
if(firstName.equals("") || firstName.trim().equals("") ||
lastName.equals("") || lastName.trim().equals(""))
throw new EmptyNameException(("First/Last name cannot be
empty!"));
int index = -1;
boolean found = false;
for(int i = 0; i < this.contacts.size(); i++)
{
if(this.contacts.get(i).getFirstName().equalsIgnoreCase(firstName)
&&
this.contacts.get(i).getLastName().equalsIgnoreCase(lastName))
{
found = true;
index = i;
break;
}
}
if(!found)
throw new ContactNotFoundException("No contact with name " +
firstName + " " + lastName + " were found!");
// contact found at index
this.contacts.remove(index);
saveContactsToDisk();
}
public Contact[] searchByLastName(String lastName) throws
EmptyNameException
{
if(lastName.equals("") || lastName.trim().equals(""))
throw new EmptyNameException(("First/Last name cannot be
empty!"));
ArrayList<Contact> searchedContacts = new
ArrayList<>();
for(int i = 0; i < this.contacts.size(); i++)
{
if(this.contacts.get(i).getLastName().equalsIgnoreCase(lastName))
searchedContacts.add(this.contacts.get(i));
}
return (Contact[]) searchedContacts.toArray(new Contact[0]);
}
public Contact[] list()
{
return (Contact[]) this.contacts.toArray(new Contact[0]);
}
public Contact[] searchByLastNamePartial(String partialLastName)
throws EmptyNameException
{
if(partialLastName.equals("") ||
partialLastName.trim().equals(""))
throw new EmptyNameException(("First/Last name cannot be
empty!"));
ArrayList<Contact> searchedContacts = new
ArrayList<>();
for(int i = 0; i < this.contacts.size(); i++)
{
if(this.contacts.get(i).getLastName().contains(partialLastName))
searchedContacts.add(this.contacts.get(i));
}
return (Contact[]) searchedContacts.toArray(new Contact[0]);
}
}
AddressBookTester.java (Main class)
import java.io.IOException;
public class AddressBookTester {
public static void main(String[] args)
{
System.out.println("Attempting to read disk for contacts..");
AddressBook addressBook = new AddressBook();
System.out.println("\nDisplaying all contacts read from
disk..");
for(int i = 0; i < addressBook.list().length; i++)
{
System.out.println(addressBook.list()[i]);
}
System.out.println("\nAttempting to add an empty Contact..");
try {
addressBook.addContact(new Contact(" ", " ", "alice@hotmail.com",
"445698712"));
} catch (EmptyNameException | DuplicateContactException |
IOException ex) {
System.out.println(ex.getMessage());
}
System.out.println("\nAttempting to add a duplicate
Contact..");
try {
addressBook.addContact(new Contact("Alice", "Stuart",
"alice@hotmail.com", "445698712"));
} catch (EmptyNameException | DuplicateContactException |
IOException ex) {
System.out.println(ex.getMessage());
}
System.out.println("\nAttempting to add a new Contact..");
try {
addressBook.addContact(new Contact("Alakha", "Measil",
"alakmeas@rediffmail.com", "1169987231"));
} catch (EmptyNameException | DuplicateContactException |
IOException ex) {
System.out.println(ex.getMessage());
}
System.out.println("Displaying all contacts read from
disk..");
for(int i = 0; i < addressBook.list().length; i++)
{
System.out.println(addressBook.list()[i]);
}
System.out.println("\nAttempting to delete a contact not
present..");
try {
addressBook.deleteContact("Wannah", "Halfboy");
} catch (EmptyNameException | ContactNotFoundException |
IOException ex) {
System.out.println(ex.getMessage());
}
System.out.println("\nAttempting to delete a contact present (Alice
Stuart)..");
try {
addressBook.deleteContact("Alice", "Stuart");
} catch (EmptyNameException | ContactNotFoundException |
IOException ex) {
System.out.println(ex.getMessage());
}
System.out.println("Displaying all contacts read from
disk..");
for(int i = 0; i < addressBook.list().length; i++)
{
System.out.println(addressBook.list()[i]);
}
System.out.println("\nAttempting to search contacts by last name
(Weasely)..");
try {
for(int i = 0; i <
addressBook.searchByLastName("Weasely").length; i++)
{
System.out.println(addressBook.searchByLastNamePartial("Weasely")[i]);
}
} catch (EmptyNameException ex) {
System.out.println(ex.getMessage());
}
System.out.println("\nAttempting to search contacts by partial last
name (son)..");
try {
for(int i = 0; i <
addressBook.searchByLastNamePartial("son").length; i++)
{
System.out.println(addressBook.searchByLastNamePartial("son")[i]);
}
} catch (EmptyNameException ex) {
System.out.println(ex.getMessage());
}
}
}
******************************************************************** SCREENSHOT *******************************************************


CONTACTS FILE (contacts.dat)

Requirements Create an Address Book class in Java for general use with the following behaviors: 1....
Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class should contain the following data fields and methods (note that all data and methods are for objects unless specified as being for the entire class) Data fields: A String object named firstName A String object named middleName A String object name lastName Methods: A Module2 constructor method that accepts no parameters and initializes the data fields from 1) to empty Strings (e.g., firstName =...
Fix the following null pointer error. import java.util.*; import java.io.*; public class PhoneBook { public static void main(String[]args)throws IOException { PhoneBook obj = new PhoneBook(); PhoneContact[]phBook = new PhoneContact[20]; Scanner in = new Scanner(System.in); obj.acceptPhoneContact(phBook,in); PrintWriter pw = new PrintWriter("out.txt"); obj.displayPhoneContacts(phBook,pw); pw.close(); } public void acceptPhoneContact(PhoneContact[]phBook, Scanner k) { //void function that takes in the parameters //phBook array and the scanner so the user can input the information //declaring these variables String fname = ""; String lname = ""; String...
JAVA PROGRAMMING In this final review lab from our intro course, use the Name class you created in the previous lab. In this lab, create a Student class with the following class variable: Student name: Name (Note: Name is a datatype) Student Id: String Create the getter and setter methods. In addition to these methods, create a toString() method, which overrides the object class toString() method. This override method prints the current value of any of this class object. Complete...
DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to add new phone contacts, display a list of all contacts, search for a specific contact by name, delete a specific contact. The program should provide the user with a console or command line choice menu about possible actions that they can perform. The choices should be the following: 1. Display list of all contacts. 2. Add a new contact. 3. Search for a contact...
Hello Can you help to fix the program. When running, it still show Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol symbol: class Contact location: class ContactMap at ContactMap.main(ContactMap.java:40) C:\Users\user\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 1 second) ************************ import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.util.TreeMap; public class ContactMap { public static void main(String args[]) throws IOException { Scanner input=new Scanner(System.in); //Create a TreeMap ,...
Requirements: Your Java class names must follow the names specified above. Note that they are case sensitive. See our template below. BankAccount: an abstract class. SavingAccount: a concrete class that extends BankAccount. o The constructor takes client's firstname, lastname and social security number. o The constructor should generate a random 6-digit number as the user account number. o The initial balance is 0 and the annual interest rate is fixed at 1.0% (0.01).o The withdraw method signature...
Write in C++ please:
Write a class named MyVector using the following UML diagram and class attribute descriptions. MyVector will use a linked listed instead of an array. Each Contact is a struct that will store a name and a phone number. Demonstrate your class in a program for storing Contacts. The program should present the user with a menu for adding, removing, and retrieving Contact info. MyVector Contact name: string phone: string next: Contact -head: Contact -tail: Contact -list_size:...
Need help working through CLion: Description: For this assignment you will create a program to manage phone contacts. Your program will allow the user to add new phone contacts, display a list of all contacts, search for a specific contact by name, delete a specific contact. Your program should provide the user with a console or command line choice menu about possible actions that they can perform. The choices should be the following: 1. Display list of all contacts. 2....
In this assignment, you will create an application that holds a list of contact information. You will be able to prompt the user for a new contact, which is added to the list. You can print the contact list at any time. You will also be able to save the contact list. MUST BE IN PYTHON FORMAT Create the folllowing objects: ContactsItem - attributes hold the values for the contact, including FirstName - 20 characters LastName - 20 characters Address...
In this assignment, you will create an application that holds a list of contact information. You will be able to prompt the user for a new contact, which is added to the list. You can print the contact list at any time. You will also be able to save the contact list. Must Be In Python Format Create the following objects: ContactsItem - attributes hold the values for the contact, including FirstName - 20 characters LastName - 20 characters Address...