-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContactManagementApp.java
More file actions
200 lines (167 loc) · 6.75 KB
/
Copy pathContactManagementApp.java
File metadata and controls
200 lines (167 loc) · 6.75 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import java.util.List;
import java.util.Scanner;
public class ContactManagementApp {
private ContactManager manager;
private Scanner scanner;
public ContactManagementApp() {
manager = new ContactManager();
scanner = new Scanner(System.in);
}
public void run() {
System.out.println("=== Contact Management System ===");
boolean running = true;
while (running) {
displayMenu();
String choice = scanner.nextLine().trim();
switch (choice) {
case "1":
addContact();
break;
case "2":
viewAllContacts();
break;
case "3":
searchContact();
break;
case "4":
updateContact();
break;
case "5":
deleteContact();
break;
case "6":
running = false;
System.out.println("Goodbye!");
break;
default:
System.out.println("Invalid option. Please try again.");
}
System.out.println();
}
}
private void displayMenu() {
System.out.println("\n--- Main Menu ---");
System.out.println("1. Add Contact");
System.out.println("2. View All Contacts");
System.out.println("3. Search Contact");
System.out.println("4. Update Contact");
System.out.println("5. Delete Contact");
System.out.println("6. Exit");
System.out.print("Choose an option: ");
}
private void addContact() {
System.out.println("\n--- Add New Contact ---");
try {
System.out.print("Name: ");
String name = scanner.nextLine().trim();
System.out.print("Phone (10 digits or XXX-XXX-XXXX): ");
String phone = scanner.nextLine().trim();
System.out.print("Email: ");
String email = scanner.nextLine().trim();
System.out.print("Address: ");
String address = scanner.nextLine().trim();
String id = manager.createContact(name, phone, email, address);
System.out.println("✓ Contact added successfully! ID: " + id);
} catch (ValidationException e) {
System.out.println("✗ Error: " + e.getMessage());
}
}
private void viewAllContacts() {
List<Contact> contacts = manager.getAllContacts();
System.out.println("\n--- All Contacts (" + contacts.size() + ") ---");
if (contacts.isEmpty()) {
System.out.println("No contacts found.");
} else {
for (Contact contact : contacts) {
System.out.println(contact);
}
}
}
private void searchContact() {
System.out.println("\n--- Search Contact ---");
System.out.println("1. Search by Name");
System.out.println("2. Search by Phone");
System.out.print("Choose search type: ");
String choice = scanner.nextLine().trim();
List<Contact> results = null;
if (choice.equals("1")) {
System.out.print("Enter name to search: ");
String name = scanner.nextLine().trim();
results = manager.searchByName(name);
} else if (choice.equals("2")) {
System.out.print("Enter phone to search: ");
String phone = scanner.nextLine().trim();
results = manager.searchByPhone(phone);
} else {
System.out.println("Invalid choice.");
return;
}
System.out.println("\n--- Search Results (" + results.size() + ") ---");
if (results.isEmpty()) {
System.out.println("No contacts found.");
} else {
for (Contact contact : results) {
System.out.println(contact);
}
}
}
private void updateContact() {
System.out.println("\n--- Update Contact ---");
System.out.print("Enter Contact ID: ");
String id = scanner.nextLine().trim();
Contact existing = manager.getContact(id);
if (existing == null) {
System.out.println("✗ Contact not found.");
return;
}
System.out.println("Current: " + existing);
System.out.println("Enter new details (press Enter to keep current value):");
try {
System.out.print("Name [" + existing.getName() + "]: ");
String name = scanner.nextLine().trim();
if (name.isEmpty()) name = existing.getName();
System.out.print("Phone [" + existing.getPhone() + "]: ");
String phone = scanner.nextLine().trim();
if (phone.isEmpty()) phone = existing.getPhone();
System.out.print("Email [" + existing.getEmail() + "]: ");
String email = scanner.nextLine().trim();
if (email.isEmpty()) email = existing.getEmail();
System.out.print("Address [" + existing.getAddress() + "]: ");
String address = scanner.nextLine().trim();
if (address.isEmpty()) address = existing.getAddress();
if (manager.updateContact(id, name, phone, email, address)) {
System.out.println("✓ Contact updated successfully!");
} else {
System.out.println("✗ Update failed.");
}
} catch (ValidationException e) {
System.out.println("✗ Error: " + e.getMessage());
}
}
private void deleteContact() {
System.out.println("\n--- Delete Contact ---");
System.out.print("Enter Contact ID: ");
String id = scanner.nextLine().trim();
Contact contact = manager.getContact(id);
if (contact == null) {
System.out.println("✗ Contact not found.");
return;
}
System.out.println("Contact: " + contact);
System.out.print("Are you sure you want to delete? (yes/no): ");
String confirm = scanner.nextLine().trim().toLowerCase();
if (confirm.equals("yes")) {
if (manager.deleteContact(id)) {
System.out.println("✓ Contact deleted successfully!");
} else {
System.out.println("✗ Delete failed.");
}
} else {
System.out.println("Delete cancelled.");
}
}
public static void main(String[] args) {
ContactManagementApp app = new ContactManagementApp();
app.run();
}
}