0% found this document useful (3 votes)
2K views4 pages

Phonebook Java Program

This document defines a class called Contacts to store contact information like name, phone number. It then defines a class called FinalPhoneBook that implements a phonebook application with options to add, view, find, and erase contacts by reading and writing to a file called PhoneBook1.txt. The main method runs a menu loop allowing the user to select an option and call the appropriate method to perform that action, like adding a new contact by prompting for details and writing to the file.

Uploaded by

Izaac Garcilazo
Copyright
© Attribution Non-Commercial (BY-NC)
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 (3 votes)
2K views4 pages

Phonebook Java Program

This document defines a class called Contacts to store contact information like name, phone number. It then defines a class called FinalPhoneBook that implements a phonebook application with options to add, view, find, and erase contacts by reading and writing to a file called PhoneBook1.txt. The main method runs a menu loop allowing the user to select an option and call the appropriate method to perform that action, like adding a new contact by prompting for details and writing to the file.

Uploaded by

Izaac Garcilazo
Copyright
© Attribution Non-Commercial (BY-NC)
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

import [Link]; import [Link]; import [Link]; import [Link].

Scanner; class Contacts { //Properties public String firstName; public String lastName; public String phone; // Defaul Constructor. Assigning Empty intial values public Contacts() { [Link] = ""; [Link] = ""; [Link] = ""; } // Assigning Values to Constructor (String, String, int) public Contacts(String firstName, String lastName, String phone){ [Link] = firstName; [Link] = lastName; [Link] = phone; } //accesors public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public String getPhone(){ return phone; } public String getContact(){ return [Link]+" "+[Link]+ " "+[Link]; } } public class FinalPhoneBook { public static void main (String [] args) throws Exception{ Contacts [] entries; entries = new Contacts[100]; Contacts [] entriesErase; entriesErase = new Contacts[100]; int selection = 0; int counter; int indexRemove; String fName,lName,pNumber; String lastName; String firstName; String phoneNumber; String lastErase; String firstErase; String phoneErase; File PhoneBook; PhoneBook = new File("[Link]"); PrintWriter output;

Scanner inSelection = new Scanner([Link]); Scanner input = new Scanner([Link]); do { do{ [Link]("PHONEBOOK:"); [Link]("1. Add Contact"); [Link]("2. View Contacts"); [Link]("3. Find Contact"); [Link]("4. Erase Contact"); [Link]("5. Exit"); [Link]("Enter selection (1-5): "); if (![Link]()) { [Link]("NOT AN OPTION. PLEASE ENTER AN OPTION (1-5)" ); [Link](); } else { selection = [Link](); if (selection>5 || selection<1){ [Link]("NOT AN OPTION. PLEASE ENTER AN OPTIO N (1-5)"); } } }while (selection<1 && selection >5); switch(selection) { case 1: [Link]("ADD CONTACT:"); [Link]("First Name:"); fName = [Link](); [Link]("Last Name:"); lName = [Link](); [Link]("Phone Number:"); pNumber = [Link](); int i=0; entries[i] = new Contacts(fName,lName,pNumber); [Link](entries[i].getContact()+ " was added to PhoneBook"); output = new PrintWriter(new FileWriter(PhoneBook, true)); [Link](entries[i].getContact()); [Link](); i++; break; case 2: [Link]("CONTACT LIST:"); // Read data from a file Scanner read = new Scanner(PhoneBook); counter = 0; while ([Link]()) { lastName = [Link](); firstName = [Link](); phoneNumber = [Link](); Contacts Entry = new Contacts(firstName,lastName,phoneNu mber); [Link]([Link]()); counter++; }

[Link](); [Link](counter + " " + "Contacts found."); break; case 3: int d=0; [Link]("FIND CONTACT: "); [Link]("Enter First Name or Last Name or Phone Num ber: "); String namefind = [Link](); Scanner read4= new Scanner(PhoneBook); //Read Phonebook and find matching names and show it in the console. while ([Link]()){ lastName = [Link](); firstName = [Link](); phoneNumber = [Link](); if ([Link](lastName)||[Link](firstName )||[Link](phoneNumber)){ entries[d] = new Contacts(firstName,lastName,phoneNu mber); [Link](entries[d].getContact()); d++; } } [Link](d + " MATCHS"); break; case 4: int nonErase=0; int erase=0; int e=0; int index=0; int indexFound=0; int indexToErase=0; [Link]("REMOVE CONTACT: "); [Link]("Enter First Name or Last Name or Phone Num ber: "); String nameToErase = [Link](); Scanner readToErase= new Scanner(PhoneBook); //Read Phonebook and find matching names and show it in the console. indexRemove=1; while ([Link]()){ lastName = [Link](); firstName = [Link](); phoneNumber = [Link](); if ([Link](lastName)||[Link](fir stName)||[Link](phoneNumber)){ entries[e] = new Contacts(firstName,lastName,phoneNu mber); [Link](indexRemove + "." +" "+ entries[e ].getContact()); e++; indexFound++; indexRemove++; } } if (indexFound>0){ [Link]("FOUND MATCH(S). ENTER # TO BE REMOVE: "); index = [Link](); indexToErase = index-1;

// concatenating to get a String String contactErase = entries[indexToErase] + ""; String delimeter= " "; String[] splitContactErase = [Link](delimeter); lastErase = splitContactErase[0]; firstErase = splitContactErase[1]; phoneErase = splitContactErase[2]; Scanner read1 = new Scanner(PhoneBook); //Read Phonebook and delete match while ([Link]()){ lastName = [Link](); firstName = [Link](); phoneNumber = [Link](); if ((([Link](firstName)) && ([Link] (lastName))) && ([Link](phoneNumber))){ entriesErase[erase] = new Contacts(firstName,lastNam e,phoneNumber); [Link](entriesErase[erase].getContact()) ; erase++; }else{ entries[nonErase] = new Contacts(firstName,lastName, phoneNumber); nonErase++; } } [Link](); //Prints matching names to a blank PhoneBook using variables store in entries. output = new PrintWriter(PhoneBook); for (int b=0; b<nonErase; b++){ [Link](entries[b]); } [Link](); [Link](erase + " " +" CONTACT(S) REMOVED"); } break; } } while (selection != 5); } }

You might also like