|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.Scanner; |
| 3 | + |
| 4 | +class Contact { |
| 5 | + private String name; |
| 6 | + private String phoneNumber; |
| 7 | + private String email; |
| 8 | + |
| 9 | + public Contact(String name, String phoneNumber, String email) { |
| 10 | + this.name = name; |
| 11 | + this.phoneNumber = phoneNumber; |
| 12 | + this.email = email; |
| 13 | + } |
| 14 | + |
| 15 | + public String getName() { |
| 16 | + return name; |
| 17 | + } |
| 18 | + |
| 19 | + public String getPhoneNumber() { |
| 20 | + return phoneNumber; |
| 21 | + } |
| 22 | + |
| 23 | + public String getEmail() { |
| 24 | + return email; |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public String toString() { |
| 29 | + return "Name: " + name + "\nPhone: " + phoneNumber + "\nEmail: " + email; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +public class AddressBook { |
| 34 | + public static void main(String[] args) { |
| 35 | + ArrayList<Contact> contacts = new ArrayList<>(); |
| 36 | + Scanner scanner = new Scanner(System.in); |
| 37 | + |
| 38 | + while (true) { |
| 39 | + System.out.println("\nAddress Book Menu:"); |
| 40 | + System.out.println("1. Add Contact"); |
| 41 | + System.out.println("2. View All Contacts"); |
| 42 | + System.out.println("3. Search Contact by Name"); |
| 43 | + System.out.println("4. Edit Contact"); |
| 44 | + System.out.println("5. Delete Contact"); |
| 45 | + System.out.println("6. Exit"); |
| 46 | + System.out.print("Select an option: "); |
| 47 | + |
| 48 | + int choice = scanner.nextInt(); |
| 49 | + scanner.nextLine(); // Consume newline |
| 50 | + |
| 51 | + switch (choice) { |
| 52 | + case 1: |
| 53 | + System.out.print("Enter Name: "); |
| 54 | + String name = scanner.nextLine(); |
| 55 | + System.out.print("Enter Phone Number: "); |
| 56 | + String phoneNumber = scanner.nextLine(); |
| 57 | + System.out.print("Enter Email: "); |
| 58 | + String email = scanner.nextLine(); |
| 59 | + contacts.add(new Contact(name, phoneNumber, email)); |
| 60 | + System.out.println("Contact added successfully!"); |
| 61 | + break; |
| 62 | + case 2: |
| 63 | + System.out.println("All Contacts:"); |
| 64 | + for (Contact contact : contacts) { |
| 65 | + System.out.println(contact); |
| 66 | + } |
| 67 | + break; |
| 68 | + case 3: |
| 69 | + System.out.print("Enter Name to Search: "); |
| 70 | + String searchName = scanner.nextLine(); |
| 71 | + for (Contact contact : contacts) { |
| 72 | + if (contact.getName().equalsIgnoreCase(searchName)) { |
| 73 | + System.out.println(contact); |
| 74 | + break; |
| 75 | + } |
| 76 | + } |
| 77 | + break; |
| 78 | + case 4: |
| 79 | + // Implement editing a contact |
| 80 | + break; |
| 81 | + case 5: |
| 82 | + // Implement deleting a contact |
| 83 | + break; |
| 84 | + case 6: |
| 85 | + System.out.println("Goodbye!"); |
| 86 | + System.exit(0); |
| 87 | + break; |
| 88 | + default: |
| 89 | + System.out.println("Invalid option. Please select a valid option."); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments