import
java.util.*;
public
class
PhoneBook {
public
static
HashMap<String, Integer> contact =
new
HashMap<String, Integer>();
public
static
List<String> inputLis = Arrays.asList(
"1"
,
"cool 123456789"
,
"y"
,
"2"
,
"cool"
,
"n"
);
public
static
int
indi = -
1
;
public
static
String input() {
indi +=
1
;
System.out.println(inputLis.get(indi));
return
inputLis.get(indi);
}
public
static
void
delete() {
System.out.println(
"Enter the contact"
+
" name to be deleted"
);
String name = input().strip();
if
(contact.containsKey(name)) {
contact.remove(name);
System.out.println(
"Contact Deleted !\n"
);
}
else
{
System.out.println(
"Contact not found !\n"
);
}
System.out.println(
"Do you want to perform more"
+
" operations? (y / n)"
);
String choice = input().strip();
if
(choice.equals(
"y"
)) {
main();
}
}
public
static
void
update() {
System.out.println(
"Enter the contact name"
+
" to be updated - "
);
String name = input().strip();
if
(contact.containsKey(name)) {
System.out.println(
"Enter the new"
+
" contact number - "
);
int
phone = Integer.parseInt(input());
contact.put(name, phone);
System.out.println(
"Contact updated\n"
);
}
else
{
System.out.println(
"Contact not found !\n"
);
}
System.out.println(
"Do you want to perform "
+
"more operations? (y / n)"
);
String choice = input().strip();
if
(choice.equals(
"y"
)) {
main();
}
}
public
static
void
search() {
System.out.println(
"Enter the name to be searched - "
);
String name = input().strip();
if
(contact.containsKey(name)) {
System.out.println(
"Contact Found !"
);
System.out.println(name +
" "
+ contact.get(name));
}
else
{
System.out.println(
"Contact not found !\n"
);
}
System.out.println(
"Do you want to perform more"
+
" operations? (y / n)"
);
String choice = input().strip();
if
(choice.equals(
"y"
)) {
main();
}
}
public
static
void
store() {
System.out.println(
"\n\nEnter the name"
+
" and phone number"
+
" separated by space - "
);
String[] namePhone = input().strip()
.split(
" "
);
String name = namePhone[
0
];
int
phone = Integer.parseInt(namePhone[
1
]);
if
(contact.containsKey(name)) {
System.out.println(
"Contact Already exists !\n"
);
}
else
{
contact.put(name, phone);
System.out.println(
"Contact Stored !"
);
}
System.out.println(
"Do you want to perform more"
+
" operations? (y / n)"
);
String choice = input().strip();
if
(choice.equals(
"y"
)) {
main();
}
}
public
static
void
main() {
System.out.println(
"Please choose any choice"
+
" from below -\n\n\n"
);
System.out.println(
"Store Contact number (1)"
);
System.out.println(
"Search Contact number (2)"
);
System.out.println(
"Update Contact number (3)"
);
System.out.println(
"Delete Contact number (4)"
);
int
choice = Integer.parseInt(input());
switch
(choice) {
case
1
:
store();
break
;
case
2
:
search();
break
;
case
3
:
update();
break
;
case
4
:
delete();
break
;
default
:
break
;
}
}
public
static
void
main(String[] args) {
System.out.println(
"----------------------"
+
"Welcome to GeeksforGeeks Phonebook"
+
"----------------------"
);
main();
}
}