forked from saisiddeswar/gitworkshop-batch2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanoj.java
More file actions
99 lines (86 loc) · 2.68 KB
/
manoj.java
File metadata and controls
99 lines (86 loc) · 2.68 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
import java.util.ArrayList;
import java.util.Scanner;
class Contact {
private String name;
private String phone;
public Contact(String name, String phone) {
this.name = name;
this.phone = phone;
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
@Override
public String toString() {
return name + " - " + phone;
}
}
public class ContactManager {
private static ArrayList<Contact> contacts = new ArrayList<>();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
boolean running = true;
while (running) {
System.out.println("\nContact Manager");
System.out.println("1. Add Contact");
System.out.println("2. List Contacts");
System.out.println("3. Search Contact");
System.out.println("4. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine(); // consume newline
switch (choice) {
case 1:
addContact();
break;
case 2:
listContacts();
break;
case 3:
searchContact();
break;
case 4:
running = false;
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid option, try again.");
}
}
}
private static void addContact() {
System.out.print("Enter name: ");
String name = scanner.nextLine();
System.out.print("Enter phone: ");
String phone = scanner.nextLine();
contacts.add(new Contact(name, phone));
System.out.println("Contact added.");
}
private static void listContacts() {
if (contacts.isEmpty()) {
System.out.println("No contacts found.");
return;
}
System.out.println("Contacts:");
for (Contact c : contacts) {
System.out.println(c);
}
}
private static void searchContact() {
System.out.print("Enter name to search: ");
String searchName = scanner.nextLine();
boolean found = false;
for (Contact c : contacts) {
if (c.getName().equalsIgnoreCase(searchName)) {
System.out.println("Found: " + c);
found = true;
}
}
if (!found) {
System.out.println("No contact found with that name.");
}
}
}