-
Notifications
You must be signed in to change notification settings - Fork 0
/
Contact.java
82 lines (73 loc) · 2.83 KB
/
Contact.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
import java.util.ArrayList;
import java.util.Scanner;
class Contact {
private String name;
private String phoneNumber;
public Contact(String name, String phoneNumber) {
this.name = name;
this.phoneNumber = phoneNumber;
}
public String getName() {
return name;
}
public String getPhoneNumber() {
return phoneNumber;
}
@Override
public String toString() {
return "Name: " + name + ", Phone Number: " + phoneNumber;
}
}
public class ContactManagementSystem {
private static ArrayList<Contact> contacts = new ArrayList<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("Contact Management System");
System.out.println("1. Add Contact");
System.out.println("2. Display Contacts");
System.out.println("3. Search Contacts");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline character
switch (choice) {
case 1:
System.out.print("Enter name: ");
String name = scanner.nextLine();
System.out.print("Enter phone number: ");
String phoneNumber = scanner.nextLine();
contacts.add(new Contact(name, phoneNumber));
System.out.println("Contact added successfully!");
break;
case 2:
System.out.println("Contacts:");
for (Contact contact : contacts) {
System.out.println(contact);
}
break;
case 3:
System.out.print("Enter name or phone number to search: ");
String query = scanner.nextLine();
boolean found = false;
for (Contact contact : contacts) {
if (contact.getName().equalsIgnoreCase(query) || contact.getPhoneNumber().equals(query)) {
System.out.println("Contact found: " + contact);
found = true;
}
}
if (!found) {
System.out.println("No contact found with the given name or phone number.");
}
break;
case 4:
System.out.println("Exiting...");
scanner.close();
return;
default:
System.out.println("Invalid choice! Please enter a number between 1 and 4.");
}
System.out.println();
}
}
}