Question: Need a flowchart for this java program - This is the question of the code Write a Java menu-driven program that creates and manipulates a
Need a flowchart for this java program -
This is the question of the code
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 be able to perform the following basic functions: Display the entire directory Search and display the contents of a particular entry Sort the entire collection by key value (the combination of last and first names) Delete an existing entry Insert an entry o the end of the directory list Save the entire directory to a file
import java.util.*;
import java.io.*;
public class Directory
{public static void main(String[] args)throws FileNotFoundException
{ ArrayList
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 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
{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
{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 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
{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
{ 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;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
