-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhoneBookMap.java
More file actions
115 lines (110 loc) · 3.96 KB
/
Copy pathPhoneBookMap.java
File metadata and controls
115 lines (110 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import java.util.*;
import java.io.*;
public class PhoneBookMap{
static File phonebook= new File("phonebook.txt");
static TreeMap<String,String> maindata = new TreeMap<String,String>();
static boolean isitsorted=false;
static Scanner userInput;
public static void main(String[] args){
int menu=0;
while(menu!=4){
System.out.println("PhoneBook Manager");
System.out.println("1. View PhoneBook");
System.out.println("2. Sort PhoneBook");
System.out.println("3. Search PhoneBook");
System.out.println("4. Exit");
System.out.print("Mode: ");
userInput = new Scanner(System.in);
menu = userInput.nextInt();
switch(menu){
case 1:
try {
System.out.println();
showPhoneBook();
System.out.println();
} catch (FileNotFoundException e) {}
break;
case 2:
try {
sortPhoneBook();
isitsorted=true;
System.out.println();
} catch (FileNotFoundException e) {}
break;
case 3:
try {
searchPhoneBook();
System.out.println();
} catch (FileNotFoundException e) {}
break;
default:
userInput.close();
break;
}
}
}
public static void showPhoneBook() throws FileNotFoundException{
if(!isitsorted){
Scanner phonedata= new Scanner(phonebook);
while(phonedata.hasNextLine()){
if(phonedata.hasNext("[A-Za-z]+")){
System.out.print(phonedata.next()+" ");
}
do{
if(phonedata.hasNext("[0-9]+")){
System.out.print(phonedata.next()+" ");
}
}while(phonedata.hasNext("[0-9]+"));
System.out.println();
}
phonedata.close();
}
else{
for(Map.Entry<String,String> count:maindata.entrySet()){
System.out.println(count.getKey()+" "+count.getValue());
}
}
}
public static void sortPhoneBook() throws FileNotFoundException{
Scanner phonedata= new Scanner(phonebook);
while(phonedata.hasNextLine()){
String phonename="",phonenumber="";
if(phonedata.hasNext("[A-Za-z]+")){
phonename=phonedata.next();
}
do{
if(phonedata.hasNext("[0-9]+")){
phonenumber=phonedata.next()+" "+phonenumber;
}
}while(phonedata.hasNext("[0-9]+"));
maindata.put(phonename,phonenumber);
}
System.out.println("PhoneBook has been sorted");
phonedata.close();
}
public static void searchPhoneBook() throws FileNotFoundException{
System.out.print("Contact name : ");
Scanner phonedata= new Scanner(phonebook);
userInput = new Scanner(System.in);
String query= userInput.next();
while(phonedata.hasNextLine()){
String phonename="",phonenumber="";
if(phonedata.hasNext("[A-Za-z]+")){
phonename=phonedata.next();
}
do{
if(phonedata.hasNext("[0-9]+")){
phonenumber=phonedata.next()+" "+phonenumber;
}
}while(phonedata.hasNext("[0-9]+"));
maindata.put(phonename,phonenumber);
}
if(maindata.containsKey(query)){
System.out.println("Contact number: "+maindata.get(query));
}
else{
System.out.println("Contact not found");
}
phonedata.close();
}
}