Need help in java Write a program that maintains a set of contacts. Each contact
ID: 3553325 • Letter: N
Question
Need help in java
Write a program that maintains a set of contacts. Each contact has name, address, and a list of
phone numbers. This contacts should be be stored in a HashMap<T>. When the program starts
up and the empty map will be treated initially. Then the program should present the user with a
"menu" that will allow the user to add, search, and delete records. Also, it will allow the user to
navigate from record to record. That is, allow the user to locate the first, next, previous, and last
records.
The "current record" in the list is the record that was most recently referenced (added or
navigation). At program start-up, the current record is undefined.
Create the method that will allow the user to add new record. If the user chooses 'Add' from the
menu, the program should prompt for a name, address, and telephone number. Upon receipt
of this information, a new record is added to the list.
If the user chooses 'delete', the program should delete the current entry from the list. (If the
current record is undefined, the program should issue an error message.) After a record is
deleted, the current record is undefined. Create the procedure that will allow the user to delete
the current record.
If the user chooses 'search', the program should prompt for a name to be searched for. It then
should search through the list for a record with a matching name and print the name, address
and telephone number to the screen. Write the code for the methods to locate and display the
first, previous, next, and last record in the list. Use care so that the end user cannot locate the
next record when the last record is the current record. And print the name, address and
telephone number to the screen.
Explanation / Answer
public class test{
public static void main(String args[]){
HashMap<String,Long> hm =new HashMap<String,Long>();
String name,str;
Long phno;
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
while (true){
System.out.println("1 Enter phone entries");
System.out.println("2 Lookup in the table");
System.out.println("3 Display names in book");
System.out.println("4 exit");
System.out.println("your choice: ");
int n =Integer.parseInt(br.readLine());
Switch (n){
case 1:
System.out.println("enter name");
name=br.readLine();
System.out.println("enter phno");
str=br.readLine();
phno=new Long(str);
c.number=phno;
hm.put(name,phno);
break;
case 2:
System.out.println("enter name");
name=br.readLine();
name=name.trim();
phno=hm.get(name);
System.out.println("phno ="+phno);
break;
case 3:
Set String= new Hashset<String>();
Set =hm.keySet();
System.out.println(set);
break;
case 4:
return;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.