Answer to Question #289642 in Java | JSP | JSF for Jonathan

Question #289642

Create a Java console application for employees’ records management using Java collections. This application should offer a Menu that allows the user to perform specific operations (e.g., add, update, search, delete employees’ records, ...).

 Employee class

  • An employee object has the following private attributes:- ID - Employee Number- Full name - Title - Department - Phone number- Email address

 Define the getters of all these attributes.

 Define setters or input methods (e.g., inputName(), inputTitle()) for all

attributes in a way that allows you to check the validity of the user’s inputs

using RegEx.

 These methods should throw an exception of type InvalidEmployeeDetailException (custom exception)

Add an employee

A method addEmployee() should be defined in the EmployeeManager class which prompts the user to enter the employee’s details from which a new Employee instance is created and added to the collection.

Methods:

updateEmployee() 

SearchEmployee

displayEmployees()

deleteEmployee()





1
Expert's answer
2022-01-22T16:33:13-0500
import java.util.Scanner;
import java.util.regex.Pattern;

public class Employee {
    private int id;
    private int employeeNumber;
    private String fullName;
    private String title;
    private String department;
    private String phoneNumber;
    private String emailAddress;

    public Employee() {
        id = -1;
        employeeNumber = -1;
        fullName = "Unknown";
        title = "Unknown";
        department = "Unknown";
        phoneNumber = "Unknown";
        emailAddress = "Unknown";
    }

    public int getId() {
        return id;
    }

    public void inputID() {
        Scanner in = new Scanner(System.in);
        System.out.print("ID: ");
        String id = in.nextLine();
        if (Pattern.matches("^[0-9]{1,9}$", id)) {
            this.id = Integer.parseInt(id);
        } else {
            throw new InvalidEmployeeDetailException();
        }
    }

    public int getEmployeeNumber() {
        return employeeNumber;
    }

    public void inputEmployeeNumber() {
        Scanner in = new Scanner(System.in);
        System.out.print("Employee Number: ");
        String employeeNumber = in.nextLine();
        if (Pattern.matches("^[0-9]{1,9}$", employeeNumber)) {
            this.employeeNumber = Integer.parseInt(employeeNumber);
        } else {
            throw new InvalidEmployeeDetailException();
        }
    }

    public String getFullName() {
        return fullName;
    }

    public void inputFullName() {
        Scanner in = new Scanner(System.in);
        System.out.print("Full Name: ");
        String fullName = in.nextLine();
        if (Pattern.matches("^[a-zA-z]{3,} ?[a-zA-z]*$", fullName)) {
            this.fullName = fullName;
        } else {
            throw new InvalidEmployeeDetailException();
        }
    }

    public String getTitle() {
        return title;
    }

    public void inputTitle() {
        Scanner in = new Scanner(System.in);
        System.out.print("Title: ");
        String title = in.nextLine();
        if (Pattern.matches("^[a-zA-z]{3,}$", title)) {
            this.title = title;
        } else {
            throw new InvalidEmployeeDetailException();
        }
    }

    public String getDepartment() {
        return department;
    }

    public void inputDepartment() {
        Scanner in = new Scanner(System.in);
        System.out.print("Department: ");
        String department = in.nextLine();
        if (Pattern.matches("^[a-zA-z]{3,}$", department)) {
            this.department = department;
        } else {
            throw new InvalidEmployeeDetailException();
        }
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void inputPhoneNumber() {
        Scanner in = new Scanner(System.in);
        System.out.print("Phone Number: ");
        String phoneNumber = in.nextLine();
        if (Pattern.matches("^\\+[0-9]{1,3}[0-9]{10}$", phoneNumber)) {
            this.phoneNumber = phoneNumber;
        } else {
            throw new InvalidEmployeeDetailException();
        }
    }

    public String getEmailAddress() {
        return emailAddress;
    }

    public void inputEmailAddress() {
        Scanner in = new Scanner(System.in);
        System.out.print("Email Address: ");
        String emailAddress = in.nextLine();
        if (Pattern.matches("^[a-zA-Z0-9_.-]{3,}@[a-zA-z]{2,4}\\.[a-zA-z]{2,4}$", emailAddress)) {
            this.emailAddress = emailAddress;
        } else {
            throw new InvalidEmployeeDetailException();
        }
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", employeeNumber=" + employeeNumber +
                ", fullName='" + fullName + '\'' +
                ", title='" + title + '\'' +
                ", department='" + department + '\'' +
                ", phoneNumber='" + phoneNumber + '\'' +
                ", emailAddress='" + emailAddress + '\'' +
                '}';
    }
}


import java.util.LinkedList;
import java.util.Scanner;

public class EmployeeManager {
    private static LinkedList<Employee> employees = new LinkedList<>();

    public static void addEmployee() {
        Employee employee = new Employee();
        employee.inputID();
        employee.inputEmployeeNumber();
        employee.inputFullName();
        employee.inputTitle();
        employee.inputDepartment();
        employee.inputPhoneNumber();
        employee.inputEmailAddress();
        employees.add(employee);
    }

    public static void updateEmployee() {
        Employee employee = searchEmployee();
        if (employee != null) {
            employee.inputID();
            employee.inputEmployeeNumber();
            employee.inputFullName();
            employee.inputTitle();
            employee.inputDepartment();
            employee.inputPhoneNumber();
            employee.inputEmailAddress();
        } else {
            System.out.println("Not found");
        }
    }

    public static Employee searchEmployee() {
        Scanner in = new Scanner(System.in);
        System.out.print("ID: ");
        int id = Integer.parseInt(in.nextLine());
        for (Employee employee : employees) {
            if (employee.getId() == id) {
                return employee;
            }
        }
        return null;
    }

    public static boolean deleteEmployee() {
        return employees.remove(searchEmployee());
    }

    public static void displayEmployees() {
        for (Employee employee : employees) {
            System.out.println(employee);
        }
    }


    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (true) {
            System.out.println("1. Add employee\n2. Update employee\n3. Search employee" +
                    "\n4. Display employees\n5. Delete employee\n0. Exit");
            switch (in.nextLine()) {
                case "1":
                    addEmployee();
                    break;
                case "2":
                    updateEmployee();
                    break;
                case "3":
                    Employee employee = searchEmployee();
                    System.out.println(employee == null ? "Not found" : employee);
                    break;
                case "4":
                    displayEmployees();
                    break;
                case "5":
                    deleteEmployee();
                    break;
                case "0":
                    System.exit(0);
            }
        }
    }
}


public class InvalidEmployeeDetailException extends RuntimeException{
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog