-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddressBook.java
99 lines (96 loc) · 2.95 KB
/
AddressBook.java
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
package com.capgemini.addressbook;
import java.util.*;
public class AddressBook {
private ArrayList<ContactDetails> arrayList;
private Map<String, ContactDetails> detailsMap;
private AddressBook() {
arrayList = new ArrayList<>();
detailsMap = new HashMap<>();
}
/**
* @param firstName
* @param lastName
* @param address
* @param state
* @param zip
* @param phoneNo
* @param emailId
*/
private void addContactDetails(String firstName, String lastName, String address, String
state, int zip, long phoneNo, String emailId) {
ContactDetails contactDetail = new ContactDetails();
contactDetail.setContactDetails(firstName, lastName, address, state, zip, phoneNo,emailId);
arrayList.add(contactDetail);
detailsMap.put(state, contactDetail);
detailsMap.put(address, contactDetail);
}
/**
* @param name
*/
private void searchbyCity(String address) {
ContactDetails contactObj = detailsMap.get(address);
System.out.println(contactObj);
}
/**
* @param state
*/
private void searchbyState(String state) {
ContactDetails contactObj = detailsMap.get(state);
System.out.println(contactObj);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
AddressBook addressBook = new AddressBook();
System.out.println("No. of contact details to enter: ");
int numOfCon = sc.nextInt();
sc.nextLine();
for(int i = 0; i < numOfCon; i++) {
System.out.println("First Name: ");
String firstName = sc.nextLine();
System.out.println("Last Name: ");
String lastName = sc.nextLine();
System.out.println("Address: ");
String address = sc.nextLine();
System.out.println("State: ");
String state = sc.nextLine();
System.out.println("ZIP: " );
int zip = sc.nextInt();
System.out.println("Phone No: ");
long phoneNo = sc.nextLong();
sc.nextLine();
System.out.println("Email ID: ");
String emailId = sc.nextLine();
addressBook.addContactDetails(firstName, lastName, address, state, zip,phoneNo, emailId);
}
System.out.println("Enter name of city you want to persons of: ");
String city = sc.nextLine();
addressBook.searchbyCity(city);
System.out.println("Enter name of state you want to persons of: ");
String state= sc.nextLine();
addressBook.searchbyState(state);
}
}
public class ContactDetails {
public String firstName;
public String lastName;
private String address;
private String state;
private int zip;
private long phoneNo;
private String emailId;
public void setContactDetails(String firstName, String lastName, String address, String state,
int zip, long phoneNo, String emailId) {
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.state = state;
this.zip = zip;
this.phoneNo = phoneNo;
this.emailId = emailId;
}
@Override
public String toString() {
return "Name: " + firstName + " " + lastName + " Address: " + address + " State: " + state + " Zip: " +
zip + " Phone No: " + phoneNo + " Email: " + emailId;
}
}