-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhoneBook.java
More file actions
276 lines (249 loc) · 11.7 KB
/
PhoneBook.java
File metadata and controls
276 lines (249 loc) · 11.7 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import java.util.Scanner;
public class PhoneBook {
LinkedList<Contact> contacts;
LinkedList<Event> events;
public Scanner input = new Scanner(System.in);
public PhoneBook() {
contacts = new LinkedList<>();
events = new LinkedList<>();
}
public void display() {
int choice = 0;
System.out.println("Welcome to the Linked Tree Phonebook!\n");
do {
System.out.println(
"Please choose an option:\n" +
"1. Add a contact\n" +
"2. Search for a contact\n" +
"3. Delete a contact\n" +
"4. Schedule an event\n" +
"5. Print event details\n" +
"6. Print contacts by first name\n" +
"7. Print all events alphabetically\n" +
"8. Exit\n");
System.out.println("Enter your choice:");
choice = input.nextInt();// Assuming the user is reasnoable user
switch (choice) {// start big switch
case 1:
System.out.print("Enter the contact's name: ");
String name = input.next();
name += input.nextLine();
System.out.print("\nEnter the contact's phone number: ");
String phoneNumber = input.next();
System.out.print("\nEnter the contact's email address: ");
String email = input.next();
System.out.print("Enter the contact's address: ");
String address = input.next();
address += input.nextLine();
System.out.print("Enter the contact's birthday: ");
String birthday = input.next();
System.out.print("Enter any notes for the contact: ");
String notes = input.next();
notes += input.nextLine();
Contact x = new Contact(name, phoneNumber, email, address, birthday, notes);
contacts.add(x);
break;
case 2:
System.out.print("Enter search criteria:\n" +
"1. Name\n" +
"2. Phone Number\n" +
"3. Email Address\n" +
"4. Address\n" +
"5. Birthday\n" +
"Enter your choice: ");
int choice2 = input.nextInt();
switch (choice2) {// start small switch
case 1:
System.out.println("Enter the contact's name:");
name = input.next();
name += input.nextLine();
Contact c = contacts.SearchContact(contacts.getHead(), name, choice2);
if (c != null) {
System.out.println("contact found!");
System.out.println(c.toString());
} else
System.out.println("contact not found!");
break;
case 2:
System.out.println("Enter the contact's phone number:");
phoneNumber = input.next();
c = contacts.SearchContact(contacts.getHead(), phoneNumber, choice2);
if (c != null) {
System.out.println("contact found!");
System.out.println(c.toString());
} else
System.out.println("contact not found!");
break;
case 3:
System.out.println("Enter the contact's email address:");
email = input.next();
if (contacts.SearchContact(contacts.getHead(), email, 3) != null) {
printContact(email);
break;
} else {
System.out.println("No Contacts found!");
break;
}
case 4:
System.out.println("Enter the contact's address: ");
address = input.next();
address += input.nextLine();
if (contacts.SearchContact(contacts.getHead(), address, 4) != null) {
printContact(address);
break;
} else {
System.out.println("No Contacts found!");
break;
}
case 5:
System.out.println("Enter the contact's birthday: ");
birthday = input.next();
if (contacts.SearchContact(contacts.getHead(), birthday, 5) != null) {
printContact(birthday);
break;
} else {
System.out.println("No Contacts found!");
break;
}
default:
System.out.println("Wrong number, please do it again");
}// end small switch
break;
case 3:
System.out.println("Enter phone number to delete a contact");
String phone = input.next();
Contact numberPhon = contacts.SearchContact(contacts.getHead(), phone, 2);
if (numberPhon == null) {
System.out.println("Contact Number Not Found!");
break;
}
events.RemoveEvent(numberPhon.getContactName());
contacts.RemoveContact(phone);
break;
case 4:
System.out.print("Enter event title: ");
String eventTitle = input.next();
eventTitle += input.nextLine();
System.out.print("Enter contact name: ");
String contactName = input.next();
contactName += input.nextLine();
System.out.print("Enter event date and time (MM/DD/YYYY HH:MM): ");
String dateTime = input.next();
dateTime += input.nextLine();
System.out.print("Enter event location: ");
String location = input.next();
location += input.nextLine();
Contact contact = contacts.SearchContact(contacts.getHead(), contactName, 1);
if (contact == null) {
System.out.println("The contact does not exist!");
break;
}
Event event1 = new Event(eventTitle, dateTime, location, contact);
events.add(event1);
break;
case 5:
System.out.println("Enter search criteria:\r\n" + //
"1. contact name\r\n" + //
"2. Event tittle");
choice2 = input.nextInt();
switch (choice2) {
case 1:
System.out.println("Enter the Contact Name:");
contactName = input.next();
contactName += input.nextLine();
Event E = (Event) events.SearchEvent(events.getHead(), contactName, choice2);
System.out.println(E.toString());
break;
case 2:
System.out.println("Enter the event title:");
eventTitle = input.next();
eventTitle += input.nextLine();
if (events.SearchEvent(events.getHead(), eventTitle, 2) != null)
PrintEventTitle(eventTitle);
break;
default:
System.out.println("Wrong number, please do it again");
}
break;
case 6:
System.out.println("Enter the First Name you want to search for:");
String FName = input.next();
printByFirstName(FName);
break;
case 7:
PrintAllEvent();
break;
case 8:
break;
default:
System.out.println("Wrong number, please do it again");
} // end big switch
} while (choice != 8);
System.out.println("Goodbye !");
}
private void printContact(String s) {// Print all contact that have the same S:Can be Email or Adrress or Birthday.
contacts.findfirst();
while (contacts.last() == false) {
if (contacts.retrieve().getEmail().equalsIgnoreCase(s)
|| contacts.retrieve().getAddress().equalsIgnoreCase(s)
|| contacts.retrieve().getBirthday().equalsIgnoreCase(s)) {
System.out.println("contact found");
System.out.println(contacts.retrieve().toString());
}
contacts.findnext();
}
if (contacts.retrieve().getEmail().equalsIgnoreCase(s)
|| contacts.retrieve().getAddress().equalsIgnoreCase(s)
|| contacts.retrieve().getBirthday().equalsIgnoreCase(s)) {
System.out.println("contact found");
System.out.println(contacts.retrieve().toString());
}
}
private void PrintAllEvent() {
if (events.empty()) {
System.out.println("There is No Event right now");
return;
}
events.findfirst();
while (events.last() == false) {
System.out.println(events.retrieve().toString());
events.findnext();
}
System.out.println(events.retrieve().toString());
}
private String extractFirstName(String name) {
int index = name.indexOf(' ');
if (index != -1) {
return name.substring(0, index);
}
return name;
}
private void printByFirstName(String name) {
if (contacts.empty()) {
System.out.println("No Contacts found!");
return;
}
Node<Contact> current = contacts.getHead();
while (current != null) {
String firstName = extractFirstName(current.data.getContactName());
if (name.compareToIgnoreCase(firstName) == 0) {
System.out.println(current.data.toString() + "\n");
}
current = current.next;
}
}
private void PrintEventTitle(String title) {// Print the Event that has the same Title
if (events.empty())
System.out.println("No Contacts found!");
events.findfirst();
while (events.last() == false) {
if (events.retrieve().getEventTitle().equalsIgnoreCase(title)) {
System.out.println(events.retrieve().toString());
}
events.findnext();
}
if (events.retrieve().getEventTitle().equalsIgnoreCase(title)) {
System.out.println(events.retrieve().toString());
}
}
}