Note that there are some explanatory texts on larger screens.

plurals
  1. PODelete and SearchByEmail methods not working
    primarykey
    data
    text
    <p>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.</p> <p>Here is my code: MainApp</p> <pre><code>//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")); //******************************************************************** </code></pre> <p>..............................</p> <pre><code> 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); } } </code></pre> <p>MenuMethods</p> <pre><code>//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); } //******************************************************************** } </code></pre> <p>EmployeeStore</p> <pre><code>//Imports. import java.io.Serializable; import java.util.HashMap; import java.util.Map; import java.util.Scanner; //******************************************************************** public class EmployeeStore implements Serializable { HashMap&lt;String, Employee&gt; map; private static Scanner keyboard = new Scanner(System.in); //Constructor. public EmployeeStore() { map = new HashMap&lt;String,Employee&gt;(); } //******************************************************************** //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. &lt;&gt; // 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&lt;String, Employee&gt; 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; } //******************************************************************** //******************************************************************** //******************************************************************** } </code></pre> <p>Employee</p> <pre><code>//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; } //******************************************************************** } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload