删除和SearchByEmail方法不起作用

时间:2012-07-21 10:23:31

标签: java hashmap

嘿我的Delete和SearchByEmail方法无效。删除基本上会询问所有正确的问题,但不会删除用户选择的员工。 SearchByEmail返回所有员工,而不是用户要求的员工。

这是我的代码: 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;
    }
//********************************************************************


//********************************************************************  
//********************************************************************


}

员工

//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;
    }
//********************************************************************
}

2 个答案:

答案 0 :(得分:2)

如果你提供了你的员工类,那么反正应该更清楚。

问题必须如下:

return e = new Employee(employeeEmail);

因此,它是关于为employeeName定义的Employee Constructor,但是您使用employeeEmail初始化employee对象。因此,它将电子邮件指定为名称..

只需将搜索短语作为字符串返回,不要为搜索或其他操作创建新的Employee对象。

在不破坏你的结构的情况下,我建议你为Employee类添加另一个构造函数

public Employee(String employeeName,String employeeEmail) 
{
    this.employeeName = employeeName;
    this.employeeEmail = employeeEmail;
}

public static Employee userInputByEmail()
{
     ...
     ...
     return e = new Employee(null,employeeEmail);

}

答案 1 :(得分:1)

您似乎正在使用

 Employee(String employeeName)

以两种不同的方式:

 return e = new Employee(employeeName);

 return e = new Employee(employeeEmail);

该字段应该是一个或另一个。也许只需创建Employee()并使用setEmployeeEmail。