0% found this document useful (0 votes)
14 views2 pages

Notes 20240502194241

This document defines a Contact class with name and phone number fields and an AddressBook class that stores contacts in a list and can load, save, add, and display contacts from a file.

Uploaded by

vaishakrr97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

Notes 20240502194241

This document defines a Contact class with name and phone number fields and an AddressBook class that stores contacts in a list and can load, save, add, and display contacts from a file.

Uploaded by

vaishakrr97
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

05.

02 7:42 PM
Hey
import java.io.*;
import java.util.*;

class Contact implements Serializable {


private String name;
private String phoneNumber;

public Contact(String name, String phoneNumber) {


this.name = name;
this.phoneNumber = phoneNumber;
}

public String getName() {


return name;
}

public String getPhoneNumber() {


return phoneNumber;
}

@Override
public String toString() {
return "Name: " + name + ", Phone Number: " + phoneNumber;
}
}

public class AddressBook {


private static final String FILE_NAME = "address_book.dat";
private List<Contact> contacts;

public AddressBook() {
contacts = new ArrayList<>();
loadContacts();
}

private void loadContacts() {


try (ObjectInputStream ois = new ObjectInputStream(new
FileInputStream(FILE_NAME))) {
contacts = (List<Contact>) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
System.out.println("Error loading contacts: " + e.getMessage());
}
}

private void saveContacts() {


try (ObjectOutputStream oos = new ObjectOutputStream(new
FileOutputStream(FILE_NAME))) {
oos.writeObject(contacts);
} catch (IOException e) {
System.out.println("Error saving contacts: " + e.getMessage());
}
}

public void addContact(Contact contact) {


contacts.add(contact);
saveContacts();
}
public void displayContacts() {
for (Contact contact : contacts) {
System.out.println(contact);
}
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
AddressBook addressBook = new AddressBook();

while (true) {
System.out.println("1. Add Contact");
System.out.println("2. Display Contacts");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");

int choice = scanner.nextInt();


scanner.nextLine(); // Consume newline

switch (choice) {
case 1:
System.out.print("Enter name: ");
String name = scanner.nextLine();
System.out.print("Enter phone number: ");
String phoneNumber = scanner.nextLine();
Contact newContact = new Contact(name, phoneNumber);
addressBook.addContact(newContact);
System.out.println("Contact added successfully!");
break;
case 2:
System.out.println("Contacts:");
addressBook.displayContacts();
break;
case 3:
System.out.println("Exiting...");
System.exit(0);
default:
System.out.println("Invalid choice. Please enter a number
between 1 and 3.");
}
}
}
}

You might also like