0% found this document useful (0 votes)
20 views6 pages

Employee Manager Assignment

The document contains Java code for an Employee Management System consisting of three classes: Employee, EmployeeManager, and EmployeeApp. The Employee class defines employee attributes and methods, the EmployeeManager class manages employee records (adding, removing, and listing employees), and the EmployeeApp class provides a graphical user interface for user interaction. A video demonstrating the code functionality is also included with the document.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views6 pages

Employee Manager Assignment

The document contains Java code for an Employee Management System consisting of three classes: Employee, EmployeeManager, and EmployeeApp. The Employee class defines employee attributes and methods, the EmployeeManager class manages employee records (adding, removing, and listing employees), and the EmployeeApp class provides a graphical user interface for user interaction. A video demonstrating the code functionality is also included with the document.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Note: This is the code that I have used.

I created three different


classes with different functions. A video has been posted along
with this file showing how the code works.

Employee Class
import java.text.SimpleDateFormat;
import java.util.Date;

public class Employee {


private int id;
private String firstName;
private String lastName;
private double salary;
private Date startDate;

// Constructor
public Employee(int id, String firstName, String lastName, double salary, Date startDate) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.salary = salary;
this.startDate = startDate;
}

// Getters
public int getId() {
return id;
}

public String getFirstName() {


return firstName;
}

public String getLastName() {


return lastName;
}

public double getSalary() {


return salary;
}

public Date getStartDate() {


return startDate;
}

// toString method
@Override
public String toString() {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
return "Employee ID: " + id +
"\nFirst Name: " + firstName +
"\nLast Name: " + lastName +
"\nSalary: " + salary +
"\nStart Date: " + dateFormat.format(startDate);
}
}

Employee Manager Class


import java.util.ArrayList;
import java.util.Date;

public class EmployeeManager {


private ArrayList<Employee> employees;

public EmployeeManager() {
employees = new ArrayList<>();
}

// Add employee
public boolean addEmployee(int id, String firstName, String lastName, double salary, Date
startDate) {
// Check if ID already exists
for (Employee emp : employees) {
if (emp.getId() == id) {
return false; // ID exists
}
}

// Add the employee to the list


Employee newEmployee = new Employee(id, firstName, lastName, salary, startDate);
employees.add(newEmployee);
return true;
}

// Remove employee by ID
public boolean removeEmployee(int id) {
for (Employee emp : employees) {
if (emp.getId() == id) {
employees.remove(emp);
return true;
}
}
return false; // Employee not found
}

// List all employees


public ArrayList<Employee> listEmployees() {
return employees;
}
}

Employee App Class (for GUI)


import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.text.ParseException;

public class EmployeeApp {


private static EmployeeManager manager = new EmployeeManager();

// GUI components
private static JTextArea outputArea = new JTextArea(10, 30);
private static JTextField idField = new JTextField(15);
private static JTextField firstNameField = new JTextField(15);
private static JTextField lastNameField = new JTextField(15);
private static JTextField salaryField = new JTextField(15);
private static JTextField dateField = new JTextField(15);

public static void main(String[] args) {


JFrame frame = new JFrame("Employee Management System");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setLayout(new FlowLayout());

// Labels and input fields


frame.add(new JLabel("Employee ID:"));
frame.add(idField);
frame.add(new JLabel("First Name:"));
frame.add(firstNameField);

frame.add(new JLabel("Last Name:"));


frame.add(lastNameField);

frame.add(new JLabel("Salary:"));
frame.add(salaryField);

frame.add(new JLabel("Start Date (dd/MM/yyyy):"));


frame.add(dateField);

// Buttons
JButton listButton = new JButton("List");
JButton addButton = new JButton("Add");
JButton removeButton = new JButton("Remove");

frame.add(listButton);
frame.add(addButton);
frame.add(removeButton);

// Output area for displaying messages


outputArea.setEditable(false);
frame.add(new JScrollPane(outputArea));

// List button action


listButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
listEmployees();
}
});

// Add button action


addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
addEmployee();
}
});

// Remove button action


removeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
removeEmployee();
}
});

frame.setVisible(true);
}

private static void listEmployees() {


outputArea.setText("");
for (Employee emp : manager.listEmployees()) {
outputArea.append(emp.toString() + "\n\n");
}
}

private static void addEmployee() {


try {
int id = Integer.parseInt(idField.getText().trim());
String firstName = firstNameField.getText().trim();
String lastName = lastNameField.getText().trim();
double salary = Double.parseDouble(salaryField.getText().trim());
String dateStr = dateField.getText().trim();

// Validate input fields


if (firstName.isEmpty() || lastName.isEmpty() || dateStr.isEmpty()) {
showError("Fields cannot be left blank.");
return;
}

// Parse the date


SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date startDate = dateFormat.parse(dateStr);

// Try adding the employee


boolean success = manager.addEmployee(id, firstName, lastName, salary, startDate);
if (success) {
showError("Employee added successfully.");
} else {
showError("Employee ID already exists.");
}
} catch (NumberFormatException | ParseException e) {
showError("Invalid input data. Ensure all fields are correct.");
}
}

private static void removeEmployee() {


try {
int id = Integer.parseInt(idField.getText().trim());

boolean success = manager.removeEmployee(id);


if (success) {
showError("Employee removed successfully.");
} else {
showError("Employee ID not found.");
}
} catch (NumberFormatException e) {
showError("Invalid ID format.");
}
}

private static void showError(String message) {


outputArea.setText(message);
}
}

You might also like