Write a Java menu-driven program that creates and manipulates a directory of nam
ID: 3576197 • 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.
Explanation / Answer
Hi,
Please see below the java classes. Please comment for any queries/feedback.
Thanks,
Anita Joseph
Entry.java
public class Entry {
private String name;
private String address;
private String phoneNumber;
public Entry(String name,String address,String phoneNumber) {
this.name=name;
this.address =address;
this.phoneNumber =phoneNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
Directory.java
public class Directory {
private Entry[] entries ;
public Directory(int n) {
this.entries = new Entry[n];
}
public Entry[] getEntries() {
return entries;
}
public void setEntries(Entry[] 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].getAddress() +", "+entries[i].getPhoneNumber());
System.out.println(" ");
}
}
public void seacrhEntry(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].getAddress() +", "+entries[i].getPhoneNumber());
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;
}
}
}
}
import java.util.Scanner;
Driver.java
public class Driver {
public static Scanner scan = new Scanner(System.in);
public static Directory directory;
public static void main(String [] args){
String n=scan.nextLine();
System.out.println("Enterthe number of Entries:");
int number= new Integer(n);
directory = new Directory(number);
int count =0 ;
int ArrayCount =1;
//reading the Name, Address and phoneNumber from the user
String flag="C";
while(count<number && "C".equalsIgnoreCase(flag)){
addEntry(count);
count++;
ArrayCount ++;
System.out.println("Exit--> Press E, Continue -> C");
flag = scan.nextLine();
}
//Sorting and displaying the Directory based on the names
directory.sortEntries();
//Seraching the Dierectory for aparticular name
System.out.println("Enter Fullname to search:");
String fullName= scan.nextLine();
directory.seacrhEntry(fullName);
//Deleting an entry
System.out.println(" Enter Fullname to delete:");
String fullNameDel= scan.nextLine();
directory.deleteentry(fullNameDel);
}
public static void addEntry(int count){
System.out.println("Please Enter Name:");
String addres1s = scan.nextLine();
System.out.println("Please enter Address: Street,City,State,Zipcode :");
String address = scan.nextLine();
System.out.println("Enter phone Number:");
String phoneNum= scan.nextLine();
//Creating new Entry Object
Entry entry = new Entry(addres1s,address,phoneNum);
//Adding the entry object to deirectory Array
directory.getEntries()[count] = entry;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.