-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
257 lines (225 loc) · 13.6 KB
/
Main.java
File metadata and controls
257 lines (225 loc) · 13.6 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
import java.util.Scanner;
import java.util.InputMismatchException;
public class Main {
public static void main(String[] args)
{
PhoneBook phoneBook = new PhoneBook();
try (Scanner input = new Scanner(System.in)) {
System.out.println("Welcome to the BST Phonebook!");
int choice = 0;
do {
System.out.println("\nPlease choose an option:");
System.out.println("1. Add a contact");
System.out.println("2. Search for a contact");
System.out.println("3. Delete a contact");
System.out.println("4. Schedule an event/appointment");
System.out.println("5. Print event details");
System.out.println("6. Print contacts by first name");
System.out.println("7. Print all events alphabetically");
System.out.println("8. Exit");
System.out.print("Enter your choice: ");
try {
choice = input.nextInt();
input.nextLine();
switch (choice) {
case 1: {
System.out.print("Enter the contact's name: ");
String contactName = input.nextLine();
System.out.print("Enter the contact's phone number: ");
String phoneNumber = input.nextLine();
System.out.print("Enter the contact's email address: ");
String emailAddress = input.nextLine();
System.out.print("Enter the contact's address: ");
String address = input.nextLine();
System.out.print("Enter the contact's birthday (MM/DD/YYYY): ");
String birthday = input.nextLine();
System.out.print("Enter any notes for the contact: ");
String notes = input.nextLine();
if (contactName.trim().isEmpty() || phoneNumber.trim().isEmpty()) {
System.out.println("Error: Contact name and phone number cannot be empty.");
} else {
Contact contact = new Contact(contactName, phoneNumber, emailAddress, address, birthday, notes);
phoneBook.addContact(contact);
}
break;
}
case 2: {
System.out.println("\nEnter search criteria:");
System.out.println("1. Name");
System.out.println("2. Phone Number");
System.out.println("3. Email Address");
System.out.println("4. Address");
System.out.println("5. Birthday");
System.out.print("Enter your choice: ");
int searchChoice = input.nextInt();
input.nextLine();
String searchValue = "";
boolean validInput = true;
switch (searchChoice) {
case 1: System.out.print("Enter the contact's name: "); searchValue = input.nextLine(); break;
case 2: System.out.print("Enter the phone number: "); searchValue = input.nextLine(); break;
case 3: System.out.print("Enter the email address: "); searchValue = input.nextLine(); break;
case 4: System.out.print("Enter the address: "); searchValue = input.nextLine(); break;
case 5: System.out.print("Enter the birthday: "); searchValue = input.nextLine(); break;
default: System.out.println("Invalid search criteria."); validInput = false; break;
}
if (validInput && !searchValue.trim().isEmpty()) {
if (searchChoice == 1) {
Contact found = phoneBook.searchContactBasedOnName(searchValue);
if (found != null) {
System.out.println("\nContact found:");
System.out.println(found);
} else {
System.out.println("Contact not found.");
}
} else {
BST<Contact> results = phoneBook.searchContactInfo(searchValue, searchChoice);
if (results.isEmpty()) {
System.out.println("No contacts found matching the criteria.");
} else {
System.out.println("\nMatching contacts found:");
results.printInOrder();
}
}
} else if (validInput) {
System.out.println("Search value cannot be empty.");
}
break;
}
case 3: {
System.out.print("Enter the name of the contact to delete: ");
String nameToDelete = input.nextLine();
if (nameToDelete.trim().isEmpty()) {
System.out.println("Contact name cannot be empty.");
} else {
phoneBook.deleteContact(nameToDelete);
}
break;
}
case 4: {
System.out.println("\nEnter type:");
System.out.println("1. Event");
System.out.println("2. Appointment");
System.out.print("Enter your choice: ");
int eventTypeChoice = input.nextInt();
input.nextLine();
boolean isEvent = (eventTypeChoice == 1);
String typeStr = isEvent ? "event" : "appointment";
System.out.print("Enter " + typeStr + " title: ");
String title = input.nextLine();
BST<Contact> contactsInEventBST = new BST<>();
if (isEvent) {
System.out.print("Enter contacts' names separated by commas: ");
String contactNamesInput = input.nextLine();
String[] names = contactNamesInput.split(",");
boolean allContactsFound = true;
for (String name : names) {
Contact contact = phoneBook.searchContactBasedOnName(name.trim());
if (contact != null) {
contactsInEventBST.add(contact.getContactName(), contact);
} else {
System.out.println("Warning: Contact not found: '" + name.trim() + "'. Skipping.");
allContactsFound = false;
}
}
if (contactsInEventBST.isEmpty() && names.length > 0) {
System.out.println("Error: No valid contacts found for the event.");
break;
}
} else { // Appointment
System.out.print("Enter contact name: ");
String contactName = input.nextLine();
Contact contact = phoneBook.searchContactBasedOnName(contactName.trim());
if (contact != null) {
contactsInEventBST.add(contact.getContactName(), contact);
} else {
System.out.println("Error: Contact '" + contactName.trim() + "' not found. Cannot schedule appointment.");
break;
}
}
System.out.print("Enter " + typeStr + " date and time (MM/DD/YYYY HH:MM): ");
String dateTime = input.nextLine();
System.out.print("Enter " + typeStr + " location: ");
String location = input.nextLine();
if (!title.trim().isEmpty() && !dateTime.trim().isEmpty() && !contactsInEventBST.isEmpty()) {
phoneBook.scheduleEvent(isEvent, title, dateTime, location, contactsInEventBST);
} else {
System.out.println("Scheduling cancelled due to missing information or contacts.");
}
break;
}
case 5: {
System.out.println("\nSearch for event by:");
System.out.println("1. Contact name");
System.out.println("2. Event title");
System.out.print("Enter your choice: ");
int searchChoice = input.nextInt();
input.nextLine();
String searchValue = "";
boolean validInput = true;
if (searchChoice == 1) {
System.out.print("Enter contact name: ");
searchValue = input.nextLine();
} else if (searchChoice == 2) {
System.out.print("Enter event title: ");
searchValue = input.nextLine();
} else {
System.out.println("Invalid choice.");
validInput = false;
}
if (validInput && !searchValue.trim().isEmpty()) {
Event foundEvent = phoneBook.searchEventsByTitleOrContactName(searchChoice, searchValue);
if (foundEvent != null) {
System.out.println("\nEvent details:");
System.out.println(foundEvent);
} else {
System.out.println("No event found matching the criteria.");
}
} else if (validInput) {
System.out.println("Search value cannot be empty.");
}
break;
}
case 6: {
System.out.print("Enter the first name: ");
String firstName = input.nextLine();
if (firstName.trim().isEmpty()) {
System.out.println("First name cannot be empty.");
} else {
BST<Contact> contactsFound = phoneBook.findContactsByFirstName(firstName);
if (contactsFound.isEmpty()) {
System.out.println("No contacts found with the first name '" + firstName + "'.");
} else {
System.out.println("\nContacts found with first name '" + firstName + "':");
contactsFound.printInOrder();
}
}
break;
}
case 7: {
phoneBook.printAllEventsAlphabetically();
break;
}
case 8: {
System.out.println("Goodbye!");
break;
}
default: {
System.out.println("Invalid choice. Please try again.");
break;
}
}
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter a number.");
input.nextLine();
choice = 0;
} catch (Exception e) {
System.out.println("An unexpected error occurred: " + e.getMessage());
e.printStackTrace();
choice = 0;
}
System.out.println();
} while (choice != 8);
}
}
}