java - Search Method for an EmployeeStore. Using HashMap -
i making application store employee details such name, id , email address. doing using hashmap. having difficulty searchbyname,id , email address methods. how go writing 1 ?
here code:
//imports. import java.util.scanner; //******************************************************************** public class mainapp { private static scanner keyboard = new scanner(system.in); public static void main(string[] args) { new mainapp().start(); } public void start() { employeestore store = new employeestore(); store.add(new employee ("james o' carroll", 18,"hotmail.com")); store.add(new employee ("andy carroll", 1171,"yahoo.com")); store.add(new employee ("luis suarez", 7,"gmail.com")); store.print(); } } //imports. import java.util.hashmap; //******************************************************************** public class employeestore { hashmap<string, employee> map; //constructor. public employeestore() { map = new hashmap<string,employee>(); } //******************************************************************** //hashmap methods. //add hashmap : employee. public void add(employee employee) { map.put(employee.getemployeename(), employee); } //******************************************************************** //remove hashmap : employee. public void remove(string key) { //remove employee name. map.remove(key); } //******************************************************************** //clear hashmap : employee. public void clear() { map.clear(); } //******************************************************************** //print hashmap : employee. public void print() { system.out.println("\n********employee's in company.********"); (employee employee : map.values()) { system.out.println("employee name:\t" + employee.getemployeename()); system.out.println("employee id:\t" + employee.getemployeeid()); system.out.println("e-mail:\t"+ employee.getemployeeemail()); } } public employee get(string name){ return map.get(name); } //******************************************************************** //******************************************************************** } //imports: //******************************************************************** //employee class. public class employee { //variables. private string employeename; private int employeeid; private string employeeemail; //******************************************************************** //constructor. public employee(string employeename, int employeeid, string employeeemail) { this.employeename = employeename; this.employeeid = employeeid; this.employeeemail = employeeemail; } //******************************************************************** //getters. public string getemployeeemail() { return employeeemail; } public void setemployeeemail(string employeeemail) { this.employeeemail = employeeemail; } public string getemployeename() { return employeename; } public int getemployeeid() { return employeeid; } //******************************************************************** //tostring method. public string tostring() { return "employee [employeename=" + employeename + ", employeeid=" + employeeid + ", employeeemail=" + employeeemail + "]"; } //******************************************************************** }
in employeestore, can add various methods mention:
- searchbyname easy implement because that's key map, simple
map.get(name)
should trick - for other 2, have 2 options:
- create 1 map each type of search (so maintain 3 maps in parallel, 1 key name, 1 key email , 1 key id). each map contain same employees. , can implement search in 1. faster uses more memory
- loop on values of map (
for(employee e : map.values()) {...}
) , check each employee if his/her email matches searched value
Comments
Post a Comment