-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContactManagementSystem.java.java
162 lines (140 loc) · 4.99 KB
/
ContactManagementSystem.java.java
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
import java.util.ArrayList;
import java.util.Scanner;
// Class representing a Contact
class Contact {
private String name;
private String phone;
private String email;
// Constructor
public Contact(String name, String phone, String email) {
this.name = name;
this.phone = phone;
this.email = email;
}
// Getters and Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
// Method to display contact details
public void displayContact() {
System.out.println("Name: " + name);
System.out.println("Phone: " + phone);
System.out.println("Email: " + email);
}
}
// Main class for the Contact Management System
public class ContactManagementSystem {
private static ArrayList<Contact> contactList = new ArrayList<>();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
System.out.println("\n--- Contact Management System ---");
System.out.println("1. Add Contact");
System.out.println("2. View All Contacts");
System.out.println("3. Update Contact");
System.out.println("4. Delete Contact");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
addContact();
break;
case 2:
viewAllContacts();
break;
case 3:
updateContact();
break;
case 4:
deleteContact();
break;
case 5:
System.out.println("Exiting the system...");
return;
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
// Method to add a new contact
private static void addContact() {
System.out.print("Enter name: ");
String name = scanner.nextLine();
System.out.print("Enter phone number: ");
String phone = scanner.nextLine();
System.out.print("Enter email: ");
String email = scanner.nextLine();
Contact contact = new Contact(name, phone, email);
contactList.add(contact);
System.out.println("Contact added successfully!");
}
// Method to view all contacts
private static void viewAllContacts() {
if (contactList.isEmpty()) {
System.out.println("No contacts found.");
} else {
System.out.println("\n--- Contact List ---");
for (int i = 0; i < contactList.size(); i++) {
System.out.println("Contact " + (i + 1) + ":");
contactList.get(i).displayContact();
System.out.println();
}
}
}
// Method to update a contact
private static void updateContact() {
System.out.print("Enter the contact number to update (1, 2, 3...): ");
int contactIndex = scanner.nextInt() - 1;
scanner.nextLine(); // Consume newline
if (contactIndex >= 0 && contactIndex < contactList.size()) {
Contact contact = contactList.get(contactIndex);
System.out.print("Enter new name (leave empty to keep current): ");
String name = scanner.nextLine();
if (!name.isEmpty()) {
contact.setName(name);
}
System.out.print("Enter new phone (leave empty to keep current): ");
String phone = scanner.nextLine();
if (!phone.isEmpty()) {
contact.setPhone(phone);
}
System.out.print("Enter new email (leave empty to keep current): ");
String email = scanner.nextLine();
if (!email.isEmpty()) {
contact.setEmail(email);
}
System.out.println("Contact updated successfully!");
} else {
System.out.println("Invalid contact number.");
}
}
// Method to delete a contact
private static void deleteContact() {
System.out.print("Enter the contact number to delete (1, 2, 3...): ");
int contactIndex = scanner.nextInt() - 1;
scanner.nextLine(); // Consume newline
if (contactIndex >= 0 && contactIndex < contactList.size()) {
contactList.remove(contactIndex);
System.out.println("Contact deleted successfully!");
} else {
System.out.println("Invalid contact number.");
}
}
}