Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Facing issues in the Employee Management System project. #1

Open
nidhi4545 opened this issue Apr 14, 2023 · 0 comments
Open

Facing issues in the Employee Management System project. #1

nidhi4545 opened this issue Apr 14, 2023 · 0 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@nidhi4545
Copy link
Owner

List of issues:

  1. In the createEmployee(), the variable String fname's value is not being carried when i call it to print in the later statements and possibly it is not being passed to the employee service's createEmployee() method - why?
    • the fname variable works fine when when asked to confirm details and is also visible when called in the seeAllEmployees method.
  2. In the updateDetailsOfCurrentEmployee() and the deleteEmployee() methods, it is not asking for the Employee-id. Already takes in the null value - why? I have not put in a not null condition. Is it because I have not made it final?
  • When I try to make the employee-id variable final, shows me the variable might not have been initialized error. However, I am initializing it in the constructor. - what is the solution?
    Including all the classes below:

Employee.java
`package model;

import java.util.Objects;
import java.util.regex.Pattern;

//Creating the base model class defining the fields and methods for the employee object.
//since i have set the fields final we cannot use the setter methods but i am running short of ideas since there are certain fields that need to be updated.
//Any other option other than setters?
public class Employee {
private final String fName;
private final String lName;
private final int age;
private final String phoneNumber;
private String workEmail;
private String personalEmail;
private String employee_ID;

private static final String emailRegex = "^(.+)@(.+).[a-z]$";
private static final Pattern pattern = Pattern.compile(emailRegex);

public Employee(String fName, String lName, int age, String phoneNumber, String personalEmail, String emp_id, String wEmail){
    this.fName = fName;
    this.lName = lName;
    this.age = age;
    this.phoneNumber = phoneNumber;
    if(personalEmail != null){
        if(!pattern.matcher(personalEmail).matches()){
            throw new IllegalArgumentException("Incorrect email address");
        }else{
            this.personalEmail = personalEmail;
        }
        this.employee_ID = emp_id;
        this.workEmail = wEmail;
    }
}

public String getfName() {
    return fName;
}

public String getlName() {
    return lName;
}

public int getAge() {
    return age;
}

public String getPhoneNumber() {
    return phoneNumber;
}

public String getWorkEmail() {
    return workEmail;
}

public String getPersonalEmail() {
    return personalEmail;
}

public String getEmployee_ID() {
    return employee_ID;
}



@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Employee employee = (Employee) o;
    return age == employee.age && phoneNumber.equals(employee.phoneNumber) && employee_ID.equals(employee.employee_ID) &&
            Objects.equals(fName, employee.fName) && Objects.equals(lName, employee.lName) &&
            Objects.equals(workEmail, employee.workEmail) && Objects.equals(personalEmail, employee.personalEmail);
}

@Override
public int hashCode() {
    return Objects.hash(fName, lName, age, phoneNumber, workEmail, personalEmail, employee_ID);
}

@Override
public String toString() {
    return "Employee Details: " +
            "fName='" + fName + '\'' +
            ", lName='" + lName + '\'' +
            ", age=" + age +
            ", phoneNumber=" + phoneNumber +
            ", workEmail='" + workEmail + '\'' +
            ", personalEmail='" + personalEmail + '\'' +
            ", employee_ID=" + employee_ID ;
}

}
`

EmployeeService.java
`package service;

import model.Employee;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class EmployeeService {

//first creating a singleton instance so that when the application is run multiple times, there aren't several instances created but instead only one entry
// point for the program in the system
private static final EmployeeService employeeServiceInstance = new EmployeeService();

private EmployeeService(){}

public static EmployeeService getEmployeeServiceInstance() {
    return employeeServiceInstance;
}

Map<String, Employee> employees = new HashMap<>();

//Since we are creating a newly creating a new employee he cannot have an employee_id or a work email address. We created the below method to assign to the
// newly created employee a unique id and work-email address
private String[] createEmployeeIDAndEmail(String fname, String lname){
Random random = new Random();
int count = random.nextInt(111,99999);
String[] empData = new String[2];
if(fname != null && lname != null){
empData[0] = Integer.toString(random.nextInt(0,999999999));
empData[1] = fname+"."+lname+count+"@learnermedia.com";
}
return empData;
}

public void createAnEmployee(String fname, String lname, int age, String phoneNumber, String personalEmail){
    String[] localEmpInstance = createEmployeeIDAndEmail(fname,lname);
    employees.put(localEmpInstance[0], new Employee(fname, lname, age,phoneNumber,personalEmail,localEmpInstance[0],localEmpInstance[1]));
}

public Employee getEmployeeDetails(String employee_ID){
    return employees.get(employee_ID);
}

//facing issues in this method as I cannot work out the logic. What I want the method to do is that when update details method from the Employee Menu class is
//called, the new details for an existing employee are updated. One way i thought of is the setter methods but for that i will have to make the fields of the
//employee class non-final. Is that a good practice?
//What I was trying to do int the belo method is, I was matching the present name with the updated name and if they were different,
//then for the same employee id, I would have passed the new value for the particular field while keeping the rest of the fields unchanged.
//I thought of a way I can work this out, but it will be a long way
public void updateDetails(String newFirstName, String newLastName, int newAge, String newPhoneNumber, String newPersonalEmail, String emp_id, String newWorkEmail){
    Employee updatedEmployee = employees.get(emp_id);
    if(!updatedEmployee.getfName().equalsIgnoreCase(newFirstName) && updatedEmployee.getlName().equalsIgnoreCase(newLastName) &&
    updatedEmployee.getAge()== newAge && updatedEmployee.getPhoneNumber().equals(newPhoneNumber) && updatedEmployee.getWorkEmail().equalsIgnoreCase(newWorkEmail)
    && updatedEmployee.getPersonalEmail().equalsIgnoreCase(newPersonalEmail) && updatedEmployee.getEmployee_ID().equalsIgnoreCase(emp_id)) {
        //is there a way that only the new value for a particular field is updated while the rest remain the same.
        // One is the setters but then i will have to make the fields non-final. is that a good practice?
        //new First Name
        employees.put(emp_id,new Employee(newFirstName,updatedEmployee.getlName(),updatedEmployee.getAge(),updatedEmployee.getPhoneNumber(),
                updatedEmployee.getPersonalEmail(),updatedEmployee.getWorkEmail(),updatedEmployee.getEmployee_ID()));
    //repeat checking for all the fields. :D
        //new Last Name
    }else if(updatedEmployee.getfName().equalsIgnoreCase(newFirstName) && !updatedEmployee.getlName().equalsIgnoreCase(newLastName) &&
    updatedEmployee.getAge()== newAge && updatedEmployee.getPhoneNumber().equals(newPhoneNumber) && updatedEmployee.getWorkEmail().equalsIgnoreCase(newWorkEmail)
    && updatedEmployee.getPersonalEmail().equalsIgnoreCase(newPersonalEmail) && updatedEmployee.getEmployee_ID().equalsIgnoreCase(emp_id)) {
        employees.put(emp_id, new Employee(updatedEmployee.getfName(), newLastName, updatedEmployee.getAge(), updatedEmployee.getPhoneNumber(),
                updatedEmployee.getPersonalEmail(), updatedEmployee.getWorkEmail(), updatedEmployee.getEmployee_ID()));
    }
        //New Age. Knowing it can be derived from the DOB as well but since I do not have DOB field we will go this way.
    else if(updatedEmployee.getfName().equalsIgnoreCase(newFirstName) && updatedEmployee.getlName().equalsIgnoreCase(newLastName) &&
    updatedEmployee.getAge() != newAge && updatedEmployee.getPhoneNumber().equals(newPhoneNumber) && updatedEmployee.getWorkEmail().equalsIgnoreCase(newWorkEmail)
    && updatedEmployee.getPersonalEmail().equalsIgnoreCase(newPersonalEmail) && updatedEmployee.getEmployee_ID().equalsIgnoreCase(emp_id)){
        employees.put(emp_id,new Employee(updatedEmployee.getfName(), updatedEmployee.getlName(),newAge,updatedEmployee.getPhoneNumber(),
                updatedEmployee.getPersonalEmail(),updatedEmployee.getWorkEmail(),updatedEmployee.getEmployee_ID()));
    }
    //New Phone Number
    else if(updatedEmployee.getfName().equalsIgnoreCase(newFirstName) && updatedEmployee.getlName().equalsIgnoreCase(newLastName) &&
    updatedEmployee.getAge()== newAge && !updatedEmployee.getPhoneNumber().equals(newPhoneNumber) && updatedEmployee.getWorkEmail().equalsIgnoreCase(newWorkEmail)
    && updatedEmployee.getPersonalEmail().equalsIgnoreCase(newPersonalEmail) && updatedEmployee.getEmployee_ID().equalsIgnoreCase(emp_id)){
        employees.put(emp_id,new Employee(updatedEmployee.getfName(), updatedEmployee.getlName(), updatedEmployee.getAge(),newPhoneNumber,
                updatedEmployee.getPersonalEmail(),updatedEmployee.getWorkEmail(),updatedEmployee.getEmployee_ID()));
    }
    //New Personal Email address
    else if(updatedEmployee.getfName().equalsIgnoreCase(newFirstName) && !updatedEmployee.getlName().equalsIgnoreCase(newLastName) &&
    updatedEmployee.getAge() == newAge && updatedEmployee.getPhoneNumber().equals(newPhoneNumber) && !updatedEmployee.getPersonalEmail().equalsIgnoreCase(newPersonalEmail)
    && updatedEmployee.getWorkEmail().equalsIgnoreCase(newWorkEmail) && updatedEmployee.getEmployee_ID().equalsIgnoreCase(emp_id)){
        employees.put(emp_id,new Employee(updatedEmployee.getfName(), updatedEmployee.getlName(), updatedEmployee.getAge(),updatedEmployee.getPhoneNumber(),
                newPersonalEmail,updatedEmployee.getWorkEmail(),updatedEmployee.getEmployee_ID()));
    }
    //New Work Email address
    else if(updatedEmployee.getfName().equalsIgnoreCase(newFirstName) && !updatedEmployee.getlName().equalsIgnoreCase(newLastName) &&
    updatedEmployee.getAge()== newAge && updatedEmployee.getPhoneNumber().equals(newPhoneNumber) && updatedEmployee.getPersonalEmail().equalsIgnoreCase(newPersonalEmail)
    && !updatedEmployee.getWorkEmail().equalsIgnoreCase(newWorkEmail) && updatedEmployee.getEmployee_ID().equals(emp_id)) {
        employees.put(emp_id, new Employee(updatedEmployee.getfName(), updatedEmployee.getlName(), updatedEmployee.getAge(), updatedEmployee.getPhoneNumber(),
                newPersonalEmail, newWorkEmail, updatedEmployee.getEmployee_ID()));
    }

//Even though the above work-around works, I feel that this is not the correct way. This was possible only because I have 6 updatable fields.
// This is an unfeasible solution where there are large number of fields. Keeping that in mind, what would be the least costly approach.
}

public void deleteAnEmployee(String emp_id){
    //just learned that this will possibly only delete the key. What about the values? Is there a way where, by passing the key, the key
    //and its corresponding values are also deleted?
    employees.remove(emp_id);
    System.out.println("Employee details have been deleted.");
}
public Collection<Employee> getAllEmployees(){
    return employees.values();
}

}
`

EmployeeMenu.java
`package menu;

import service.EmployeeService;

import java.util.Locale;
import java.util.Scanner;

import static service.EmployeeService.getEmployeeServiceInstance;

public class EmployeeMenu {

String menu = """
                Welcome to Employee Management System
                -------------------------------------
                1. Create an Employee
                2. See all Employees
                3. Update details of existing employee
                4. Delete an employee details
                5. Exit
                ---------------------------------------
                Please select the desired number from the above menu\040
                """;
Scanner scan = new Scanner(System.in);

public void showMenu(){
    int choice;
    System.out.println(menu);
    while(scan.hasNext()){
        choice = scan.nextInt();
            switch (choice){
                case 1 -> createEmployees(scan);
                case 2 -> seeAllEmployees();
                case 3 -> updateDetailsOfCurrentEmployee(scan);
                case 4 -> deleteEmployee(scan);
                case 5 -> leaveApplication();
                default -> System.out.println("Number chosen out of range. Please select a number between 1 and 5.");
            }
        }
        showMenu();
}

public void createEmployees(Scanner scan){
    try {
        System.out.println("Please enter your first name: ");
        String fname = scan.nextLine().strip();//the first name is not being printed on the screen. Why?
        scan.nextLine();
        System.out.println("Please enter your last name: ");
        String lname = scan.nextLine().strip();
        System.out.println("Please enter your age: ");
        int age = scan.nextInt();
        System.out.println("Please enter your 10-digit phone number: ");
        String phoneNumber = scan.next();
        phoneNumber = checkPhoneNumberLength(phoneNumber);
        scan.nextLine();
        System.out.println("Please enter your personal email address: ");
        String personalEmail = scan.nextLine();
        System.out.println("Please confirm the fields entered: \n" +
                "Name: " + fname+" "+lname +
                "\nAge: " + age +
                "\nPhone Number: " + phoneNumber +
                "\n Alternate Email: " + personalEmail);
        System.out.println("All the details are confirmed?(y/n): ");
        char userChoice = scan.nextLine().charAt(0);
        EmployeeService employeeService = getEmployeeServiceInstance();
        if (userChoice == 'y') {
            employeeService.createAnEmployee(fname, lname, age, phoneNumber, personalEmail);
        } else if (userChoice == 'n') {
            System.out.println("Which field would you like to change?");
            String userAnswer = scan.nextLine();
            switch (userAnswer.toLowerCase()) {
                case "first name" -> {
                    System.out.println("Please enter the confirm first name: ");
                    fname = scan.nextLine();
                }
                case "last name" -> {
                    System.out.println("Please enter the confirm last name: ");
                    lname = scan.nextLine();
                }
                case "age" -> {
                    System.out.println("Please enter the confirm age: ");
                    age = scan.nextInt();
                }
                case "phone number" -> {
                    System.out.println("Please enter the confirm phone number: ");
                    phoneNumber = scan.nextLine();
                    phoneNumber = checkPhoneNumberLength(phoneNumber);
                }
                case "personal email" -> {
                    System.out.println("Please enter the confirm alternative email address: ");
                    personalEmail = scan.nextLine();
                }
                default -> System.out.println("Changes completed.");
            }
            employeeService.createAnEmployee(fname, lname, age, phoneNumber, personalEmail);
        }
    }catch (NullPointerException e){
        e.getLocalizedMessage();
    }
    System.out.println();
    showMenu();
}

private String checkPhoneNumberLength(String phNumber){
    if(!phNumber.matches("[0-9]+")){
        System.out.println("Invalid number");
    }
    if(phNumber.length() == 10){
        System.out.println("Phone number accepted!");
    }else{
        System.out.println("Please enter the correct phone number: ");
        phNumber = scan.nextLine();
    }
    return phNumber;
}

public void seeAllEmployees(){
    EmployeeService employeeService = getEmployeeServiceInstance();
    System.out.println(employeeService.getAllEmployees());
    System.out.println();
    showMenu();
}

public void updateDetailsOfCurrentEmployee(Scanner scan){
    System.out.println("Please enter the employee id: ");
    String employee_id = scan.nextLine(); //already takes a null value. Why?
    EmployeeService employeeService = getEmployeeServiceInstance();
    System.out.println(employeeService.getEmployeeDetails(employee_id));
    System.out.println("Which field would you like to update?");
    String userAnswer = scan.nextLine();
    String fname = "",lname = "",personalEmail = "",wEmail ="", phoneNumber = "";
    int age = 0;
    switch (userAnswer.toLowerCase(Locale.ROOT)) {
        case "first name" -> {
            System.out.println("Please enter the confirm first name: ");
            fname = scan.nextLine();
        }
        case "last name" -> {
            System.out.println("Please enter the confirm last name: ");
            lname = scan.nextLine();
        }
        case "age" -> {
            System.out.println("Please enter the confirm age: ");
            age = scan.nextInt();
        }
        case "phone number" -> {
            System.out.println("Please enter the confirm phone number: ");
            phoneNumber = scan.nextLine();
            String pNumber = phoneNumber;
            phoneNumber = checkPhoneNumberLength(pNumber);
        }
        case "personal email" -> {
            System.out.println("Please enter the confirm alternative email address: ");
            personalEmail = scan.nextLine();
        }
        case "work email" -> {
            System.out.println("Please enter the correct work email address: ");
            wEmail = scan.nextLine();
        }
        default -> System.out.println("Changes completed.");
    }
    employeeService.updateDetails(fname,lname,age,phoneNumber,personalEmail,employee_id,wEmail);
    System.out.println("PFB the updated details: \n" +
            "Name: "+fname.concat(lname)+
            "\nAge: "+age+
            "\nPhone Number: "+phoneNumber+
            "\n Alternate Email: "+personalEmail);
    System.out.println();
    showMenu();
}

public void deleteEmployee(Scanner scan){
    EmployeeService employeeService = getEmployeeServiceInstance();
    System.out.println("Please enter the employee id of the employee to be deleted: ");
    String emp_id = scan.nextLine();
    System.out.println(employeeService.getEmployeeDetails(emp_id));
    System.out.println("Please confirm if you want to delete the details(y/n): ");
    char userChoice = scan.nextLine().charAt(0);
    if(userChoice == 'y'){
        employeeService.deleteAnEmployee(emp_id);
    }else if(userChoice == 'n'){
        System.out.println("No data deleted.");
    }
    System.out.println();
    showMenu();
}

public void leaveApplication(){
    System.exit(0);
}

}
`

EmployeeMain.java
`package main;

import menu.EmployeeMenu;

public class EmployeeMain {
public static void main(String[] args){

    EmployeeMenu employeeMenu = new EmployeeMenu();
    employeeMenu.showMenu();
}

}
`
Issue-1
issue-2

@nidhi4545 nidhi4545 added bug Something isn't working help wanted Extra attention is needed labels Apr 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant