import java.util.
*;
import java.util.Scanner;
public class PhoneBook {
public static Scanner input = new Scanner(System.in);
public static BST<String, Contact> contactsTree = new BST<String,
Contact>(); //BST
public static LinkedList<Event> events = new LinkedList<Event>();
//---------------------------------------------------------------------------------
//this method adds a contact after checking existing and phonenumber uniquness
public static void AddContact() {
Contact c = new Contact();
System.out.print("Enter the contact name: ");//user unput
c.name = input.nextLine();
if (!contactsTree.empty() && contactsTree.findkey(c.name)) {
System.out.println("Contact already exist!");
return;
}
System.out.print("Enter the contact's phone number:");
c.phoneNumber = input.nextLine();
if (!contactsTree.empty()){
if(contactsTree.searchPhoneNumber(c.phoneNumber)) {
System.out.println("Contact already exist!");
return;
}}
System.out.print("Enter the contact's email address: ");
c.emailAddress = input.nextLine();
System.out.print("Enter the contact's address: ");
c.address = input.nextLine();
System.out.print("Enter the contact's birthday: ");
c.birthday = input.nextLine();
System.out.print("Enter any notes for the contact: ");
c.notes = input.nextLine();
if (contactsTree.insert(c.name, c)) {
System.out.println("Contact added successfully!");
}
}
//---------------------------------------------------------------------------------
//searching for a contact by users criteria
public static void SearchContact(int choice) {
int choice1 = choice;
int counter = 0;
int counter2 = 0;
if (contactsTree.empty()) {
System.out.println("Contact not found!");
return;
} else {
if (choice == 1) {
System.out.print("Enter the contact's name: ");
String name = input.nextLine();
if (contactsTree.findkey(name)) {
System.out.println("Contact Found!");
System.out.println(contactsTree.retrieve());
}
} else if (choice == 2) {
System.out.print("Enter the contact's phone number:");
String phonenumber = input.nextLine();
if(contactsTree.searchPhoneNumber(phonenumber));
System.out.println("Contact Found!");
System.out.println(contactsTree.retrieve());
} else if (choice == 3) {
System.out.print("Enter the contact's email address: ");
String emailaddress = input.nextLine();
contactsTree.searchEmail(emailaddress);
}else if (choice == 4) {
System.out.print("Enter the contact's address: ");
String address = input.nextLine();
contactsTree.searchAddress(address);
else if (choice == 5) {
System.out.print("Enter the contact's Birthday: ");
String birthday = input.nextLine();
contactsTree.searchBirthday(birthday);
}
}
}
//---------------------------------------------------------------------------------
//deletes a contact by his or her name is it is exists
public static void DeleteContact() {
Contact c = new Contact();
System.out.print("Enter the contact name: ");
c.name = input.nextLine();
if (!contactsTree.empty() ){
if (!contactsTree.findkey(c.name)) {
System.out.println("Contact not found!");
return;
} else {
c = contactsTree.retrieve();
contactsTree.remove_key(c.name);
deleteEvent(c);
}
}
else {
System.out.println("Empty Contacts List! , cannot Delete!");
}
}
//---------------------------------------------------------------------------------
//scheduling an event after checking contact existence and checking contacts,
and checking that there is no date and time conflicts
public static void ScheduleEvent() {
Contact c = new Contact();
Event e = new Event();
System.out.println("Enter Type:");
System.out.println("1.Event");
System.out.println("2.Appointment");
System.out.print("Enter your choice:");
int choice = input.nextInt();
input.nextLine();
boolean eventFlag = false;
boolean AddedFlag = false;
//Attributes of an event is taken by the users input
if (choice == 1) { //Event
e.EventType = true;
System.out.print("Enter event title: ");
e.title = input.nextLine();
System.out.print("Enter contacts name seperated by comma: ");
String inputs = input.nextLine();
String names[] = inputs.split(",");
System.out.print("Enter the event date and time (MM/DD/YYYY) hh:mm :");
e.date = input.next();
e.time = input.next();
System.out.print("Enter event location: ");
input.nextLine();
e.location = input.nextLine();
for (int i = 0; i < names.length; i++) {
c.name = names[i].trim();
if (!contactsTree.empty() && contactsTree.findkey(c.name)) {
c = contactsTree.retrieve();
AddedFlag = c.addEvent(e);
if (AddedFlag) {
contactsTree.update(c.name, c);
if (!events.empty() && events.search(e)) {
Event eventFound = events.retrieve();
if ((eventFound.date.compareTo(e.date) == 0) &&
(eventFound.time.compareTo(e.time) == 0)
&& (eventFound.location.compareTo(e.location)
== 0) && (eventFound.EventType == e.EventType)) {
eventFound.contactsNames.insertSort(c.name);
events.update(eventFound);
eventFlag = true;
}
}
if (!eventFlag) {
e.contactsNames.insertSort(c.name);
events.insertSort(e);
}
System.out.println("Event was succesfully scheduled for " +
c.name);
} else {
System.out.println("There's a conflict!");
}
} else {
System.out.println("Contact " + c.name + " not Found!");
}
}
} // end choice 1 "Event"
else if (choice == 2) { // Appointment
e.EventType = false;
System.out.print("Enter event title: ");
e.title = input.nextLine();
System.out.print("Enter Contact name:");
c.name= input.nextLine();
if (!contactsTree.empty() && contactsTree.findkey(c.name)) {
c = contactsTree.retrieve();
System.out.print("Enter the Appointment date and time (MM/DD/YYYY)
hh:mm :");
e.date = input.next();
e.time=input.next();
System.out.print("Enter event location: ");
input.nextLine();
e.location = input.nextLine();
if (!events.empty() && events.search(e)) {
Event eventFound = events.retrieve();
if (e.compareToSame(eventFound)) {
System.out.println("Appointment already scheduled , cannot
add more than one Contact!");
}
} else {
AddedFlag = c.addEvent(e);
if (AddedFlag) {
contactsTree.update(c.name, c);
e.contactsNames.insertSort(c.name);
events.insertSort(e);
System.out.println("Appointment scheduled successfully!");
} else {
System.out.println("Contact has conflicting Appointment !
");
}
}
} else {
System.out.println("Contact not Found! ");
}
} else {
System.out.println("Wrong choice !");
}
} // end method schedule
//---------------------------------------------------------------------------------
----------
//prints events based by usres criteria(evnet title, or contact name)
public static void PrintEvent(int choice) {
if (choice == 1) { // By Contact Name
Contact c = new Contact();
System.out.print("Enter the contact name: ");
c.name = input.nextLine();
if (!contactsTree.empty()) {
if (contactsTree.findkey(c.name)) {
System.out.println("Contact found !");
c = contactsTree.retrieve();
c.events.findFirst();
for (int i = 0; i < c.events.size; i++) {
Event e = c.events.retrieve();
if (!events.empty() && events.search(e)) {
System.out.println(c.events.retrieve());
}
c.events.findNext();
}
if (c.events.empty()) {
System.out.println("No Event/Appointment found for this
contact !");
}
} else {
System.out.println("Contact not found !");
}
} else {
System.out.println("There's no contacts !");
}
}
else if (choice == 2) { // By Event/Appointment Title
Event e = new Event();
System.out.print("Enter the Event/Appointment title: ");
e.title = input.nextLine();
if (!events.empty()) {
events.findFirst();
for (int i = 0; i < events.size; i++) {
if (events.retrieve().compareTo(e) == 0) {
if (events.retrieve().EventType == true ){
System.out.println("Event Found!");}
else {
System.out.println("Appointment Found!");}
Event found = events.retrieve();
System.out.println(found.toString());
}
events.findNext();
}
}
else{
System.out.print("there is no Event/Appointment!");}
}
else {
System.out.println("Invalid choice. Try again.");
}
} //end method print Event details
//---------------------------------------------------------------------------------
//display the all contacts that has the same first name
public static void PrintContactsFirstName() {
System.out.print("Enter the first name:");
String firstName = input.nextLine().trim();
if (!contactsTree.empty()) {
contactsTree.searchFirstName(firstName);
} else {
System.out.println("No Contacts found !");
}
}
//---------------------------------------------------------------------------------
//display all events in the phonebook
public static void PrintAllEvents() {
if (!events.empty()) {
events.findFirst();
for ( int i = 0 ; i<events.size ; i++){
System.out.println((i+1)+") "+events.retrieve());
events.findNext();
}
} else {
System.out.println("No events found !");
}
}
//---------------------------------------------------------------------------------
//delets all events of a contact
public static void deleteEvent(Contact c) {
if ( !c.events.empty()){
c.events.findFirst();
for ( int i = 0 ; i<c.events.size ; i++){
Event deleted = c.events.retrieve();
if(!events.empty() && events.search(deleted) ){
if (events.retrieve().compareToSame(deleted)){
Event updated = events.retrieve();
updated.removeContact(c.name);
if (!updated.contactsNames.empty()){
events.update(updated);
}
else{
events.remove(deleted);
System.out.println("Event Deleted there's no
participants!");}
}
}
c.events.findNext();
}
System.out.println("Event deleted successfully!");
System.out.println(c);
}
else {
System.out.println("the Contact has no events to delete!");
}
}
}