0% found this document useful (0 votes)
24 views5 pages

Java 2.4 Nites

The document describes a menu driven Java application to manage employee data. The application allows adding an employee, displaying all employees, and exiting. Employee details like name, ID, age, and salary are stored in a file using file handling and serialization in Java.

Uploaded by

Shivanshu Sharma
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)
24 views5 pages

Java 2.4 Nites

The document describes a menu driven Java application to manage employee data. The application allows adding an employee, displaying all employees, and exiting. Employee details like name, ID, age, and salary are stored in a file using file handling and serialization in Java.

Uploaded by

Shivanshu Sharma
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/ 5

Experiment 2.

Student Name: Nitesh UID: 21BCS5320


Branch: BE-CSE Section/Group:SC_907/ B
Semester: 6 Date of Performance:06-03-
2024 Subject Name: Java Lab
Subject Code:21CSH-319

1. Aim: Create a menu based Java application with the following options.1. Add an
Employee2.Display All3.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. Algo. /Approach and output:

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

class Employee implements Serializable {


private static final long serialVersionUID = 1L;
private int id; private String name; private int
age;
private double salary;
public Employee(int id, String name, int age, double salary) {
this.id = id;
this.name = name;
this.age = age; this.salary
= salary;
}

@Override public String toString() { return


id + " " + name + " " + age + " " + salary;
}
}

public class emp2_4 { private static final String FILE_NAME =


"employee_data.txt"; private static final Scanner scanner = new
Scanner(System.in); private static final List<Employee> employees
= new ArrayList<>();

public static void main(String[] args) {


loadDataFromFile();

while (true) {
System.out.println("\nMain Menu");
System.out.println("1. Add an Employee");
System.out.println("2. Display All");
System.out.println("3. Exit");

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


int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice)
{
case 1:
addEmployee();
break; case 2:
displayAllEmployees();
break; case 3:
saveDataToFile();
System.out.println("Exiting the System"); return;
default:
System.out.println("Invalid choice!");
}
}
}

private static void addEmployee() {


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 Employee Age: "); int age
= scanner.nextInt();
System.out.print("Enter Employee Salary: ");
double salary = scanner.nextDouble();

employees.add(new Employee(id, name, age, salary));


System.out.println("Employee added successfully.");
}

private static void displayAllEmployees() {


System.out.println("----Report-----"); for
(Employee emp : employees) { System.out.println(emp);
}
System.out.println("----End of Report-----");
}

private static void saveDataToFile() {


try (ObjectOutputStream oos = new ObjectOutputStream(new
FileOutputStream(FILE_NAME))) {
oos.writeObject(employees);
System.out.println("Data saved to file: " + FILE_NAME);
} catch (IOException e) { e.printStackTrace();
}
}

@SuppressWarnings("unchecked") private
static void loadDataFromFile() { File file
= new File(FILE_NAME);
if (file.exists()) {
try (ObjectInputStream ois = new ObjectInputStream(new
FileInputStream(FILE_NAME))) {
employees.addAll((List<Employee>) ois.readObject());
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}

Output:-

You might also like