0% found this document useful (0 votes)
92 views8 pages

PBLJ Experiment 2.4

This document describes a student's experiment on an employee management system program written in Java. The program allows users to perform CRUD (create, read, update, delete) operations on employee data stored in an arraylist, including adding, searching, editing, and deleting employees. Data is serialized and stored in an external file. The main menu displays options to perform these operations and basic input/output is done through the scanner class.

Uploaded by

amal suresh
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)
92 views8 pages

PBLJ Experiment 2.4

This document describes a student's experiment on an employee management system program written in Java. The program allows users to perform CRUD (create, read, update, delete) operations on employee data stored in an arraylist, including adding, searching, editing, and deleting employees. Data is serialized and stored in an external file. The main menu displays options to perform these operations and basic input/output is done through the scanner class.

Uploaded by

amal suresh
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/ 8

Experiment: - 2.

Student Name: Amal S UID: 20BCS7582


Branch: BE (CSE) Semester: 5th
Section/Group: 20BCS-806/A
Subject Name: Project Based Learning in JAVA Lab
Subject Code: 20CSP-321

Aim: - Employee Management System

Program: -
package LAB;

import java.io.*;
import java.util.*;

class Employee implements Serializable{

int id,age;
String name;
float salary;
long contact_no;
String email_id;

public Employee(int id, String name, int age, float salary, long contact_no, String
email_id)
{
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
this.contact_no = contact_no;
this.email_id = email_id;
}

public String toString() {


return "\nEmployee Details :" + "\nID: " + this.id + "\nName: " + this.age + "\
nAge: " + this.name + "\nSalary: "
+ this.salary + "\nContact No: " + this.contact_no + "\nEmail-id: " +
this.email_id;
}

public class EmployeeManagement


{
static void display(ArrayList<Employee> al)
{
System.out.println("\n--------------Employee List---------------\n");
System.out.printf("%-10s%-15s%-5s%-10s%-20s%-10s%n",
"ID","Name","Age","salary","contact-no","Email-Id");
for(Employee e : al)
{
System.out.printf("%-5s%-20s%-10s%-10s%-15s%-10s
%n",e.id,e.name,e.age,e.salary,e.contact_no,e.email_id);
}
}

@SuppressWarnings("unchecked")
public static void main(String[] args)
{
int id,age;
String name;
float salary;
long contact_no;
String email_id;

Scanner sc = new Scanner(System.in);


ArrayList<Employee> al = new ArrayList<Employee>();

File f = null;
FileInputStream fis = null;
ObjectInputStream ois = null;
FileOutputStream fos = null;
ObjectOutputStream oos =null;
try{

f = new File("C:\\Users\\rajat\\Documents\\PBLJ LAB\\PBLJ\\src\\LAB\\


EmployeeDataList1.txt");
if(f.exists())
{
fis = new FileInputStream(f);
ois = new ObjectInputStream(fis);
al = (ArrayList<Employee>)ois.readObject();
}

}
catch(Exception exp){
System.out.println(exp);
}
do
{
System.out.println("\n*********Welcome to the Employee Management
System**********\n");
System.out.println("""
1). Add an Employee
2). Search for Employee
3). Edit Employee details
4). Delete Employee Details
5). Display all
6). EXIT
""");
System.out.println("Enter your choice : ");
int ch = sc.nextInt();

switch (ch) {
case 1 -> {
System.out.println("\nEnter the following details to ADD list:\n");
System.out.println("Enter ID :");
id = sc.nextInt();
System.out.println("Enter Name :");
name = sc.next();
System.out.println("Enter Age :");
age = sc.nextInt();
System.out.println("Enter Salary :");
salary = sc.nextFloat();
System.out.println("Enter Contact No :");
contact_no = sc.nextLong();
System.out.println("Enter Email-ID :");
email_id = sc.next();
al.add(new Employee(id, name, age, salary, contact_no, email_id));
display(al);
}
case 2 -> {
System.out.println("Enter the Employee ID to search :");
id = sc.nextInt();
int i = 0;
for (Employee e : al) {
if (id == e.id) {
System.out.println(e + "\n");
i++;
}
}
if (i == 0) {
System.out.println("\nEmployee Details are not available,
Please enter a valid ID!!");
}
}
case 3 -> {
System.out.println("\nEnter the Employee ID to EDIT the details");
id = sc.nextInt();
int j = 0;
for (Employee e : al) {
if (id == e.id) {
j++;
do {
int ch1 = 0;
System.out.println("""

EDIT Employee Details :


1). Employee ID
2). Name
3). Age
4). Salary
5). Contact No.
6). Email-ID
7). GO BACK
""");
System.out.println("Enter your choice : ");
ch1 = sc.nextInt();
switch (ch1) {
case 1 -> {
System.out.println("\nEnter new Employee ID:");
e.id = sc.nextInt();
System.out.println(e + "\n");
}
case 2 -> {
System.out.println("Enter new Employee Name:");
e.name = sc.nextLine();
System.out.println(e + "\n");
}
case 3 -> {
System.out.println("Enter new Employee Age:");
e.age = sc.nextInt();
System.out.println(e + "\n");
}
case 4 -> {
System.out.println("Enter new Employee
Salary:");
e.salary = sc.nextFloat();
System.out.println(e + "\n");
}
case 5 -> {
System.out.println("Enter new Employee Contact
No. :");
e.contact_no = sc.nextLong();
System.out.println(e + "\n");
}
case 6 -> {
System.out.println("Enter new Employee Email-ID
:");
e.email_id = sc.next();
System.out.println(e + "\n");
}
case 7 -> j++;
default -> System.out.println("\nEnter a correct
choice from the List :");
}
}
while (j == 1);
}
}
if (j == 0) {
System.out.println("\nEmployee Details are not available,
Please enter a valid ID!!");
}
}
case 4 -> {
System.out.println("\nEnter Employee ID to DELETE from the
Databse :");
id = sc.nextInt();
int k = 0;
try {
for (Employee e : al) {
if (id == e.id) {
al.remove(e);
display(al);
k++;
}
}
if (k == 0) {
System.out.println("\nEmployee Details are not available,
Please enter a valid ID!!");
}
} catch (Exception ex) {
System.out.println(ex);
}
}
case 5 -> {
try {
al = (ArrayList<Employee>) ois.readObject();
} catch (ClassNotFoundException e2) {
System.out.println(e2);
} catch (Exception e2) {
System.out.println(e2);
}
display(al);
}
case 6 -> {
try {
assert f != null;
fos = new FileOutputStream(f);
oos = new ObjectOutputStream(fos);
oos.writeObject(al);
} catch (IOException e1) {
e1.printStackTrace();
} catch (Exception e2) {
e2.printStackTrace();
} finally {
try {
assert fis != null;
fis.close();
assert ois != null;
ois.close();
assert fos != null;
fos.close();
assert oos != null;
oos.close();
} catch (Exception e1) {
e1.printStackTrace();
}

}
System.out.println("\nYou have chosen EXIT !! Saving Files and
closing the tool.");
sc.close();
System.exit(0);
}
default -> System.out.println("\nEnter a correct choice from the
List :");
}
}
while(true);
}
}

Output: -

You might also like