9marks
🧩 Syntax:
import java.io.*;
import java.util.*;
class PhoneDirectory {
private Hashtable<String, String> phoneBook;
private ArrayDeque<String> callingHistory;
public static ArrayDeque<String> operationHistory;
private static final String FILE_NAME = "phone_directory.txt";
private static final String CALLING_HISTORY_FILE_NAME = "callinghistory.txt";
public PhoneDirectory() {
phoneBook = new Hashtable();
callingHistory = new ArrayDeque();
operationHistory = new ArrayDeque();
}
public void addContact(String name, String phoneNumber) {
phoneBook.put(name, phoneNumber);
operationHistory.addFirst("add" + name);
}
public void removeContact(String name) {
if (phoneBook.containsKey(name)) {
phoneBook.remove(name);
operationHistory.addFirst("remove" + name);
System.out.println(name + " removed successfully");
}
}
public void searchContact(String name) {
String phoneNumber = phoneBook.get(name);
if (phoneNumber != null) {
System.out.println("Contact found:");
System.out.println("Name: " + name);
System.out.println("Phone Number: " + phoneNumber);
operationHistory.addFirst("search" + name);
} else {
System.out.println("Contact not found.");
}
}
public void displayAllContacts() {
if (phoneBook.isEmpty()) {
System.out.println("Phone Directory is empty.");
return;
}
System.out.println("Phone Directory:");
for (String name : phoneBook.keySet()) {
System.out.println("Name:" + name + ", Phone Number:" + phoneBook.get(name));
}
operationHistory.addFirst("displayed all contacts");
}
public void renameContact(String oldName, String newName) {
if (phoneBook.containsKey(oldName)) {
String phoneNumber = phoneBook.get(oldName);
phoneBook.remove(oldName);
phoneBook.put(newName, phoneNumber);
operationHistory.addFirst("rename " + oldName + "to" + newName);
System.out.println("rename of " + oldName + " is successful");
}
}
public void callContact(String name) {
String phoneNumber = phoneBook.get(name);
if (phoneNumber != null) {
callingHistory.add(name);
operationHistory.addFirst("call " + name);
System.out.println("Calling..." + name);
} else {
System.out.println("Contact not found. Cannot call.");
}
}
public void savePhoneDirectoryToFile() {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_NAME))) {
for (String name : phoneBook.keySet()) {
writer.write(name + "," + phoneBook.get(name));
writer.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void saveCallingHistoryToFile() {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(CALLING_HISTORY_FILE_NAME))) {
for (String calledContact : callingHistory) {
writer.write(calledContact);
writer.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void loadPhoneDirectoryFromFile() {
try (BufferedReader reader = new BufferedReader(new FileReader(FILE_NAME))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 2) {
phoneBook.put(parts[0], parts[1]);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
PhoneDirectory phoneDirectory = new PhoneDirectory();
phoneDirectory.loadPhoneDirectoryFromFile();
File fl = new File(FILE_NAME);
fl.createNewFile();
File f2 = new File(CALLING_HISTORY_FILE_NAME);
f2.createNewFile();
Scanner scanner = new Scanner(System.in);
int choice;
String name, phoneNumber, newName;
do {
System.out.println("\nPhone Directory Management System");
System.out.println("1. Add a Contact");
System.out.println("2. Remove a Contact");
System.out.println("3. Search for a Contact");
System.out.println("4. Display All Contacts");
System.out.println("5. Rename a Contact");
System.out.println("6. Call a Contact");
System.out.println("7. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
scanner.nextLine(); // Clear the newline character from the buffer
switch (choice) {
case 1:
System.out.print("Enter the name of the contact: ");
name = scanner.nextLine();
System.out.print("Enter the phone number: ");
phoneNumber = scanner.nextLine();
phoneDirectory.addContact(name, phoneNumber);
phoneDirectory.savePhoneDirectoryToFile();
break;
case 2:
System.out.print("Enter the name of the contact to remove: ");
name = scanner.nextLine();
phoneDirectory.removeContact(name);
phoneDirectory.savePhoneDirectoryToFile();
break;
case 3:
System.out.print("Enter the name of the contact to search: ");
name = scanner.nextLine();
phoneDirectory.searchContact(name);
break;
case 4:
phoneDirectory.displayAllContacts();
break;
case 5:
System.out.print("Enter the name of the contact to rename: ");
name = scanner.nextLine();
System.out.print("Enter the new name: ");
newName = scanner.nextLine();
phoneDirectory.renameContact(name, newName);
phoneDirectory.savePhoneDirectoryToFile();
break;
case 6:
System.out.print("Enter the name of the contact to call: ");
name = scanner.nextLine();
phoneDirectory.callContact(name);
phoneDirectory.saveCallingHistoryToFile();
break;
case 7:
System.out.println("Exiting Phone Directory Management System.");
break;
default:
System.out.println("Invalid choice. Please try again.");
break;
}
} while (choice != 7);
System.out.println("Recent Operations History: ");
for (String s : operationHistory) {
System.out.println(s);
}
scanner.close();
}
}