0% found this document useful (0 votes)
17 views

2.4 Java

The document describes a menu driven Java application to manage employee data. The application allows adding an employee, displaying all employees, and exiting. It uses file handling to store employee objects in a file and load them back.

Uploaded by

Scooby Dobby
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)
17 views

2.4 Java

The document describes a menu driven Java application to manage employee data. The application allows adding an employee, displaying all employees, and exiting. It uses file handling to store employee objects in a file and load them back.

Uploaded by

Scooby Dobby
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/ 4

DEPARTMENTOF

COMPUTERSCIENCE&ENGINEERING
Experiment 2.4

Student Name: Shreya Sharma UID:21BCS6785


Branch: CSE Section/Group: CC-636(B)
Semester: 6th Date of Performance: 29/02/24
Subject Name: PBL with Java 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 the concept of File Handling in Java.
• To learn about Linked List and Exception Handling in Java.

3. Algo. /Code:
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; }
}
DEPARTMENTOF
COMPUTERSCIENCE&ENGINEERING

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;
case2:
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."); }
DEPARTMENTOF
COMPUTERSCIENCE&ENGINEERING
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();
}
}
}
}
DEPARTMENTOF
COMPUTERSCIENCE&ENGINEERING
4. Output:

Learning Outcomes:
Learn about the concept of File Handling in Java, Linked Lists and Exceptional Handling in Java.

You might also like