-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContactBook.java
More file actions
53 lines (46 loc) · 1.73 KB
/
Copy pathContactBook.java
File metadata and controls
53 lines (46 loc) · 1.73 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
// ContactBook.java
import java.util.ArrayList;
import java.util.List;
public class ContactBook {
private List<Contact> contacts;
public ContactBook() {
this.contacts = new ArrayList<>();
}
public void addContact(String name, String phoneNumber, String email) {
Contact contact = new Contact(name, phoneNumber, email);
contacts.add(contact);
System.out.println("Contact added successfully.");
}
public void viewContacts() {
if (contacts.isEmpty()) {
System.out.println("Contact book is empty.");
} else {
System.out.println("Contact List:");
for (int i = 0; i < contacts.size(); i++) {
Contact contact = contacts.get(i);
System.out.println((i + 1) + ". Name: " + contact.getName() +
", Phone: " + contact.getPhoneNumber() +
", Email: " + contact.getEmail());
}
}
}
public void editContact(int index, String name, String phoneNumber, String email) {
if (index >= 0 && index < contacts.size()) {
Contact contact = contacts.get(index);
contact.setName(name);
contact.setPhoneNumber(phoneNumber);
contact.setEmail(email);
System.out.println("Contact updated successfully.");
} else {
System.out.println("Invalid contact index.");
}
}
public void deleteContact(int index) {
if (index >= 0 && index < contacts.size()) {
Contact contact = contacts.remove(index);
System.out.println("Contact deleted: " + contact.getName());
} else {
System.out.println("Invalid contact index.");
}
}
}