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

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 views

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
You are on page 1/ 4

import java.io.File; import java.io.FileWriter; import java.io.PrintWriter; import java.util.

Scanner; class Contacts { //Properties public String firstName; public String lastName; public String phone; // Defaul Constructor. Assigning Empty intial values public Contacts() { this.firstName = ""; this.lastName = ""; this.phone = ""; } // Assigning Values to Constructor (String, String, int) public Contacts(String firstName, String lastName, String phone){ this.firstName = firstName; this.lastName = lastName; this.phone = phone; } //accesors public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public String getPhone(){ return phone; } public String getContact(){ return this.lastName+" "+this.firstName+ " "+this.phone; } } 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("PhoneBook1.txt"); PrintWriter output;

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

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

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy