· 7 years ago · Sep 17, 2018, 11:18 PM
1SearchByEmail print
2public class EmployeeStore {
3
4 HashMap<String, Employee> map;
5
6 public EmployeeStore() {
7 map = new HashMap<String, Employee>();
8 }
9
10 //....
11
12 public void add(Employee employee) {
13 map.put(employee.getEmployeeName(), employee);
14 }
15
16 public Employee searchByName(String name) {
17 Employee employee = map.get(name);
18 System.out.println(employee);
19 return employee;
20 }
21
22 public Employee searchByEmail(String email) {
23 for (Employee employee : map.values()) {
24 if (email.equals(employee.getEmployeeEmail())) {
25 return employee;
26 }
27 System.out.println(employee);
28 }
29 Employee employee = map.get(email);
30 System.out.println(employee);
31
32 return employee;
33 }
34}
35
36public Employee searchByEmail(String email)
37 {
38 for (Employee employee : map.values())
39 {
40 if (email.equals(employee.getEmployeeEmail()))
41 {
42 System.out.println(employee);
43 return employee;
44 }
45 }
46
47
48 return null;
49 }
50
51public class MainApp {
52
53 private static Scanner keyboard = new Scanner(System.in);
54
55 public static void main(String[] args) {
56 new MainApp().start();
57 }
58
59 public void start() {
60 EmployeeStore Store = new EmployeeStore();
61 Store.add(new Employee("James O' Carroll", 18, "hotmail.com"));
62
63 Store.add(new Employee("Andy Carroll", 1171, "yahoo.com"));
64
65 Store.add(new Employee("Luis Suarez", 7, "gmail.com"));
66 Store.searchByName("James O' Carroll");
67 //Store.print();
68 }
69}
70
71Employee james = Store.searchByName("James O' Carroll");
72Employee andy = Store.searchByEmail("yahoo.com");
73
74System.out.println(james);
75System.out.println(andy);