Write a Java program that creates and manipulates a directory of names, telephon
ID: 3831742 • 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
// Person.java
public class Person {
private String firstName;
private String lastName;
private long telephoneNumber;
public Person(String firstName, String lastName, long telephoneNumber) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.telephoneNumber = telephoneNumber;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public long getTelephoneNumber() {
return telephoneNumber;
}
public void setTelephoneNumber(long telephoneNumber) {
this.telephoneNumber = telephoneNumber;
}
public String toString(){
return this.firstName+" "+this.lastName+" "+this.telephoneNumber;
}
}
// End of Person class
// Directory Class
import java.util.*;
public class Directory {
HashMap <String,Person> map= new HashMap <String,Person>();
public void insert(Person p1) {
map.put(p1.getFirstName()+p1.getLastName(), p1);
}
public void delete(Person p1) {
if (map.containsKey(p1.getFirstName()+p1.getLastName())){
map.remove(p1);
}
}
public void search(Person p1) {
if (map.containsKey(p1.getFirstName()+p1.getLastName())){
map.get(p1);
}
}
public void display(Person p1) {
if (map.containsKey(p1.getFirstName()+p1.getLastName())){
System.out.println(p1.getFirstName()+p1.getLastName() + " "+map.get(p1));
}
}
public void dispalyAll() {
for(String k : map.keySet()){
System.out.println(k+ " "+ map.get(k));
}
}
}
// End of Directory class
// Tester
public class Tester {
public static void main(String[] args) {
Directory d1=new Directory();
Person p1= new Person("Sri","Bebo",9848012345l);
d1.insert(p1);
d1.delete(p1);
d1.search(p1);
d1.display(p1);
d1.dispalyAll();
}
}
//End of tester class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.