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

Exp 2.4

The document describes a menu-based Java application to manage employee data. The application allows adding an employee, displaying all employees, and exiting. Employee details are stored in a linked list and saved to a file.

Uploaded by

Cross Eks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views6 pages

Exp 2.4

The document describes a menu-based Java application to manage employee data. The application allows adding an employee, displaying all employees, and exiting. Employee details are stored in a linked list and saved to a file.

Uploaded by

Cross Eks
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 2.4
Student Name: Kaustabh Pal UID: 21BCS8791
Branch: CSE Section/Group: SC-905(B)
Semester: 6th Date of Performance: 07-03-24
Subject Name: JAVA LAB Subject Code: 21CSH-319

1. Aim: Create a menu-based Java application with the following options,1. Add
an Employee 2.Display All 3.Exit. If option 1 is selected, the application should
gather details of the employee like employee name, employee id, designation
and salary and store it in a file. If option 2 is selected, the application should
display all the employee details. If option 3 is selected the application should
exit.

2. Objective: To learn about concept of File Handling in java, to learn


about LinkedList, Exception Handling in java.

3. Input/Apparatus:
Hardware Requirements:- Minimum 384MB RAM, 100 GB hard disk,
processor with 2.1 MHz

Software Requirements:- Eclipse, NetBeans, IntelliJ, etc.

4. Procedure:
import java.io.*;

import java.util.LinkedList;

import java.util.Scanner;

class Employee implements Serializable {

private static final long serialVersionUID = 1L;

int id;

String name;

String designation;
double salary;

public Employee(int id, String name, String designation, double salary) {

this.id = id;

this.name = name;

this.designation = designation;

this.salary = salary;

@Override

public String toString() {

return "Employee{" +

"id=" + id +

", name='" + name + '\'' +

", designation='" + designation + '\'' +

", salary=" + salary +

'}';

public class EmployeeManager {

static LinkedList<Employee> employeeList = new LinkedList<>();

static final String FILE_PATH = "employees.dat";

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

while (true) {
System.out.println("\n1. Add an Employee");

System.out.println("2. Display All");

System.out.println("3. Exit");

System.out.print("Enter your choice: ");

int choice = scanner.nextInt();

switch (choice) {

case 1:

addEmployee(scanner);

break;

case 2:

displayAll();

break;

case 3:

System.out.println("Exiting...");

return;

default:

System.out.println("Invalid choice. Please enter a valid one.");

private static void addEmployee(Scanner scanner) {

System.out.print("Enter Employee ID: ");

int id = scanner.nextInt();

scanner.nextLine(); // consume newline

System.out.print("Enter Employee Name: ");

String name = scanner.nextLine();


System.out.print("Enter Designation: ");

String designation = scanner.nextLine();

System.out.print("Enter Salary: ");

double salary = scanner.nextDouble();

Employee employee = new Employee(id, name, designation, salary);

employeeList.add(employee);

saveToFile();

private static void displayAll() {

readFromFile();

if (employeeList.isEmpty()) {

System.out.println("No employees to display.");

return;

for (Employee employee : employeeList) {

System.out.println(employee);

private static void saveToFile() {

try (ObjectOutputStream oos = new ObjectOutputStream(new


FileOutputStream(FILE_PATH))) {

oos.writeObject(employeeList);

} catch (IOException e) {

e.printStackTrace();

}
}

@SuppressWarnings("unchecked")

private static void readFromFile() {

File file = new File(FILE_PATH);

if (!file.exists()) return;

try (ObjectInputStream ois = new ObjectInputStream(new


FileInputStream(FILE_PATH))) {

Object object = ois.readObject();

employeeList = (LinkedList<Employee>) object;

} catch (IOException | ClassNotFoundException e) {

e.printStackTrace();

}
5. Output:

You might also like