Write a Java program that creates and manipulates a directory of names, telephon
ID: 3831465 • Letter: W
Question
Write a Java program that creates and manipulates a directory of names, telephone numbers.
The following information will be stored for each person in the directory:
- Name (Last, First)
- Home telephone number
You should keep the entire collection ordered by key value (the combination of last and first names). Your program should be able to perform the following basic functions:
- Search and display the contents of a particular entry
- Display the entire directory
- Delete an existing entry
- Insert an entry
- Save the entire directory to a file
Use a Binary Search Tree representation and make sure that the tree remains a Binary Search Tree after each operation
Explanation / Answer
import java.util.*;
import java.io.*;
public class Directory
{public static void main(String[] args)throws FileNotFoundException
{ ArrayList<Person> personList = new ArrayList<Person>();
int choice=getOption();
while(choice!=7)
{switch (choice)
{ case 1: addPerson(personList); break;
case 2: searchPerson(personList); break;
case 3: deletePerson(personList);break;
case 4: sortDirectory(personList);break;
case 5: displayDirectory(personList);break;
case 6: saveDirectory(personList);break;
default: System.out.println("Wrong choice");
}
choice=getOption();
}
}
public static void sortDirectory(ArrayList<Person> personList)
{Person a = new Person();
Person b= new Person();
Person temp = new Person();
for(int i = 0; i < personList.size()-1; i++)
for(int j = i+1; j < personList.size(); j++)
{a=personList.get(i);
b=personList.get(j);
if(a.getName().compareTo(b.getName())>0)
{personList.set(j,a);
personList.set(i,b);
}
}
}
public static void saveDirectory(ArrayList<Person> personList)throws FileNotFoundException
{Scanner scan = new Scanner(System.in);
Person temp = new Person();
System.out.println("Enter name of file to save in: ");
String filename=scan.nextLine();
PrintStream output=new PrintStream(new File(filename));
for(int i = 0; i < personList.size(); i++)
{ temp=personList.get(i);
output.println(temp);
}
output.close();
}
public static int getOption()
{ Scanner scan = new Scanner(System.in);
System.out.println("Press 1 to Add Person");
System.out.println("Press 2 to Search Person");
System.out.println("Press 3 to Delete a Person");
System.out.println("Press 4 to Sort the Directory");
System.out.println("Press 5 to Display the Directory");
System.out.println("Press 6 to Save the Directory");
System.out.println("Press 7 to exit");
System.out.println("Enter your choice");
int choice = Integer.parseInt(scan.nextLine());
return choice;
}
public static void displayDirectory(ArrayList<Person> personList)
{System.out.println("Complete Directory");
Person temp = new Person();
for(int i = 0; i < personList.size(); i++)
{ temp=personList.get(i);
System.out.println(temp);
}
}
public static void addPerson(ArrayList<Person> personList)
{ Person A1 = new Person();
Scanner sc = new Scanner(System.in);
System.out.println("Enter name: ");
A1.name = sc.nextLine();
System.out.println("Enter address: ");
A1.address = sc.nextLine();
System.out.println("Enter telephone number: ");
A1.telephone = Integer.parseInt(sc.nextLine());
personList.add(A1);
}
public static void deletePerson(ArrayList<Person> personList)
{Scanner scan = new Scanner(System.in);
System.out.print("Who would you like to delete? ");
int n=searchPerson(personList);
if(n>=0)
personList.remove(n);
}
public static int searchPerson(ArrayList<Person> personList)
{ Scanner scan = new Scanner(System.in);
Person temp = new Person();
System.out.println("Enter person's name: ");
String personName = scan.nextLine();
for(int i = 0; i < personList.size(); i++)
{ temp=personList.get(i);
if(personName.equals(temp.getName()))
{System.out.println("Person Found"+temp);
return i;
}
}
System.out.println("Person not found");
return -1;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.