Address book contains a set of contact details.It can be a student details, customer details and so on.
Let us create it in java.
Logic:
This Application has four operations as follows.
- · Add Contact
- · Remove Contact
- · View Contacts
- · Exit
To add the contact details, user has to enter the person
name, phone number and email id.
To remove the contact details, user need to give the index
value.
View contacts used to display the details.
Exit is used to exit the application.
Steps:
- Three classes: ContactData, AddressBookEg, AddressBookAppEg
- ContactData class is used to declare the variables, get the values and set the values.
- AddressBookEg class is used to define the functions addContact(),getContacts() and removeContact().
- AddressBookAppEg is the main class. It contains the main() function.
- It reads the input from the user.Process according to the input and give you the output.
Program:
import java.util.ArrayList;
import java.util.Scanner;
class ContactData {
private String
cName;
private String
cPhoneNumber;
private String
cEmail;
public
ContactData(String cName, String cPhoneNumber, String cEmail) {
this.cName =
cName;
this.cPhoneNumber = cPhoneNumber;
this.cEmail =
cEmail;
}
public String
getName() {
return cName;
}
public String
getPhoneNumber() {
return
cPhoneNumber;
}
public String
getEmail() {
return cEmail;
}
public void
setName(String cName) {
this.cName =
cName;
}
public void
setPhoneNumber(String phoneNumber) {
this.cPhoneNumber = cPhoneNumber;
}
public void
setEmail(String email) {
this.cEmail =
cEmail;
}
}
class AddressBookEg {
private
ArrayList<Contact> contacts;
public
AddressBookEg() {
contacts = new
ArrayList<>();
}
public void
addContact(String cName, String cPhoneNumber, String cEmail) {
contacts.add(new Contact(cName, cPhoneNumber, cEmail));
}
public void
removeContact(int index) {
if (index
>= 0 && index < contacts.size()) {
contacts.remove(index);
}
}
public
ArrayList<Contact> getContacts() {
return
contacts;
}
public Contact
getContact(int index) {
if (index
>= 0 && index < contacts.size()) {
return
contacts.get(index);
}
return null;
}
}
public class AddressBookAppEg {
private static
AddressBookEg addressBookeg = new AddressBookEg();
private static
Scanner scanner1 = new Scanner(System.in);
public static void
main(String[] args) {
while (true) {
printMenu();
int
choice1 = scanner1.nextInt();
scanner1.nextLine();
switch
(choice1) {
case
1:
addContact();
break;
case
2:
removeContact();
break;
case
3:
displayContacts();
break;
case
4:
System.out.println("Exiting the application...");
return;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
private static
void printMenu() {
System.out.println("****Address Book Application****");
System.out.println("1. Add the Contact detail");
System.out.println("2. Remove a Contact");
System.out.println("3. View the Contacts");
System.out.println("4. Exit the application");
System.out.print("Choose an option according to your choice:
");
}
private static
void addContact() {
System.out.print("Enter the Person name: ");
String name =
scanner1.nextLine();
System.out.print("Enter the phone number: ");
String
phoneNumber = scanner1.nextLine();
System.out.print("Enter the email id: ");
String email =
scanner1.nextLine();
addressBookeg.addContact(name, phoneNumber, email);
}
private static
void removeContact() {
System.out.print("Enter the index to delete the data: ");
int
contactNumber = scanner1.nextInt();
addressBookeg.removeContact(contactNumber - 1);
}
private static
void displayContacts() {
ArrayList<Contact> contacts = addressBookeg.getContacts();
System.out.println("Contacts details:");
for (int i =
0; i < contacts.size(); i++) {
Contact
contact = contacts.get(i);
System.out.println((i + 1) + ". " + contact.getName() + "
- " + contact.getPhoneNumber() + " - " + contact.getEmail());
}
}
}
Output:
C:\raji\blog>javac AddressBookAppEg.java
C:\raji\blog>java AddressBookAppEg
****Address Book Application****
1. Add the Contact detail
2. Remove a Contact
3. View the Contacts
4. Exit the application
Choose an option according to your choice: 1
Enter the Person name: william
Enter the phone number: 2314428901
Enter the email id: william23@qwe.com
****Address Book Application****
1. Add the Contact detail
2. Remove a Contact
3. View the Contacts
4. Exit the application
Choose an option according to your choice: 1
Enter the Person name: raja
Enter the phone number: 1234567899
Enter the email id: raji78@gwe.com
****Address Book Application****
1. Add the Contact detail
2. Remove a Contact
3. View the Contacts
4. Exit the application
Choose an option according to your choice: 1
Enter the Person name: ajay
Enter the phone number: 09123456788
Enter the email id: ajay@abc.com
****Address Book Application****
1. Add the Contact detail
2. Remove a Contact
3. View the Contacts
4. Exit the application
Choose an option according to your choice: 3
Contacts details:
1. william - 2314428901 - william23@qwe.com
2. raja - 1234567899 - raji78@gwe.com
3. ajay - 09123456788 - ajay@abc.com
****Address Book Application****
1. Add the Contact detail
2. Remove a Contact
3. View the Contacts
4. Exit the application
Choose an option according to your choice: 2
Enter the index to delete the data: 1
****Address Book Application****
1. Add the Contact detail
2. Remove a Contact
3. View the Contacts
4. Exit the application
Choose an option according to your choice: 3
Contacts details:
1. raja - 1234567899 - raji78@gwe.com
2. ajay - 09123456788 - ajay@abc.com
****Address Book Application****
1. Add the Contact detail
2. Remove a Contact
3. View the Contacts
4. Exit the application
Choose an option according to your choice: 4
Exiting the application...
That’s all. The java program to implement address book
application is created successfully. Keep coding!!
No comments:
Post a Comment