// Entity Classes
// [Link]
package [Link];
import [Link];
public class Employee {
private int employeeId;
private String firstName;
private String lastName;
private String employeeName;
private int employeeAge;
private Date dateOfBirth;
private String gender;
private String email;
private String phoneNo;
private Date hireDate;
private int departmentId;
// Constructors
public Employee() {}
public Employee(int employeeId, String firstName, String lastName, String
gender, String email) {
[Link] = employeeId;
[Link] = firstName;
[Link] = lastName;
[Link] = firstName + " " + lastName;
[Link] = gender;
[Link] = email;
}
// Getters and Setters
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
[Link] = employeeId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
[Link] = firstName;
[Link] = [Link] + " " + [Link];
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
[Link] = lastName;
[Link] = [Link] + " " + [Link];
}
// Other getters and setters
public String getEmployeeName() {
return employeeName;
}
public int getEmployeeAge() {
return employeeAge;
}
public void setEmployeeAge(int employeeAge) {
[Link] = employeeAge;
}
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
[Link] = dateOfBirth;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
[Link] = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
[Link] = email;
}
public String getPhoneNo() {
return phoneNo;
}
public void setPhoneNo(String phoneNo) {
[Link] = phoneNo;
}
public Date getHireDate() {
return hireDate;
}
public void setHireDate(Date hireDate) {
[Link] = hireDate;
}
public int getDepartmentId() {
return departmentId;
}
public void setDepartmentId(int departmentId) {
[Link] = departmentId;
}
@Override
public String toString() {
return "Employee{" +
"employeeId=" + employeeId +
", name='" + employeeName + '\'' +
", email='" + email + '\'' +
", department=" + departmentId +
'}';
}
}
// [Link]
package [Link];
public class Department {
private int departmentId;
private String departmentName;
private double departmentBudget;
private double remainingBudget;
// Constructors
public Department() {}
public Department(int departmentId, String departmentName) {
[Link] = departmentId;
[Link] = departmentName;
}
// Getters and Setters
public int getDepartmentId() {
return departmentId;
}
public void setDepartmentId(int departmentId) {
[Link] = departmentId;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
[Link] = departmentName;
}
public double getDepartmentBudget() {
return departmentBudget;
}
public void setDepartmentBudget(double departmentBudget) {
[Link] = departmentBudget;
}
public double getRemainingBudget() {
return remainingBudget;
}
public void setRemainingBudget(double remainingBudget) {
[Link] = remainingBudget;
}
@Override
public String toString() {
return "Department{" +
"departmentId=" + departmentId +
", departmentName='" + departmentName + '\'' +
", budget=" + departmentBudget +
'}';
}
}
// [Link] - Database Utility Class
package [Link];
import [Link];
import [Link];
import [Link];
public class DBUtil {
private static final String URL =
"jdbc:mysql://localhost:3306/employee_management";
private static final String USERNAME = "root";
private static final String PASSWORD = "password"; // Change to your MySQL
password
static {
try {
[Link]("[Link]");
} catch (ClassNotFoundException e) {
[Link]();
}
}
public static Connection getConnection() throws SQLException {
return [Link](URL, USERNAME, PASSWORD);
}
public static void closeConnection(Connection connection) {
if (connection != null) {
try {
[Link]();
} catch (SQLException e) {
[Link]();
}
}
}
}
// [Link] - Data Access Object for Employee
package [Link];
import [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
public class EmployeeDAO {
public boolean addEmployee(Employee employee) {
String sql = "INSERT INTO Employee (Employee_ID, First_name, Last_name,
Employee_name, " +
"Employee_age, Date_of_birth, Gender, Email, Phone_NO,
Hire_date, Department_ID) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
try (Connection conn = [Link]();
PreparedStatement pstmt = [Link](sql)) {
[Link](1, [Link]());
[Link](2, [Link]());
[Link](3, [Link]());
[Link](4, [Link]());
[Link](5, [Link]());
[Link](6, new
[Link]([Link]().getTime()));
[Link](7, [Link]());
[Link](8, [Link]());
[Link](9, [Link]());
[Link](10, new [Link]([Link]().getTime()));
[Link](11, [Link]());
int rowsAffected = [Link]();
return rowsAffected > 0;
} catch (SQLException e) {
[Link]();
return false;
}
}
public Employee getEmployeeById(int employeeId) {
String sql = "SELECT * FROM Employee WHERE Employee_ID = ?";
try (Connection conn = [Link]();
PreparedStatement pstmt = [Link](sql)) {
[Link](1, employeeId);
ResultSet rs = [Link]();
if ([Link]()) {
Employee employee = new Employee();
[Link]([Link]("Employee_ID"));
[Link]([Link]("First_name"));
[Link]([Link]("Last_name"));
[Link]([Link]("Employee_age"));
[Link]([Link]("Date_of_birth"));
[Link]([Link]("Gender"));
[Link]([Link]("Email"));
[Link]([Link]("Phone_NO"));
[Link]([Link]("Hire_date"));
[Link]([Link]("Department_ID"));
return employee;
}
} catch (SQLException e) {
[Link]();
}
return null;
}
public List<Employee> getAllEmployees() {
List<Employee> employees = new ArrayList<>();
String sql = "SELECT * FROM Employee";
try (Connection conn = [Link]();
Statement stmt = [Link]();
ResultSet rs = [Link](sql)) {
while ([Link]()) {
Employee employee = new Employee();
[Link]([Link]("Employee_ID"));
[Link]([Link]("First_name"));
[Link]([Link]("Last_name"));
[Link]([Link]("Employee_age"));
[Link]([Link]("Date_of_birth"));
[Link]([Link]("Gender"));
[Link]([Link]("Email"));
[Link]([Link]("Phone_NO"));
[Link]([Link]("Hire_date"));
[Link]([Link]("Department_ID"));
[Link](employee);
}
} catch (SQLException e) {
[Link]();
}
return employees;
}
public boolean updateEmployee(Employee employee) {
String sql = "UPDATE Employee SET First_name = ?, Last_name = ?,
Employee_name = ?, " +
"Employee_age = ?, Date_of_birth = ?, Gender = ?, Email = ?, "
+
"Phone_NO = ?, Hire_date = ?, Department_ID = ? WHERE
Employee_ID = ?";
try (Connection conn = [Link]();
PreparedStatement pstmt = [Link](sql)) {
[Link](1, [Link]());
[Link](2, [Link]());
[Link](3, [Link]());
[Link](4, [Link]());
[Link](5, new
[Link]([Link]().getTime()));
[Link](6, [Link]());
[Link](7, [Link]());
[Link](8, [Link]());
[Link](9, new [Link]([Link]().getTime()));
[Link](10, [Link]());
[Link](11, [Link]());
int rowsAffected = [Link]();
return rowsAffected > 0;
} catch (SQLException e) {
[Link]();
return false;
}
}
public boolean deleteEmployee(int employeeId) {
String sql = "DELETE FROM Employee WHERE Employee_ID = ?";
try (Connection conn = [Link]();
PreparedStatement pstmt = [Link](sql)) {
[Link](1, employeeId);
int rowsAffected = [Link]();
return rowsAffected > 0;
} catch (SQLException e) {
[Link]();
return false;
}
}
public List<Employee> getEmployeesByDepartment(int departmentId) {
List<Employee> employees = new ArrayList<>();
String sql = "SELECT * FROM Employee WHERE Department_ID = ?";
try (Connection conn = [Link]();
PreparedStatement pstmt = [Link](sql)) {
[Link](1, departmentId);
ResultSet rs = [Link]();
while ([Link]()) {
Employee employee = new Employee();
[Link]([Link]("Employee_ID"));
[Link]([Link]("First_name"));
[Link]([Link]("Last_name"));
[Link]([Link]("Employee_age"));
[Link]([Link]("Date_of_birth"));
[Link]([Link]("Gender"));
[Link]([Link]("Email"));
[Link]([Link]("Phone_NO"));
[Link]([Link]("Hire_date"));
[Link]([Link]("Department_ID"));
[Link](employee);
}
} catch (SQLException e) {
[Link]();
}
return employees;
}
}
// Main Application Class
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class EmployeeManagementSystem {
private static Scanner scanner = new Scanner([Link]);
private static EmployeeDAO employeeDAO = new EmployeeDAO();
private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-
dd");
public static void main(String[] args) {
boolean exit = false;
while (!exit) {
[Link]("\n===== Employee Management System =====");
[Link]("1. Add Employee");
[Link]("2. View Employee Details");
[Link]("3. View All Employees");
[Link]("4. Update Employee");
[Link]("5. Delete Employee");
[Link]("6. View Employees by Department");
[Link]("0. Exit");
[Link]("Enter your choice: ");
int choice = [Link]();
[Link](); // Clear buffer
switch (choice) {
case 1:
addEmployee();
break;
case 2:
viewEmployeeDetails();
break;
case 3:
viewAllEmployees();
break;
case 4:
updateEmployee();
break;
case 5:
deleteEmployee();
break;
case 6:
viewEmployeesByDepartment();
break;
case 0:
exit = true;
[Link]("Thank you for using Employee Management
System!");
break;
default:
[Link]("Invalid choice. Please try again.");
}
}
}
private static void addEmployee() {
try {
Employee employee = new Employee();
[Link]("Enter Employee ID: ");
[Link]([Link]());
[Link](); // Clear buffer
[Link]("Enter First Name: ");
[Link]([Link]());
[Link]("Enter Last Name: ");
[Link]([Link]());
[Link]("Enter Age: ");
[Link]([Link]());
[Link](); // Clear buffer
[Link]("Enter Date of Birth (yyyy-MM-dd): ");
String dobStr = [Link]();
Date dob = [Link](dobStr);
[Link](dob);
[Link]("Enter Gender (Male/Female/Other): ");
[Link]([Link]());
[Link]("Enter Email: ");
[Link]([Link]());
[Link]("Enter Phone Number: ");
[Link]([Link]());
[Link]("Enter Hire Date (yyyy-MM-dd): ");
String hireDateStr = [Link]();
Date hireDate = [Link](hireDateStr);
[Link](hireDate);
[Link]("Enter Department ID: ");
[Link]([Link]());
[Link](); // Clear buffer
boolean success = [Link](employee);
if (success) {
[Link]("Employee added successfully!");
} else {
[Link]("Failed to add employee.");
}
} catch (ParseException e) {
[Link]("Invalid date format. Please use yyyy-MM-dd
format.");
}
}
private static void viewEmployeeDetails() {
[Link]("Enter Employee ID: ");
int employeeId = [Link]();
[Link](); // Clear buffer
Employee employee = [Link](employeeId);
if (employee != null) {
[Link]("\n===== Employee Details =====");
[Link]("ID: " + [Link]());
[Link]("Name: " + [Link]());
[Link]("Age: " + [Link]());
[Link]("Date of Birth: " +
[Link]([Link]()));
[Link]("Gender: " + [Link]());
[Link]("Email: " + [Link]());
[Link]("Phone: " + [Link]());
[Link]("Hire Date: " +
[Link]([Link]()));
[Link]("Department ID: " + [Link]());
} else {
[Link]("Employee not found.");
}
}
private static void viewAllEmployees() {
List<Employee> employees = [Link]();
if ([Link]()) {
[Link]("No employees found.");
return;
}
[Link]("\n===== All Employees =====");
[Link]("%-5s %-20s %-10s %-30s %-10s%n", "ID", "Name", "Gender",
"Email", "Dept ID");
[Link]("-------------------------------------------------------------")
;
for (Employee employee : employees) {
[Link]("%-5d %-20s %-10s %-30s %-10d%n",
[Link](),
[Link](),
[Link](),
[Link](),
[Link]());
}
}
private static void updateEmployee() {
[Link]("Enter Employee ID to update: ");
int employeeId = [Link]();
[Link](); // Clear buffer
Employee employee = [Link](employeeId);
if (employee == null) {
[Link]("Employee not found.");
return;
}
try {
[Link]("Enter First Name (current: " +
[Link]() + "): ");
String firstName = [Link]();
if (![Link]()) {
[Link](firstName);
}
[Link]("Enter Last Name (current: " + [Link]()
+ "): ");
String lastName = [Link]();
if (![Link]()) {
[Link](lastName);
}
[Link]("Enter Age (current: " + [Link]() +
"): ");
String ageStr = [Link]();
if (![Link]()) {
[Link]([Link](ageStr));
}
[Link]("Enter Email (current: " + [Link]() + "):
");
String email = [Link]();
if (![Link]()) {
[Link](email);
}
[Link]("Enter Phone Number (current: " +
[Link]() + "): ");
String phone = [Link]();
if (![Link]()) {
[Link](phone);
}
[Link]("Enter Department ID (current: " +
[Link]() + "): ");
String deptIdStr = [Link]();
if (![Link]()) {
[Link]([Link](deptIdStr));
}
boolean success = [Link](employee);
if (success) {
[Link]("Employee updated successfully!");
} else {
[Link]("Failed to update employee.");
}
} catch (NumberFormatException e) {
[Link]("Invalid number format.");
}
}
private static void deleteEmployee() {
[Link]("Enter Employee ID to delete: ");
int employeeId = [Link]();
[Link](); // Clear buffer
[Link]("Are you sure you want to delete this employee? (y/n): ");
String confirm = [Link]();
if ([Link]("y")) {
boolean success = [Link](employeeId);
if (success) {
[Link]("Employee deleted successfully!");
} else {
[Link]("Failed to delete employee.");
}
} else {
[Link]("Deletion cancelled.");
}
}
private static void viewEmployeesByDepartment() {
[Link]("Enter Department ID: ");
int departmentId = [Link]();
[Link](); // Clear buffer
List<Employee> employees =
[Link](departmentId);
if ([Link]()) {
[Link]("No employees found in this department.");
return;
}
[Link]("\n===== Employees in Department " + departmentId + "
=====");
[Link]("%-5s %-20s %-10s %-30s%n", "ID", "Name", "Gender",
"Email");
[Link]("-------------------------------------------------------------")
;
for (Employee employee : employees) {
[Link]("%-5d %-20s %-10s %-30s%n",
[Link](),
[Link](),
[Link](),
[Link]());
}
}
}