Write a Java menu-driven program that creates and manipulates a directory of nam
ID: 3576583 • Letter: W
Question
Write a Java menu-driven program that creates and manipulates a directory of names, telephone numbers, and home addresses. The following information will be stored for each person in the directory:
- Name (Last, First)
- Home address (street address, city, state, zip code)
- Home telephone number
Your program should use an Array data structure to store the directory entries and should be able to perform the following basic functions. The directory should remain an sorted after each of the operations below:
- Display the entire directory in sorted order by key value (combination of last and first names)
- Search and display the contents of a particular entry
- Delete an existing entry
- Insert an entry
The user should enter the size of the array, N. An error message should be displayed if inserting into the array makes the size larger than N.
You must use three classes: entry class, directory class, and a driver.
I used Bluej to do this. The system said Class compiled-no sysntax errors, but output window can`t appear.
Entry.java
public class Entry
{
private Directory[] entries ;
public Entry(int N) {
this.entries = new Directory[N];
}
public Directory[] getEntries() {
return entries;
}
public void setDirectory(Directory[] entries) {
this.entries = entries;
}
public void sortEntries(){
String temp;
for (int i = 0; i < this.entries.length; i++)
{
for (int j = i + 1; j < this.entries.length; j++)
{
if (entries[i].getName().compareTo(entries[j].getName())>0)
{
temp = entries[i].getName();
entries[i].setName(entries[j].getName());
entries[j].setName (temp);
}
}
}
System.out.print("Names in Sorted Order:");
for (int i = 0; i < this.entries.length; i++)
{ System.out.println(" ");
System.out.print(entries[i].getName() + ","+entries[i].getStreet()+","+entries[i].getCity() +","+entries[i].getState()+","+entries[i].getZip()+"," +entries[i].getTelephone());
System.out.println(" ");
}
}
public void searchDirectory(String name){
for (int i = 0; i < this.entries.length; i++)
{
if(entries[i].getName().equalsIgnoreCase(name)){
System.out.println("Showing the details of : "+name+" ");
System.out.print(entries[i].getName() + ","+entries[i].getStreet()+","+entries[i].getCity()+","+entries[i].getState()+","+entries[i].getZip() +", "+entries[i].getTelephone());
break;
}
}
}
public void deleteEntry(String name){
for (int i = 0; i < this.entries.length; i++)
{
if(entries[i].getName().equalsIgnoreCase(name)){
System.out.println("Deleting the details of : "+name+" ");
entries[i]=null;
}
}
}
}
Directory.java
public class Directory {
String name;
String street;
String city;
String state;
String zip;
String telephone;
public Directory(String Name, String street, String city, String state, String zip, String telephone) {
this.name = name;
this.street = street;
this.city = city;
this.state = state;
this.zip = zip;
this.telephone = telephone;
}
public String getName() {
return name;
}
public void setName(String Name) {
this.name = name;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
}
Driver.java
import java.util.Scanner;
public class Driver {
public static Entry entry;
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
String n = scan.nextLine();
System.out.println("How many people you want to enter?");
int number = new Integer(n);
entry = new Entry(number);
while(true){
System.out.println("1.add a person 2. search a person 3.Delete your enter 4.Exit");
int choice = scan.nextInt();
if(choice == 1){
scan.nextLine();
System.out.println("Enter first Name: ");
String firstName = scan.nextLine();
System.out.println("Enter last Name: ");
String lastName = scan.nextLine();
System.out.println("Enter Street: ");
String street = scan.nextLine();
System.out.println("Enter City: ");
String city = scan.nextLine();
System.out.println("Enter State: ");
String state = scan.nextLine();
System.out.println("Enter Zip: ");
String zip = scan.nextLine();
System.out.println("Enter phone number: ");
String phone = scan.nextLine();
entry.sortEntries();
}
else if(choice == 2){
System.out.println("please enter Name to search: ");
String Name = scan.nextLine();
entry.searchDirectory(Name);
}
else if(choice == 3){
System.out.println("please enter Name to delete: ");
String Name = scan.nextLine();
entry.deleteEntry(Name);
}
else{
System.out.println("Thank you");
break;
}
}
}
}
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.