-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContactBookProgram.java
More file actions
80 lines (72 loc) · 3.45 KB
/
Copy pathContactBookProgram.java
File metadata and controls
80 lines (72 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// ContactBookProgram.java
import java.util.Scanner;
public class ContactBookProgram {
public static void main(String[] args) {
// Create an instance of the ContactBook
ContactBook contactBook = new ContactBook();
// Create a Scanner object to read user input
Scanner scanner = new Scanner(System.in);
int choice = -1;
// The loop that runs until the user chooses to exit
while (choice != 0) {
System.out.println("----- Contact Book -----");
System.out.println("1. Add a contact");
System.out.println("2. View contacts");
System.out.println("3. Edit a contact");
System.out.println("4. Delete a contact");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
// Check if the next input is an integer
if (scanner.hasNextInt()) {
choice = scanner.nextInt();
scanner.nextLine(); // Consume newline character
System.out.println();
// Switch case to perform different actions based on the choice
switch (choice) {
case 1:
System.out.print("Enter the name: ");
String name = scanner.nextLine();
System.out.print("Enter the phone number: ");
String phoneNumber = scanner.nextLine();
System.out.print("Enter the email: ");
String email = scanner.nextLine();
contactBook.addContact(name, phoneNumber, email);
System.out.println();
break;
case 2:
contactBook.viewContacts();
System.out.println();
break;
case 3:
System.out.print("Enter the index of the contact to edit: ");
int editIndex = scanner.nextInt();
scanner.nextLine(); // Consume newline character
System.out.print("Enter the new name: ");
String newName = scanner.nextLine();
System.out.print("Enter the new phone number: ");
String newPhoneNumber = scanner.nextLine();
System.out.print("Enter the new email: ");
String newEmail = scanner.nextLine();
contactBook.editContact(editIndex - 1, newName, newPhoneNumber, newEmail);
System.out.println();
break;
case 4:
System.out.print("Enter the index of the contact to delete: ");
int deleteIndex = scanner.nextInt();
scanner.nextLine(); // Consume newline character
contactBook.deleteContact(deleteIndex - 1);
System.out.println();
break;
case 5:
return; // Exit the program
default:
System.out.println("Invalid choice. Please try again.\n");
}
} else {
System.out.println("Invalid input. Please try again.\n");
scanner.nextLine(); // Consume invalid input
}
}
scanner.close();
}
}