Copy 6
Copy 6
java
public
class
Contac
t {
private String firstName;
private String lastName;
private long phoneNumber;
private String emailId;
import
java.util.Scanne
r;
while (true) {
System.out.println("Menu\n" +
"\n" +
"1.Add Contact\n" +
"\n" +
"2.Display all contacts\n" +
"\n" +
"3.Search contact by phone \n" +
"\n" +
"4.Remove contact\n" +
"\n" +
"5.Exit\n" +
"\n" +
"Enter your choice: ");
int choice =
Integer.parseInt(scanner.nextLine());
switch (choice) {
case 1: {
System.out.println("Add a Contact:");
break;
}
case 2: {
System.out.println("The contacts in the
List are:");
break;
}
case 3: {
System.out.println("Enter the Phone
number to search contact:");
long phoneNumber =
Long.parseLong(scanner.nextLine());
Contact contact =
phoneBook.viewContactGivenPhone(phoneNumber);
if (contact == null) {
// I don't wanna deal with null
pointer exception
} else {
System.out.println("The contact
is:");
System.out.println("First Name: " +
contact.getFirstName());
System.out.println("Last Name: " +
contact.getLastName());
System.out.println("Phone No.: " +
contact.getPhoneNumber());
System.out.println("Email: " +
contact.getEmailId());
}
break;
}
case 4: {
System.out.println("Enter the Phone
number to remove :");
long phoneNumber =
Long.parseLong(scanner.nextLine());
if (option.startsWith("Y") ||
option.startsWith("y")) {
boolean isContactRemoved =
phoneBook.removeContact(phoneNumber);
if (isContactRemoved) {
System.out.println("The contact
is successfully deleted.");
} else {
// No instruction given
}
} else if (option.startsWith("N") ||
option.startsWith("n")) {
// Don't remove
} else {
// Wrong input
}
}
}
}
}
}
Phonebook.java
import
java.util.ArrayLis
t;
import java.util.List;
return null;
}
if (contact == null) {
return false;
} {
return phoneBook.remove(contact);
}
}
}