All Projects

Bus Ticket Booking System in Python
Bus Ticket Booking System in Java
Bus Ticket Booking System in Cpp
Bus Ticket Booking System in C
Cafe Order System in Python
Cafe Order System in Java
Cafe Order System in Cpp
Cafe Order System in C
Social Media Account Details Manager in Python
Social Media Account Details Manager in Java
Social Media Account Details Manager in Cpp
Social Media Account Details Manager in C
Mini ATM in Python Language
Mini ATM in Java Language
Mini ATM in cpp language
Mini ATM in c language
Pocket Money Manager in Python Language
Pocket Money Manager in Java Language
Pocket Money Manager in Cpp Language
Pocket Money Manager in C Language
Username Password Generator in Python Language
Username Password Generator in Cpp Language
Username Password Generator in C Language
Telephone Directory in Python Language
Telephone Directory in Java Language
Telephone Directory in Cpp Language
Telephone Directory in C Language
Calander in Cpp Language
Calander in C Language
NoteBook in Python Language
NoteBook in Cpp Language
NoteBook in C Language
Voting System in Python Language
Voting System in Java Language
Voting System in Cpp Language
Voting System in C Language
Age Calculator in Python Language
Age Calculator in Java Language
Age Calculator in Cpp Language
Age Calculator in C Language
Currency Converter in Python Language
Currency Converter in Java Language
Currency Converter in Cpp Language
Currency Converter in C Language
Random Password Generator in python language
Random Password Generator in java language
Random Password Generator in cpp language
Random Password Generator in c language
Billing System in python language
Billing System in c++ language
Billing System in c language
Atm in c language

Telephone Directory in Java Language


Aniket The Programmer

1K+ Subscribers

Click To Subscribe My Channel

Subscribe

Source Code

        
import java.util.Scanner;

class Contact {
    String name;
    String telephone;
    String address;
    String relationship;
}

public class TelephoneDirectory {
    final static int MAX_CONTACTS = 100;
    static Contact[] directory = new Contact[MAX_CONTACTS];
    static int contactCount = 0;
    static Scanner scanner = new Scanner(System.in);

    static void addContact() {
        if (contactCount < MAX_CONTACTS) {
            Contact newContact = new Contact();
            System.out.print("Enter name: ");
            newContact.name = scanner.next();
            System.out.print("Enter telephone number: ");
            newContact.telephone = scanner.next();
            System.out.print("Enter address: ");
            newContact.address = scanner.next();
            System.out.print("Enter relationship: ");
            newContact.relationship = scanner.next();

            directory[contactCount] = newContact;
            contactCount++;
            System.out.println("Contact added successfully!");
        } else {
            System.out.println("Directory is full.");
        }
    }

    static void showContacts() {
        if (contactCount == 0) {
            System.out.println("No contacts in the directory.");
        } else {
            System.out.println("Contacts in the directory:");
            for (int i = 0; i < contactCount; i++) {
                System.out.println("Contact " + (i + 1) + ":");
                System.out.println("Name: " + directory[i].name);
                System.out.println("Telephone: " + directory[i].telephone);
                System.out.println("Address: " + directory[i].address);
                System.out.println("Relationship: " + directory[i].relationship);
                System.out.println();
            }
        }
    }

    static void updateContact() {
        if (contactCount == 0) {
            System.out.println("No contacts in the directory to update.");
        } else {
            System.out.print("Enter the name of the contact to update: ");
            String name = scanner.next();
            boolean found = false;
            for (int i = 0; i < contactCount; i++) {
                if (directory[i].name.equals(name)) {
                    System.out.print("Enter new telephone number: ");
                    directory[i].telephone = scanner.next();
                    System.out.print("Enter new address: ");
                    directory[i].address = scanner.next();
                    System.out.print("Enter new relationship: ");
                    directory[i].relationship = scanner.next();
                    System.out.println("Contact updated successfully!");
                    found = true;
                    break;
                }
            }
            if (!found) {
                System.out.println("Contact not found.");
            }
        }
    }

    static void deleteContact() {
        if (contactCount == 0) {
            System.out.println("No contacts in the directory to delete.");
        } else {
            System.out.print("Enter the name of the contact to delete: ");
            String name = scanner.next();
            boolean found = false;
            for (int i = 0; i < contactCount; i++) {
                if (directory[i].name.equals(name)) {
                    for (int j = i; j < contactCount - 1; j++) {
                        directory[j] = directory[j + 1];
                    }
                    contactCount--;
                    System.out.println("Contact deleted successfully!");
                    found = true;
                    break;
                }
            }
            if (!found) {
                System.out.println("Contact not found.");
            }
        }
    }

    public static void main(String[] args) {
        int choice;
        do {
            System.out.println("\nTelephone Directory");
            System.out.println("1. Add Contact");
            System.out.println("2. Show Contacts");
            System.out.println("3. Update Contact");
            System.out.println("4. Delete Contact");
            System.out.println("5. Exit");
            System.out.print("Enter your choice: ");
            choice = scanner.nextInt();

            switch (choice) {
                case 1:
                    addContact();
                    break;
                case 2:
                    showContacts();
                    break;
                case 3:
                    updateContact();
                    break;
                case 4:
                    deleteContact();
                    break;
                case 5:
                    System.out.println("Exiting program. Goodbye!");
                    break;
                default:
                    System.out.println("Invalid choice. Please enter a valid option.");
            }
        } while (choice != 5);
    }
}
        
    

Source Code Github Link: View

View Video On Youtube Link: View