Hey can anyone help me understand how to do an edit method for my company application. I had previously asked for help with a search method. And i think the edit method might involve the search method.
Here is my code:
EmployeeStore.
//Imports.
import java.util.HashMap;
//********************************************************************
public class EmployeeStore
{
HashMap<String, Employee> map;
//Constructor.
public EmployeeStore()
{
map = new HashMap<String,Employee>();
}
//********************************************************************
//Hashmap Methods.
//Add to the Hashmap : Employee.
public void add(Employee employee)
{
map.put(employee.getEmployeeName(), employee);
}
//********************************************************************
//Remove from the Hashmap : Employee.
public void remove(String key)
{
//Remove the Employee by name.
map.remove(key);
}
//********************************************************************
//Clear the Hashmap : Employee.
public void clear()
{
map.clear();
}
//********************************************************************
//Print the Hashmap : Employee.
public void print()
{
System.out.println("\n********Employee's in the Company.********");
for (Employee employee : map.values())
{
//System.out.println(employee); to print the toString of Employee class
//or:
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);
}
/*public void searchByName ()
{
//(for(Employee e : map.values()) {...})
//and check for each employee if his/her email matches the searched value
for(Employee e : map.values())
{
System.out.println(e);
map.equals(getClass());
}
}*/
//********************************************************************
public Employee searchByName(String name)
{
Employee employee = map.get(name);
System.out.println(employee);
return employee;
}
//********************************************************************
public Employee searchByEmail(String email)
{
for (Employee employee : map.values())
{
if (email.equals(employee.getEmployeeEmail()))
{
System.out.println(employee);
return employee;
}
}
return null;
}
//********************************************************************
//********************************************************************
//********************************************************************
}
Employee class.
//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 "\t\t\tEmployee\n" +
"********************************************************************\n"+
"Employee Name: "+ employeeName +"\n"+
"Employee Id: " + employeeId +"\n"+
"Employee Email: " + employeeEmail;
}
//********************************************************************
}
You can use the put method of java's HashMap for this as well. From the API for HashMap's put method:
If the map previously contained a mapping for this key, the old value is replaced.
So, something like:
public void edit(Employee employee)
{
map.put(employee.getEmployeeName(), employee);
}
And then in the other code:
Employee employee = getEmployeeByName("Someniceemployeename");
if (employee != null)
{
employee.setEmployeeEmail("awesomeness#stackoverflow.com");
edit(employee);
}
As for editing the ID of an employee, you need to take some additional precautions. First, you want to make sure that the map contains the ID you want to edit (as usual). Second, when "editing" the ID you need to first remove the old employee instance from the map (with the old ID) and then add the new employee with put.
Related
So I have done everything the task requires in the code below.
The problem I am have is about the
Defense copy's of a Object
Change the values in the Employee object.
For 2
I change the values but I call new which is creating a new instance of the object.
employee1 = new Employee("626347B", "Sam O'Conor", 24000);
This way the problem works as the second time I call mainDepartment.display();the values are the same.
But this does not feel like proper encapsulation as i am creating a new object.
I was thinking of
employee1.setName("Conor Bryan);
Is how you test for encapsulation, now when I call mainDepartment.display(); the name value does change so the question is wrong.
!-----Question-----!
Display the Employee details
Display the department details
Change the values in the Employee object.
Display the Department details again (The information should not change)
Again display the Employee details (The information should be changed here).
!----Test----!
package question1;
public class Test {
public static void main(String[] args) {
//Creating a instance of both Employee and Department
Employee employee1 = new Employee("2726354E", "Bob Ings", 30000 );
//Updated Code for Department to take a copy of Employee
Employee copy = new Employee("2726354E", "Bob Ings", 30000 );
Department mainDepartment = new Department("Main Floor", copy);
//Displaying both instances of Employee and Department
employee1.display();
mainDepartment.display();
System.out.println("");
//Changing values in constructor for the instance of Employee we made earlier on
employee1 = new Employee("626347B", "Sam O'Conor", 24000);
mainDepartment.display();
System.out.println("");
System.out.println("");
employee1.display();
}
}
!----Department----!
package question1;
public class Department {
private String deptName;
private Employee employee;
private int officeNumber;
//Constructor with all three parameters
public Department(String deptName, Employee employee, int officeNumber) {
this.deptName = deptName;
this.employee = employee;
this.officeNumber = officeNumber;
}
//Constructor with the officeNumber set to 0
public Department(String deptName, Employee employee) {
this.deptName = deptName;
this.employee = employee;
this.officeNumber = 0;
}
//Displaying the instance of the object information in a anesthetically pleasing manner
public void display() {
System.out.println("Department");
Employee.seperationLine();
System.out.println("Department Name: " + getDeptName());
Employee.seperationLine();
System.out.println("Employee: " + employee.toString());
Employee.seperationLine();
System.out.println("Office Number: " + getOfficeNumber());
}
//Getters and Setters
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public int getOfficeNumber() {
return officeNumber;
}
public void setOfficeNumber(int officeNumber) {
this.officeNumber = officeNumber;
}
}
!----Employee----!
package question1;
public class Employee {
private String ppsNum;
private String name;
private double salary;
public Employee() {}
//Parameterized constructor
public Employee(String ppsNum, String name, double salary) {
this.ppsNum = ppsNum;
this.name = name;
this.salary = salary;
}
//Defensive copy constructor
public Employee(Employee copy) {
this.ppsNum = copy.ppsNum;
this.name = copy.name;
this.salary = copy.salary;
}
//Displaying the instance of the object information in a anesthetically pleasing manner
public void display() {
System.out.println("Employee Information");
seperationLine();
System.out.println("Name: " + getName());
seperationLine();
System.out.println("PPS number: " + getPpsNum());
seperationLine();
System.out.println("Salary: " + getSalary() + "0");
seperationLine();
System.out.println("\n");
}
public String toString() {
return "[ppsNum=" + ppsNum + ", name=" + name + ", salary=" + salary + "]";
}
//Getters and Setters
public String getPpsNum() {
return ppsNum;
}
public void setPpsNum(String ppsNum) {
this.ppsNum = ppsNum;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public static void seperationLine() {
System.out.println("------------------------");
}
}
employee1 = new Employee("626347B", "Sam O'Conor", 24000);
is not changing the values in an Employee object, it is creating a new Employee object.
I was thinking of
employee1.setName("Conor Bryan);
Yes, this is the correct way to change a value in an object. Do that instead of creating a new object, then display the result and you should see that the value of employee1 changed.
In order to fulfill the second requirement (that Department doesn't change) you need to make a copy of the Employee when you pass it to Department.
//Constructor with all three parameters
public Department(String deptName, Employee employee, int officeNumber) {
this.deptName = deptName;
this.employee = employee; // make a copy here
this.officeNumber = officeNumber;
}
The easiest way to do this is probably to implement a copy constructor in your Employee class, then use that in Department. You'd need to make a copy anywhere you set the employee attribute in Department, so in both of your constructors and in the setEmployee method.
Suppose I have a arraylist , I am dynamically adding multiple employee objects to the arraylist. Employee object have field like Id, name, email . My requirement is when I am adding a employee object to arraylist. Suppose that object having a email which is already there of another object added to arraylist. Then it should not allow to add current object to the arraylist or show some error message.Is there any methods available in Collection module to achieve this thing in shortest possible way..
if i got your question correctly u need to avoid duplicates for employee object based on emailaddress attribute. I would recommend to use Sets instead of arraylist.
this is how you would do with sets with overriding equals and hashcode.
package com.test.testing;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
Employee employee = new Employee("anilhk#gmail.com", "1");
Employee employee2 = new Employee("abc#gmail.com", "2");
Employee employee3 = new Employee("anilhk#gmail.com", "3");
List<Employee> empList = new ArrayList<Employee>();
empList.add(employee);
empList.add(employee2);
empList.add(employee3);
System.out.println("Employee List " +empList);
Set<Employee> empSet = new HashSet<Employee>();
for (Employee emp : empList) {
if (empSet.contains(emp)) {
System.out.println("Employee with employee email " +emp.getEmailAddress() + " and employee id " +emp.getId() +" already exists");
}
else {
empSet.add(emp);
}
}
System.out.println(empSet);
}
private static class Employee {
private String emailAddress;
private String id;
#Override
public String toString() {
return "Employee [emailAddress=" + emailAddress + ", id=" + id + "]";
}
public Employee(String emailAddress, String id) {
this.emailAddress = emailAddress;
this.id = id;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((emailAddress == null) ? 0 : emailAddress.hashCode());
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof Employee)) {
return false;
}
Employee other = (Employee) obj;
if (emailAddress == null) {
if (other.emailAddress != null) {
return false;
}
} else if (!emailAddress.equals(other.emailAddress)) {
return false;
}
return true;
}
}
}
output.
Hello World!
Employee List [Employee [emailAddress=anilhk#gmail.com, id=1], Employee [emailAddress=abc#gmail.com, id=2], Employee [emailAddress=anilhk#gmail.com, id=3]]
Employee with employee email anilhk#gmail.com and employee id 3 already exists
[Employee [emailAddress=anilhk#gmail.com, id=1], Employee [emailAddress=abc#gmail.com, id=2]]
employee3 is having the same email address as employee and thus that is excluded from the list.
HTH
// Use LinkedHashMap to keep insertion order
Map<String, Employee> employees = new LinkedHashMap<>();
// v1: add new employee with unique email
employees.putIfAbsent(employee.getEmail(), employee);
// v2: add new employee and show message for duplication email
if(employees.containsKey(employee.getEmail()))
System.out.println("Email " + employee.getEmail() + " duplication");
else
employees.put(employee.getEmail(), employee);
// get all employees in order they were added
List<Employee> res = new ArrayList<>(employees.values());
You have to write a function which will go through all the nodes of array list and check it your current email address is present before or not . if it present then return false or true and display message.
More efficient way would be to keep a separate set of emails(Set< String > emailSet), and check it every time before adding an employee to the list:
if (!emailSet.contains(emp.getEmail()) {
employeeList.add(emp);
emailSet.add(emp.getEmail());
} else {
//show error message
}
I am implemeting a circular queue in java which will accept objects (Employee) as entries. Now I have a method to edit the surname of the specific object, but somehow I cannot access the surname setter method found in the Employee class from the CQueue class even though I am importing all the required packages. Here is the code:
//PACKAGES IMPORTED
package linearstructures;
import dataobjects.*;
import linearnodes.*;
public class CQueue{
Node front, rear, temp;
boolean full = false;
String key;
public AnyClass searchKey(String key)
{
temp = rear.next; // first node
do
{
if (temp.obj.getKey().equals(key))
return temp.obj;
temp = temp.next;
} while (temp != rear.next);
return null;
}
public AnyClass editObject(String key){
int choice, newSeqNo;
double newPay;
boolean exit = false;
Employee etemp = new Employee(); //INCLUDED THIS AFTER EDIT
searchKey(key);
if(searchKey(key) != null){
temp.obj.getData();
System.out.println();
System.out.println("------------------------");
System.out.print("Enter new Salary: ");
newPay = sc.nextDouble();
System.out.println("------------------------");
System.out.println();
etemp.setPay(newPay); //INCLUDED THIS AFTER EDIT
else
System.out.println("NO OBJECT WAS FOUND!");
return null;
}
}
Employee class:
package dataobjects;
public class Employee extends AnyClass
{
public String surname;
public double pay;
public Employee(){}
public Employee(int seqNo, String surname, double pay)
{
super(seqNo);
this.surname = surname;
this.pay = pay;
}
public double getSalary()
{
return pay;
}
public void setPay(double newPay)
{
pay = newPay;
}
public String getData()
{
return super.getData() + ", Surname: " + surname + ", Pay: " + pay;
}
public String getKey()
{
return surname;
}
}
AnyClass class:
package dataobjects;
public class AnyClass
{
public int seqNo;
public AnyClass(){}
public AnyClass(int seqNo)
{
this.seqNo = seqNo;
}
public int getseqNo()
{
return seqNo;
}
public void setseqNo(int seqNo) {
this.seqNo = seqNo;
}
public String getData()
{
return "Sequential Number - " + seqNo;
}
public String getKey()
{
return Integer.toString(seqNo);
}
public void edit(){}
}
Node Class
package linearnodes;
import dataobjects.*;
public class Node{
public AnyClass obj;
public Node next;
public Node(AnyClass obj){
next = null;
this.obj = obj;
}
}
Your editobject method could be something like this:
public AnyClass editObject(String key){
// ...
// Store your search result to avoid performing searchKey twice
AnyClass searchResult = searchKey(key);
if( searchResult != null ){
// Check if searchResult is an Employee object
if( searchResult instanceof Employee )
// Cast AnyClass to Employee
Employee empl = (Employee) searchResult;
// Your logic here...
// Set properties of the Employee object
empl.setPay(newPay);
// ...
return empl;
}
// Add 'else if' here, if you need to manage other Object types
// otherwise you can join previous if conditions
}
else
System.out.println("NO OBJECT WAS FOUND!");
return null;
}
Your code creates a new local Employee instance that dies when the method ends, its value will be lost because no object points to it.
for include pay "etemp.setPay(newPay);" you will change return object to Employee.
public Employee editObject(String key){
Employee etemp = new Employee(); //INCLUDED THIS AFTER EDIT
....
....
etemp.setPay(newPay); //INCLUDED THIS AFTER EDIT
return etemp;
}
because AnyClass hasn't "public double pay;"
Hey my Delete and SearchByEmail methods are not working. Delete basically asks all the right questions but does not delete the Employee the user chooses. SearchByEmail returns all of the employees rather than the one the user has asked for.
Here is my code:
MainApp
//Imports.
import java.util.Scanner;
//********************************************************************
public class MainApp
{
//The Scanner is declared here for use throughout the whole MainApp.
private static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args)
{
new MainApp().start();
}
public void start()
{
//Create a Store named Store and add Employee's to the Store.
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"));
//********************************************************************
..............................
case 3:
System.out.println("Delete");
Employee employeeDelete = MenuMethods.userInputByName();
Store.searchByName(employeeDelete.getEmployeeName());
Store.remove(employeeDelete);
break;
case 4:
System.out.println("Delete All");
Store.clear();
break;
case 5:
System.out.println("Edit");
Employee employeeEdit = MenuMethods.userInputByName();
Store.searchByName(employeeEdit.getEmployeeName());
if (employeeEdit != null)
{
employeeEdit.setEmployeeName("Joe");
employeeEdit.setEmployeeId(1);
employeeEdit.setEmployeeEmail("webmail.com");
Store.edit(employeeEdit);
}
break;
case 6:
int searchChoice;
searchChoice =MenuMethods.getMenuChoice("1.Search by Name.\n2.Search by Email.", 2, "Please enter your choice:", "Error [1,2] Only");
String temp = keyboard.nextLine();
System.out.println("Search");
switch (searchChoice) {
case 1:
System.out.println("Search by Name.");
Employee employeeSearchName = MenuMethods.userInputByName();
Store.searchByName(employeeSearchName.getEmployeeName());
break;
case 2:
System.out.println("Search by Email.");
Employee employeeSearchEmail = MenuMethods.userInputByEmail();
Store.searchByEmail(employeeSearchEmail.getEmployeeEmail());
break;
//Test Code.
/*System.out.println("Search");
Employee employee1 = MenuMethods.userInputByName();
Employee foundEmployee = Store.searchByName(employee1.getEmployeeName());
if (foundEmployee != null) {
System.out.println("Found employee!");
} else {
System.out.println("Not Found!");
}*/
}
break;
case 7:
System.out.println("Print");
Store.print();
break;
case 8:
System.out.println("Exit");
break;
}
} while (choice != 8);
}
}
MenuMethods
//Imports
import java.util.Scanner;
//********************************************************************
public class MenuMethods
{
private static Scanner keyboard = new Scanner(System.in);
..............................
public static Employee userInput()
{
String temp = keyboard.nextLine();
Employee e = null;
System.out.println("Please enter the Employee Name:");
String employeeName = keyboard.nextLine();
System.out.println("Please enter the Employee ID:");
int employeeId = keyboard.nextInt();
temp = keyboard.nextLine();
System.out.println("Please enter the Employee E-mail address:");
String employeeEmail = keyboard.nextLine();
return e = new Employee(employeeName , employeeId, employeeEmail);
}
//********************************************************************
public static Employee userInputByName()
{
//String temp is for some reason needed. If it is not included
//The code will not execute properly.
String temp = keyboard.nextLine();
Employee e = null;
System.out.println("Please enter the Employee Name:");
String employeeName = keyboard.nextLine();
return e = new Employee(employeeName);
}
//********************************************************************
public static Employee userInputByEmail()
{
//String temp is for some reason needed. If it is not included
//The code will not execute properly.
String temp = keyboard.nextLine();
Employee e = null;
System.out.println("Please enter the Employee Email:");
String employeeEmail = keyboard.nextLine();
//This can use the employeeName's constructor because java accepts the parameters instead
//of the name's.
return e = new Employee(employeeEmail);
}
//********************************************************************
}
EmployeeStore
//Imports.
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
//********************************************************************
public class EmployeeStore implements Serializable
{
HashMap<String, Employee> map;
private static Scanner keyboard = new Scanner(System.in);
//Constructor.
public EmployeeStore()
{
map = new HashMap<String,Employee>();
}
//********************************************************************
//Hashmap Methods.
//Add to the Hashmap : Employee.
public void add(Employee employee)
{
map.put(employee.getEmployeeName(), employee);
}
//********************************************************************
//Remove from the Hashmap : Employee.
public Employee remove(Employee key)
{
//Remove the Employee by name.
if(map.containsKey(key))
return map.remove(key); //if its there remove and return
else
return null; //if its not there return 0x00000000 address
}
//********************************************************************
//Clear the Hashmap : Employee.
public void clear()
{
map.clear();
}
//********************************************************************
//Print the Hashmap : Employee.
public void print()
{
System.out.println("\n********Employee's in the Company.********");
for (Employee employee : map.values())
{
//System.out.println(employee); to print the toString of Employee class
//or:
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);
}
/*public void searchByName ()
{
//(for(Employee e : map.values()) {...})
//and check for each employee if his/her email matches the searched value
for(Employee e : map.values())
{
System.out.println(e);
map.equals(getClass());
}
}*/
//********************************************************************
public Employee searchByName(String employeeName)
{
Employee employee = map.get(employeeName);
System.out.println(employee);
return employee;
}
//********************************************************************
Employee findByEmail(String email) {
for (Employee employee : map.values())
if (employee.getEmployeeEmail().equals(email))
return employee;
// Not found.
return null;
}
public boolean searchByEmail(String employeeEmail) {
boolean employee = findByEmail(employeeEmail) != null;
System.out.println(employee);
return employee;
}
/*public void searchByName ()
{
//(for(Employee e : map.values()) {...})
//and check for each employee if his/her email matches the searched value
for(Employee e : map.values())
{
System.out.println(e);
map.equals(getClass());
System.out.println(e);
map.equals(employee.getEmployeeEmail());
}
}*/
//********************************************************************
public void edit(Employee employee)
{
map.put(employee.getEmployeeName(), employee);
}
//********************************************************************
public EmployeeStore copy()
{
//instanciate the destination copy and give same name
EmployeeStore Copy = new EmployeeStore();
//by specifying the type of the entry in the for loop i.e. <>
// we don't have to typecast the getKey() and getValue() methods
//as shown in the commented out line inside the for loop
for(Map.Entry<String, Employee> entry : map.entrySet())
{
//getting each key-value and putting into the copy
//theCopy.add((MovieKey)entry.getKey(), (Movie)entry.getValue());
Copy.add(entry.getValue());
}
//return address of the new MovieStore object
return Copy;
}
//********************************************************************
//********************************************************************
//********************************************************************
}
Employee
//Imports:
//********************************************************************
//Employee Class.
public class Employee
{
//Variables.
private String employeeName;
private int employeeId;
private String employeeEmail;
//********************************************************************
//Constructors.
public Employee(String employeeName, int employeeId, String employeeEmail)
{
this.employeeName = employeeName;
this.employeeId = employeeId;
this.employeeEmail = employeeEmail;
}
//Overloading the constructor for the use with userInputByName method.
public Employee(String employeeName)
{
this.employeeName = employeeName;
}
//********************************************************************
//Getters.
public String getEmployeeEmail()
{
return employeeEmail;
}
public String getEmployeeName()
{
return employeeName;
}
public int getEmployeeId()
{
return employeeId;
}
//********************************************************************
//Setters.
public void setEmployeeEmail(String employeeEmail)
{
this.employeeEmail = employeeEmail;
}
public void setEmployeeName(String employeeName)
{
this.employeeName = employeeName;
}
public void setEmployeeId(int employeeId)
{
this.employeeId = employeeId;
}
//********************************************************************
//toString method.
public String toString()
{
return "\t\t\tEmployee\n" +
"********************************************************************\n"+
"Employee Name: "+ employeeName +"\n"+
"Employee Id: " + employeeId +"\n"+
"Employee Email: " + employeeEmail;
}
//********************************************************************
}
If you have provided your Employee class it should be more clear, anyways.
The problem has to be the following:
return e = new Employee(employeeEmail);
So it is about your Employee Constructor which is defined for employeeName but you initialize employee object with employeeEmail. Therefore, it assigns email as a name..
Just return the search phrase as a String don't create new Employee objects for searches or other operations.
Without destructing your structure, I would suggest you to add another constructor to Employee class
public Employee(String employeeName,String employeeEmail)
{
this.employeeName = employeeName;
this.employeeEmail = employeeEmail;
}
and
public static Employee userInputByEmail()
{
...
...
return e = new Employee(null,employeeEmail);
}
You appear to be using
Employee(String employeeName)
in 2 different ways:
return e = new Employee(employeeName);
and
return e = new Employee(employeeEmail);
The field should be one or the other. Perhaps just create Employee() and use setEmployeeEmail.
I have an EmployeeStore class that stores details for employee's in a company such as Name, id and e-mail. I need a method to search the hashmap by the e-mail addresses of its employees and then print out that individual employee with the matching address.
I got the code to search the map in another answer but can't manage to print the employee.
Here is my code:
public class EmployeeStore {
HashMap<String, Employee> map;
public EmployeeStore() {
map = new HashMap<String, Employee>();
}
//....
public void add(Employee employee) {
map.put(employee.getEmployeeName(), employee);
}
public Employee searchByName(String name) {
Employee employee = map.get(name);
System.out.println(employee);
return employee;
}
public Employee searchByEmail(String email) {
for (Employee employee : map.values()) {
if (email.equals(employee.getEmployeeEmail())) {
return employee;
}
System.out.println(employee);
}
Employee employee = map.get(email);
System.out.println(employee);
return employee;
}
}
To do this i changed my code to this:
public Employee searchByEmail(String email)
{
for (Employee employee : map.values())
{
if (email.equals(employee.getEmployeeEmail()))
{
System.out.println(employee);
return employee;
}
}
return null;
}
Main:
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.searchByName("James O' Carroll");
//Store.print();
}
}
Your searchByxxx methods return an Employee so you should read that value then it is easy to print it:
Employee james = Store.searchByName("James O' Carroll");
Employee andy = Store.searchByEmail("yahoo.com");
System.out.println(james);
System.out.println(andy);
and you need to clean the searchByXXX methods a little and remove the unnecessary print statements.