Interview Question
public class Contact implements Comparable<Contact> {
private String firstName;
private String lastName;
private String phoneNumber;
@Override
public int compareTo(Contact o) {
return getFirstName().compareTo(o.getFirstName());
}
}
public class AddressBook {
Set<Contact> contacts;
public AddressBook() {
contacts = new TreeSet<Contact>();
}
}
import java.util.*;
class Contact implements Comparable<Contact>
{
private String firstName;
private String lastName;
private long phoneNumber;
Contact(String fname,String lname,long phno)
{
firstName=fname;
lastName=lname;
phoneNumber=phno;
}
void setFirstName()
{
this.firstName=firstName;
}
void setLastName()
{
this.lastName=lastName;
}
String getFirstName()
{
return firstName;
}
String getLastName()
{
return lastName;
}
@Override
public int compareTo(Contact o)
{
return getFirstName().compareTo(o.getFirstName());
}
}
class AddressBook
{
Set<Contact> contacts;
public AddressBook()
{
contacts= new TreeSet<Contact>();
}
public static void main(String a[])
{
AddressBook ob=new AddressBook();
contacts.add(new Contact("shashank","mani",8804567889));
contacts.add(new Contact("rahul","sharma",90045067889));
contacts.add(new Contact("mohan","mani",9894568089));
contacts.add(new Contact("rajesh","sharma",12885607889));
contacts.add(new Contact("ashutosh","mani",7834567889));
contacts.add(new Contact("raja","goyal",6734567889));
contacts.add(new Contact("mayank","sharma",2234467889));
contacts.add(new Contact("bullu","verma",1234568809));
contacts.add(new Contact("nimish","narayan",8256907834));
contacts.add(new Contact("c.p","sharm",9256738890));
Iterator it=contacts.iterator();
while(it.hasNext())
System.out.println(it.next());
}
}
Still More Needed..?
in java we have properties class that is widely used to implement the telephone book
One of the most useful aspects of Properties is that the information contained in a
Properties object can be easily stored to or loaded from disk with the store( ) and
load( ) methods. At any time, you can write a Properties object to a stream or read
it back. This makes property lists especially convenient for implementing simple
databases. For example, the following program uses a property list to create a simple
computerized telephone book that stores names and phone numbers. To find a
person’s number, you enter his or her name. The program uses the store( ) and load( )
methods to store and retrieve the list. When the program executes, it first tries to load
the list from a file called phonebook.dat. If this file exists, the list is loaded. You can
then add to the list. If you do, the new list is saved when you terminate the program.
Notice how little code is required to implement a small, but functional, computerized
phone book.
/* A simple telephone number database that uses a property list. */
import java.io.*;
import java.util.*;
class Phonebook
{
public static void main(String args[])throws IOException
{
Properties ht = new Properties();
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
String name, number;
FileInputStream fin = null;
boolean changed = false;
// Try to open phonebook.dat file.
try
{
fin = new FileInputStream("phonebook.dat");
}
catch(FileNotFoundException e)
{
// ignore missing file
}
/* If phonebook file already exists,
load existing telephone numbers. */
try
{
if(fin != null)
{
ht.load(fin);
fin.close();
}
}
catch(IOException e)
{
System.out.println("Error reading file.");
}
// Let user enter new names and numbers.
do
{
System.out.println("Enter new name" + " ('quit' to stop): ");
name = br.readLine();
if(name.equals("quit")) continue;
System.out.println("Enter number: ");
number = br.readLine();
ht.put(name, number);
changed = true;
}
while(!name.equals("quit"));
// If phone book data has changed, save it.
if(changed)
{
FileOutputStream fout = new FileOutputStream("phonebook.dat");
ht.store(fout, "Telephone Book");
fout.close();
}
// Look up numbers given a name.
do
{
System.out.println("Enter name to find" + " ('quit' to quit): ");
name = br.readLine();
if(name.equals("quit")) continue;
number = (String) ht.get(name);
System.out.println(number);
} while(!name.equals("quit"));
}
}
use a trie
- Anonymous May 13, 2011