0% found this document useful (0 votes)
2 views6 pages

Copy 6

The document contains Java code for a simple phone book application with three main classes: Contact, Main, and PhoneBook. The Contact class manages individual contact details, while the Main class provides a user interface for adding, displaying, searching, and removing contacts. The PhoneBook class maintains a list of contacts and implements methods for managing them.

Uploaded by

pansotraneha1997
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Copy 6

The document contains Java code for a simple phone book application with three main classes: Contact, Main, and PhoneBook. The Contact class manages individual contact details, while the Main class provides a user interface for adding, displaying, searching, and removing contacts. The PhoneBook class maintains a list of contacts and implements methods for managing them.

Uploaded by

pansotraneha1997
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Contact.

java

public
class
Contac
t {
private String firstName;
private String lastName;
private long phoneNumber;
private String emailId;

public Contact(String firstName, String lastName, long phoneNumber,


String emailId) {
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
this.emailId = emailId;
}

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 long getPhoneNumber() {


return phoneNumber;
}
public void setPhoneNumber(long phoneNumber) {
this.phoneNumber = phoneNumber;
}

public String getEmailId() {


return emailId;
}

public void setEmailId(String emailId) {


this.emailId = emailId;
}
}
Main.java

import
java.util.Scanne
r;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
PhoneBook phoneBook = new PhoneBook();

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:");

System.out.println("Enter the First Name:


");
String firstName = scanner.nextLine();

System.out.println("Enter the Last Name:


");
String lastName = scanner.nextLine();

System.out.println("Enter the Phone No.:


");
long phoneNumber =
Long.parseLong(scanner.nextLine());

System.out.println("Enter the Email: ");


String email = scanner.nextLine();

Contact contact = new Contact(firstName,


lastName, phoneNumber, email);
phoneBook.addContact(contact);

break;
}
case 2: {
System.out.println("The contacts in the
List are:");

for (Contact contact :


phoneBook.viewAllContacts()) {
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 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());

System.out.println("Do you want to remove


the contact (Y/N): ");
String option = 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;

public class PhoneBook {


private List<Contact> phoneBook = new
ArrayList<>();

public List<Contact> getPhoneBook() {


return phoneBook;
}

public void setPhoneBook(List<Contact> phoneBook) {


this.phoneBook = phoneBook;
}
public void addContact(Contact contact) {
phoneBook.add(contact);
}

public List<Contact> viewAllContacts() {


return phoneBook;
}

public Contact viewContactGivenPhone(long


phoneNumber) {
for (Contact contact : phoneBook) {
if (contact.getPhoneNumber() ==
phoneNumber) {
return contact;
}
}

return null;
}

public boolean removeContact(long phoneNumber) {


Contact contact =
viewContactGivenPhone(phoneNumber);

if (contact == null) {
return false;
} {
return phoneBook.remove(contact);
}
}
}

You might also like