In: Computer Science
Person.java
public class Person {
private String firstName, lastName, address, phone;
public Person()
{
this.firstName = this.lastName = this.address = this.phone =
"";
}
public Person(String firstName, String lastName, String address,
String phone) {
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.phone = phone;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public String getName(){ return(getFirstName() + " " +
getLastName()); }
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String strToFile()
{
return(firstName + "," + lastName + "," + address + "," +
phone);
}
@Override
public String toString()
{
return("Name: " + getFirstName() + " " + getLastName() + ",
Address: " + getAddress() + ", Phone: " + getPhone());
}
}
AddressBook.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class AddressBook {
private ArrayList<Person> people;
public AddressBook()
{
this.people = new ArrayList<>();
}
public void readFile(String filename)
{
Scanner fileReader;
try
{
fileReader = new Scanner(new File(filename));
while(fileReader.hasNextLine())
{
String[] data = fileReader.nextLine().trim().split(",");
String firstName = data[0];
String lastName = data[1];
String address = data[2];
String phone = data[3];
people.add(new Person(firstName, lastName, address, phone));
}
fileReader.close();
}catch(FileNotFoundException fnfe){
System.out.println(filename + " could not be found!
Exiting..");
System.exit(0);
}
}
public void writeFile(String filename)
{
FileWriter fw;
PrintWriter pw;
try {
fw = new FileWriter(new File(filename));
pw = new PrintWriter(fw);
for(Person p : people)
pw.write(p.strToFile() + System.lineSeparator());
pw.flush();
fw.close();
pw.close();
System.out.println("Address book has been saved to " + filename +
".\n");
} catch (IOException ex) {
System.out.println("Error in writing data to " + filename + "!
Exiting..");
System.exit(0);
}
}
private int indexOf(String name)
{
int index = -1;
for(int i = 0; i < people.size(); i++)
{
if(people.get(i).getFirstName().equalsIgnoreCase(name))
{
index = i;
break;
}
}
return index;
}
public void addPerson()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first name: ");
String firstName = sc.nextLine().trim();
int index = indexOf(firstName);
if(index == -1)
{
System.out.print("Enter the last name: ");
String lastName = sc.nextLine().trim();
System.out.print("Enter the address: ");
String address = sc.nextLine().trim();
System.out.print("Enter the phone number: ");
String phone = sc.nextLine().trim();
people.add(new Person(firstName, lastName, address, phone));
System.out.println(firstName + " is added to the address book
successfully.\n");
}
else
System.out.println(firstName + " is already added to the address
book!\n");
}
public void removePerson()
{
if(people.isEmpty())
{
System.out.println("List is empty!\n");
return;
}
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first name: ");
String firstName = sc.nextLine().trim();
int index = indexOf(firstName);
if(index == -1)
System.out.println("Sorry, no such person found!\n");
else
{
people.remove(index);
System.out.println(firstName + " is removed from the address book
successfully.\n");
}
}
public void searchPerson()
{
if(people.isEmpty())
{
System.out.println("List is empty!\n");
return;
}
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first name: ");
String firstName = sc.nextLine().trim();
int index = indexOf(firstName);
if(index == -1)
System.out.println("Sorry, no such person found!\n");
else
{
System.out.println("Match found:\n" + people.get(index) +
"\n");
}
}
public void updatePerson()
{
if(people.isEmpty())
{
System.out.println("List is empty!\n");
return;
}
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first name: ");
String firstName = sc.nextLine().trim();
int index = indexOf(firstName);
if(index == -1)
System.out.println("Sorry, no such person found!\n");
else
{
System.out.println("Provide new information for " +
people.get(index).getName() + ":");
System.out.print("Enter new address: ");
String newAddress = sc.nextLine().trim();
System.out.print("Enter new phone: ");
String newPhone = sc.nextLine().trim();
people.get(index).setAddress(newAddress);
people.get(index).setPhone(newPhone);
System.out.println("Information for " + people.get(index).getName()
+ " is successfully updated.\n");
}
}
public void displayAll()
{
if(people.isEmpty())
{
System.out.println("List is empty!\n");
return;
}
for(Person p : people)
System.out.println(p);
System.out.println();
}
}
Main.java (Main class)
import java.util.Scanner;
public class Main {
private static final String FILENAME = "my_address_book.txt";
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
AddressBook book = new AddressBook();
book.readFile(FILENAME);
int choice;
do
{
displayMenu();
choice = Integer.parseInt(sc.nextLine().trim());
switch(choice)
{
case 1:
{
System.out.println("\nADD A PERSON:\n"
+ "-------------");
book.addPerson();
break;
}
case 2:
{
System.out.println("\nREMOVE A PERSON:\n"
+ "----------------");
book.removePerson();
break;
}
case 3:
{
System.out.println("\nSEARCH A PERSON:\n"
+ "----------------");
book.searchPerson();
break;
}
case 4:
{
System.out.println("\nUPDATE A PERSON:\n"
+ "----------------");
book.updatePerson();
break;
}
case 5:
{
System.out.println("\nDISPLAY ALL PEOPLE:\n"
+ "-------------------");
book.displayAll();
break;
}
case 0:
{
book.writeFile(FILENAME);
System.out.println("\nThanks..Goodbye!\n");
System.exit(0);
}
default:
System.out.println("\nInvalid selection!\n");
}
}while(choice != 0);
}
private static void displayMenu()
{
System.out.print("Choose from the following options:\n"
+ "1. Add a person\n"
+ "2. Remove a person\n"
+ "3. Search a person\n"
+ "4. Update a person\n"
+ "5. Display all people\n"
+ "0. Exit\n"
+ "Your selection >> ");
}
}
************************************************************ SCREENSHOT ****************************************************
INPUT FILE (my_address_book.txt) - This file needs to be created before running the code and this file should be created within the same working directory where the above .java files will be residing.
CONSOLE OUTPUT :