Experiment 2.
Student Name:Manoj Gogoi UID: 21BCS7877
Branch: BE-CSE Section/Group: 652 B
Semester: 6 Date of Performance:15-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:-