-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContactBook.java
More file actions
124 lines (113 loc) · 4.51 KB
/
Copy pathContactBook.java
File metadata and controls
124 lines (113 loc) · 4.51 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
import java.util.*;
public class ContactBook {
// Inner class to store contact details
static class Contact {
String name;
String phone;
String email;
String address;
int age;
String gender;
String dob;
String profilePicture;
String resume;
// Constructor to initialize contact details
Contact(String name, String phone, String email, String address, int age, String gender, String dob, String profilePicture, String resume) {
this.name = name;
this.phone = phone;
this.email = email;
this.address = address;
this.age = age;
this.gender = gender;
this.dob = dob;
this.profilePicture = profilePicture;
this.resume = resume;
}
public String toString() {
return "Name: " + name + ", Phone: " + phone + ", Email: " + email + ", Address: " + address +
", Age: " + age + ", Gender: " + gender + ", DOB: " + dob + ", Profile Picture: " + profilePicture +
", Resume: " + resume;
}
}
static Scanner scanner = new Scanner(System.in);
static List<Contact> contacts = new ArrayList<>();
// Main method to run the contact book
public static void main(String[] args) {
while (true) {
System.out.println("\n--- Contact Book Menu ---");
System.out.println("1. Add Contact");
System.out.println("2. View Contacts");
System.out.println("3. Delete 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:
viewContacts();
break;
case 3:
deleteContact();
break;
case 4:
System.out.println("Exiting Contact Book...");
return;
default:
System.out.println("Invalid option. Try again.");
}
}
}
// Function to add a new contact with all details
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();
System.out.print("Enter address: ");
String address = scanner.nextLine();
System.out.print("Enter age: ");
int age = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Enter gender (Male/Female/Other): ");
String gender = scanner.nextLine();
System.out.print("Enter date of birth (DD/MM/YYYY): ");
String dob = scanner.nextLine();
System.out.print("Enter profile picture URL: ");
String profilePicture = scanner.nextLine();
System.out.print("Enter resume URL: ");
String resume = scanner.nextLine();
// Create a new Contact object and add to the list
contacts.add(new Contact(name, phone, email, address, age, gender, dob, profilePicture, resume));
System.out.println("Contact added successfully!");
}
// Function to display all contacts with all details
static void viewContacts() {
if (contacts.isEmpty()) {
System.out.println("No contacts available.");
} else {
System.out.println("Contacts List:");
for (int i = 0; i < contacts.size(); i++) {
System.out.println((i + 1) + ". " + contacts.get(i));
}
}
}
// Function to delete a contact
static void deleteContact() {
viewContacts();
if (!contacts.isEmpty()) {
System.out.print("Enter contact number to delete: ");
int index = scanner.nextInt() - 1;
if (index >= 0 && index < contacts.size()) {
contacts.remove(index);
System.out.println("Contact deleted.");
} else {
System.out.println("Invalid contact number.");
}
}
}
}