Java Programming : Creating a Phonebook using a simple CSV file

[info] You to have Java and Eclipse Configured and Installed. If not please install the JDK and download Eclipse[/info] In this tutorial, we will create a simple phonebook system using a text file. This is without accessing or using any advanced database management system like Access, MySQL or Oracle. The program will be a textual interactive application without a graphical interface for the sake of simplicity ! We will be having three main sections for writing the software:

  1. Interaction with the user.
  2. Adding Contacts
  3. Searching for a contact.

1. Interaction with the user

Before, we begin! let’s write a simple skeleton for the program to interact with the user !

1) Create a project using Eclipse ( or other IDE ), let’s call it : MyPhoneBook

2) Create a Class and call it : PhoneBook

[info] Click here for how to create  a project using Eclipse[/info]

import java.io.*;
import java.util.*;

public class PhoneBook {
	public static String help_msg=	"Press: H  Help   -  A  Add contact  -  S  Search  - Q  Exit :";
	public static void main(String[] args) {		
		System.out.println("\n\n***** Welcome to MyPhone Book *****\n\n");
		Scanner s=new Scanner(System.in);		
		for(;;){
				System.out.print("[Main Menu] "+help_msg+"\n:");
				String command=s.nextLine().trim();				

				if (command.equalsIgnoreCase("H")){					
					System.out.println(help_msg);
				}else if (command.equalsIgnoreCase("A")){
					System.out.print("Type in contact details in the format: name,lastname,phone\n:");

				}else if (command.equalsIgnoreCase("S")){
					System.out.print("Type in the name you are searching for :\n:");

				}else if (command.equalsIgnoreCase("Q")){
					System.out.println("Good Bye User....");
					System.exit(0);
				}else{					
					System.out.print("Unknown command ! Try again \n:");
				}

		}

	}

}

Explanations :

Line 5:  public static String help_msg= …. We are declaring a variable as static => The variable belongs to the class. Accessing static variables through the format :ClassName.VariableName

Line 8: Scanner s=new Scanner(System.in); Scanner is a class for reading from the keyboard when taking the special object System.in

Line 11 : s.next().trim(); s.next() is to read the next input from the keyboard, directly followed by trim() to remove (clip) special characters like space, new line from start and end of the word. This is the output of the program when run from Eclipse. You can press any of the letters and press Enter. d2

 

2. Adding Contacts

It is from line : 15 till line : 28 as shown below:

Line : 18,  line.split(“,”) : this breaks or split the string using the separator “,” into an elements of an array.

import java.io.*;
import java.util.*;

public class PhoneBook {
	public static String help_msg=	"Press: H  Help   -  A  Add contact  -  S  Search  - Q  Exit :";
	public static void main(String[] args) {		
		System.out.println("\n\n***** Welcome to MyPhone Book *****\n\n");
		Scanner s=new Scanner(System.in);		
		for(;;){
				System.out.print("[Main Menu] "+help_msg+"\n:");
				String command=s.nextLine().trim();				

				if (command.equalsIgnoreCase("H")){					
					System.out.println(help_msg);
				}else if (command.equalsIgnoreCase("A")){
					System.out.print("Type in contact details in the format: name,lastname,phone\n:");
					String line=s.nextLine().trim();
					String [] tmp=line.split(",");
					while(tmp.length!=3){
						System.out.print("Error, Try the format : name,lname,phone\n:");
						line=s.nextLine().trim();
						tmp=line.split(",");						
					}
					try{
						FileWriter fs = new FileWriter("phonebook.csv",true);
						BufferedWriter out = new BufferedWriter(fs);
						out.write(line+"\n");
						out.close();
					}catch(Exception e){e.printStackTrace();}

				}else if (command.equalsIgnoreCase("S")){
					System.out.print("Type in the name you are searching for :\n:");

				}else if (command.equalsIgnoreCase("Q")){
					System.out.println("Good Bye User....");
					System.exit(0);
				}else{					
					System.out.print("Unknown command ! Try again \n:");
				}

		}

	}

}

If you run this program, under your project workspace, you will see a new file being created called phonebook.csv, try to open it with Excel d1

3. Searching for Contacts

it is from line : 31 till line 45

Line 34 :  s.toLowerCase() convert a string to small letters.

Line 37: Scanner s2=new new Scanner(f) : This is a Scanner object for reading file.

Line 40 : s.indexOf(“search”), returns the position of the term “search” within the string s. If not found, it returns -1

import java.io.*;
import java.util.*;

public class PhoneBook {
	public static String help_msg=	"Press: H  Help   -  A  Add contact  -  S  Search  - Q  Exit :";
	public static void main(String[] args) {		
		System.out.println("\n\n***** Welcome to MyPhone Book *****\n\n");
		Scanner s=new Scanner(System.in);		
		for(;;){
				System.out.print("[Main Menu] "+help_msg+"\n:");
				String command=s.nextLine().trim();				

				if (command.equalsIgnoreCase("H")){					
					System.out.println(help_msg);
				}else if (command.equalsIgnoreCase("A")){
					System.out.print("Type in contact details in the format: name,lastname,phone\n:");
					String line=s.nextLine().trim();
					String [] tmp=line.split(",");
					while(tmp.length!=3){
						System.out.print("Error, Try the format : name,lname,phone\n:");
						line=s.nextLine().trim();
						tmp=line.split(",");						
					}
					try{
						FileWriter fs = new FileWriter("phonebook.csv",true);
						BufferedWriter out = new BufferedWriter(fs);
						out.write(line+"\n");
						out.close();
					}catch(Exception e){e.printStackTrace();}

				}else if (command.equalsIgnoreCase("S")){
					System.out.print("Type in the name you are searching for :\n:");
					String search=s.nextLine().trim();
					search=search.toLowerCase();
					try{
							File f=new File("phonebook.csv");
							Scanner s2=new Scanner(f);
							while(s2.hasNextLine()){
								String line=s2.nextLine();
								if (line.toLowerCase().indexOf(search)>=0){
									System.out.print("\nResult: "+line);
								}
							}
							System.out.print("\n\n");
						}catch(Exception e){e.printStackTrace();}	

				}else if (command.equalsIgnoreCase("Q")){
					System.out.println("Good Bye User....");
					System.exit(0);
				}else{					
					System.out.print("Unknown command ! Try again \n:");
				}

		}

	}

}

BIG Questions

  1. Can we add extra functionalities : Delete ? Modify Contacts ?
  2. Multi-user Access ?
  3. Different User Roles = ? Read-only ? Read and Write ? Admin access ?
  4. Network Access = Remote Access ?
  5. Scaling ? File having over 10000000 contact Records ?
  6. Backup, Maintenance…..
Comments

Leave a Reply

Your email address will not be published. Required fields are marked *