In: Computer Science
in java
Write a contacts database program that presents the user with a menu that allows the user to select between the following options:
If the user selects the first option, the user is prompted to
enter a person's name and phone number which will get saved at the
end of a file named contacts.txt.
If the user selects the second option, the program prompts the user
asking for the name of the contact. It then searches the
contacts.txt for a matching name. If found, it displays the phone
number on the screen. If not found, it will display an appropriate
error message.
If the user selects the third option, the program displays all
contacts stored in contacts.txt in a neat table.
The program is menu driven and will repeat presenting the menu and
processing choices until the user selects the fourth option to
quit.
If the user selects an invalid option, an appropriate error message
should be displayed.
If the user selects to print all contacts to the screen with no
stored contacts, an appropriate error message should be
displayed.
You may use message dialogs or make it a purely console-based application
///////////////////ContactsProgram.java//////////////////////////////////
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class ContactsProgram {
private static final String
FILE_NAME="contacts.txt";//filename is taken as a constant
//scanner and FileWriter instance variable
declared
private static Scanner sc = null;
private static FileWriter fw = null;
//main method
public static void main(String[] args){
File file = new
File(FILE_NAME);
try {
file.createNewFile();
} catch (IOException e) {
System.out.println("Error:"+e.getMessage());
}//A new file is created in the
classpath
do{
int choice =
printMenu();
//menu logic
based on user choice
if(choice ==1){
//if 1: save contact
saveContact(file);
}else if(choice
==2){ //if 2: search contact
searchContact(file);
}else if(choice
==3){ //if 3: display all contacts
printAllContacts(file);
}else if(choice
==4){//if 4: exit after closing scanner
System.out.println("Thank you!");
sc.close();
break;
}else{//other
choice: show error
System.out.println("Error: Invalid
choice!");
}
}while(true);//loop indefinitely
until user choose 4
}
/**
* get the menu choice from user
* @return
*/
public static int printMenu(){
sc = new Scanner(System.in);
System.out.println("1. Save a
contact.");
System.out.println("2. Search for a
contact.");
System.out.println("3. Print all
contacts out to the screen.");
System.out.println("4.
Quit.");
System.out.println("Enter your
choice:");
int choice =
Integer.parseInt(sc.nextLine());
return choice;
}
/**
* Take data from user and store contact in the next
available line in contacts file
* @param file
*/
public static void saveContact(File file){
sc = new Scanner(System.in);
System.out.println("Enter person's
name:");
String name = sc.nextLine();
System.out.println("Enter person's
phone number:");
String phoneNumber =
sc.nextLine();
try {
fw = new
FileWriter(file,true); //open the file in append mode to
write
fw.append(name
+","+phoneNumber+"\n");//append the line
fw.close();//close the fw
} catch (IOException e) {
System.out.println("Error in writing into file!");
}
}
/**
* search contact's phone number from file if the
entered name is found
* if name is not found then error message will be
displayed.
* @param file
*/
public static void searchContact(File file){
sc = new Scanner(System.in);
System.out.println("Enter persons
name to search:");
String name = sc.nextLine();
String phoneNumber = null;
try {
sc = new
Scanner(file);
} catch (FileNotFoundException e)
{
System.out.println("file not found!");
return;
}
while(sc.hasNextLine()){
String
currentLine = sc.nextLine();
String[] data =
currentLine.split(",");
if(data[0].equals(name)){
phoneNumber = data[1];
break;
}
}
if(phoneNumber!=null){
System.out.println("Phone number for "+name + " is :
"+phoneNumber);
}else{
System.out.println("Tha name "+name+" is NOT FOUND in the contacts
file!");
}
}
//print all contacts form file in console
public static void printAllContacts(File file){
try{
sc = new
Scanner(file);
}catch(FileNotFoundException
fe){
System.out.println("File Not Found!");
return;
}
int counter = 0;
while(sc.hasNextLine()){
counter++;
if(counter
==1){
System.out.println(String.format("%-20s\t%-20s",
"Name","PhoneNumber"));
System.out.println("----------------------------------------------------------");
}
String
currentLine = sc.nextLine();
String[] data =
currentLine.split(",");
String name =
data[0];
String
phoneNumber = data[1];
System.out.println(String.format("%-20s\t%-20s",
name,phoneNumber));
}
if(counter==0){//if there is no
entry in the file
System.out.println("Error:There is no contacts to be
displayed!");
}
}
}
==================================
OUTPUT
==================================
1. Save a contact.
2. Search for a contact.
3. Print all contacts out to the screen.
4. Quit.
Enter your choice:
1
Enter person's name:
tomas
Enter person's phone number:
5675675432
1. Save a contact.
2. Search for a contact.
3. Print all contacts out to the screen.
4. Quit.
Enter your choice:
1
Enter person's name:
samuel
Enter person's phone number:
3344556677
1. Save a contact.
2. Search for a contact.
3. Print all contacts out to the screen.
4. Quit.
Enter your choice:
1
Enter person's name:
ian
Enter person's phone number:
8765432100
1. Save a contact.
2. Search for a contact.
3. Print all contacts out to the screen.
4. Quit.
Enter your choice:
1
Enter person's name:
jacky
Enter person's phone number:
6758760987
1. Save a contact.
2. Search for a contact.
3. Print all contacts out to the screen.
4. Quit.
Enter your choice:
3
Name PhoneNumber
----------------------------------------------------------
tomas 5675675432
samuel 3344556677
ian 8765432100
jacky 6758760987
1. Save a contact.
2. Search for a contact.
3. Print all contacts out to the screen.
4. Quit.
Enter your choice:
2
Enter persons name to search:
samuel
Phone number for samuel is : 3344556677
1. Save a contact.
2. Search for a contact.
3. Print all contacts out to the screen.
4. Quit.
Enter your choice:
2
Enter persons name to search:
nil
Tha name nil is NOT FOUND in the contacts file!
1. Save a contact.
2. Search for a contact.
3. Print all contacts out to the screen.
4. Quit.
Enter your choice:
1
Enter person's name:
nil
Enter person's phone number:
0157896321
1. Save a contact.
2. Search for a contact.
3. Print all contacts out to the screen.
4. Quit.
Enter your choice:
3
Name PhoneNumber
----------------------------------------------------------
tomas 5675675432
samuel 3344556677
ian 8765432100
jacky 6758760987
nil 0157896321
1. Save a contact.
2. Search for a contact.
3. Print all contacts out to the screen.
4. Quit.
Enter your choice:
2
Enter persons name to search:
nil
Phone number for nil is : 0157896321
1. Save a contact.
2. Search for a contact.
3. Print all contacts out to the screen.
4. Quit.
Enter your choice:
4
Thank you!
======================================
The file: contacts.txt(that has been generated
======================================