当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Delete and SearchByEmail methods not working

问题描述:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: