I need to write a code with a constructor class in order to see the whole phonelist using an array that reads from a document txt, able to add a person and phone number, search just one person in the phonelist and able to modify their phone number. here is my code
Constructor:
public class PhoneBookEntry {
private String name;
private String phone;
public void setName(String n){
name=n;
}
public void setPhone(String p){
phone=p;
}
public String getName(){
return name;
}
public String getPhone(){
return phone;
}
public String toString(){
String str = "name " + name + "\n phone" + phone;
return str;
}
}
and here is the code that runs it
import java.util.Scanner;
import java.io.*;
public class PhoneBookEntryTest {
/**
*
*/
public static void main(String[] args)throws IOException {
// declaraciones
final int SIZE = 10;
String theName;
int indexFound;
Scanner kb = new Scanner(System.in);
PhoneBookEntry[] phoneList = new PhoneBookEntry[SIZE];
int entriesAmount=0;
int select;
entriesAmount = fillArrays(phoneList);
do{
System.out.println("Choose the corresponding number of your desired choice");
System.out.println("1. See complete list");
System.out.println("2. Add one person to the list");
System.out.println("3. Look for a person in the list");
System.out.println("4. Modify the phone number of one of the person ");
System.out.println("5. Exit");
System.out.println("Enter your selection");
select = kb.nextInt();
switch (select)
{ case 1: viewPhoneBook(phoneList,entriesAmount);
break;
case 2: entriesAmount=insertEntry(phoneList,entriesAmount,kb);
break;
case 3: kb.nextLine();
System.out.println("Enter the name you wish to find");
theName = kb.nextLine();
indexFound = searchPhone(theName,phoneList,entriesAmount);
if( indexFound == -1)
System.out.println("That name is not found. The format must be: LastName,FirstName");
else System.out.println(phoneList[indexFound]);
break;
// case 4:
case 5: updateFile(phoneList,entriesAmount);
System.out.println("Until next time.");
break;
default: System.out.println("Error. Your entry selection must be 1, 2, 3, 4 or 5");
}//end switch
}while (select!= 5);
}//end main
public static int fillArrays(PhoneBookEntry[] myPhoneBook)throws IOException{
File inFile = new File("Phonebook.txt");
if(!inFile.exists()){System.out.println("file not found");
System.exit(1);
}
Scanner fileReader = new Scanner(inFile);
int c =0;
while(fileReader.hasNext()&& c < myPhoneBook.length){
String aName= fileReader.next();
String aPhone= fileReader.nextLine();
PhoneBookEntry tempEntry = new PhoneBookEntry();
tempEntry.setName(aName);
tempEntry.setPhone(aPhone);
myPhoneBook[c] = tempEntry;
c++;
}//end while
if(fileReader.hasNext()){
System.out.println("There are still data in the file that can't fit into the array.");
System.out.println("Contact your programmer");
System.exit(2);
}//end if
fileReader.close();
return c;
}
public static int insertEntry(PhoneBookEntry[] myPhoneBook, int listAmount, Scanner kb){
if(listAmount == myPhoneBook.length){
System.out.println("Arrays are full. add operation cancelled.");
return listAmount;
}
kb.nextLine();//clean buffer
System.out.println("Enter the name");
String aName = kb.nextLine();
//check if that name is already in the list
int indexFound = searchPhone(aName,myPhoneBook,listAmount);
if (indexFound != -1)
System.out.println("That name already exists, it can't be added.");
else{myPhoneBook[listAmount].setName(aName);
System.out.println("Enter the phone number(with dashes)");
String aPhone=kb.nextLine();
myPhoneBook[listAmount].setPhone(aPhone);
listAmount++;}
return listAmount;
}
public static void updateFile(PhoneBookEntry[] myPhoneBook, int listAmount)throws IOException{
PrintWriter phoneFile = new PrintWriter("Phonebook.txt");
for(int k=0; k<listAmount; k++ )
phoneFile.println(myPhoneBook[k]);
phoneFile.close();
}
public static void viewPhoneBook(PhoneBookEntry[] myPhoneBook, int listAmount){
System.out.println("Name & Phone List");
for(int k=0; k<listAmount; k++ )
System.out.println(myPhoneBook[k]);
}
public static int searchPhone(String searchName, PhoneBookEntry[] myPhoneBook, int listAmount){
int foundPosition = -1;
boolean found = false;
int c =0;
while(!found && c < listAmount)
if (searchName.equals(myPhoneBook[c].getName())){
found = true;
foundPosition = c;
}
else c++;
return foundPosition;
}//end searchPhone
}
I still haven't written the code for number 4, but where I am asking help is in number 2 when i have to add the phone number it gives me an error message any help on number 2 would be great, and if u want to add your 2 cents on number 4 you're welcome to do so, but I'll see what i can do, the other ones work fine. Thanks.