0

嘿,我的 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);

     }
}

菜单方法

//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);

    }
    //********************************************************************


}

员工商店

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

2 回答 2

2

如果你提供了你的 Employee 类,它应该更清楚,无论如何。

问题必须如下:

return e = new Employee(employeeEmail);

所以它是关于您的员工构造函数,它是为员工名称定义的,但您使用员工电子邮件初始化员工对象。因此,它将电子邮件分配为名称..

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

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

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

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

}
于 2012-07-21T10:51:43.537 回答
1

你似乎在使用

 Employee(String employeeName)

以两种不同的方式:

 return e = new Employee(employeeName);

 return e = new Employee(employeeEmail);

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

于 2012-07-21T10:47:53.493 回答