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

Java 2.4

Uploaded by

Kartavya
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)
19 views

Java 2.4

Uploaded by

Kartavya
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

Experiment: 2.

Student Name: Yash Gahlawat UID: 21BCS9224


Branch: CSE Section/Group: CC_646 B
Semester: 6th Date of Performance:
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. Algorithm:
• Create a class Employee to store its details like id, name ,salary, age, etc.
• Create a EmployeeManager class where you ask for your choice using:
- Add an employee
- Display All
- Exit

4. Source Code:
import java.io.*;
import java.util.*;

class Employee {
private String name;
private int id;
private String designation;
private double salary;

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


this.name = name;
this.id = id;
this.designation = designation;
this.salary = salary;
}

public String getName() {


return name;
}

public int getId() {


return id;
}

public String getDesignation() {


return designation;
}

public double getSalary() {


return salary;
}
}

public class EmployeeManagementApp {


private static final String FILE_PATH = "employees.txt";

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
List<Employee> employees = new ArrayList<>();

while (true) {
System.out.println("Yash Gahlawat");
System.out.println("21BCS9224");
System.out.println("\nMenu:");
System.out.println("1. Add an Employee");
System.out.println("2. Display All Employees");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline

switch (choice) {
case 1:
addEmployee(employees, scanner);
break;
case 2:
displayAllEmployees(employees);
break;
case 3:
saveEmployeesToFile(employees);

System.out.println("Exiting the application. Goodbye!");


System.exit(0);
default:
System.out.println("Invalid choice. Please try again.");
}
}
}

private static void addEmployee(List<Employee> employees, Scanner scanner) {


System.out.print("Enter employee name: ");
String name = scanner.nextLine();
System.out.print("Enter employee ID: ");
int id = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Enter employee designation: ");
String designation = scanner.nextLine();
System.out.print("Enter employee salary: ");
double salary = scanner.nextDouble();
scanner.nextLine(); // Consume newline

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


employees.add(employee);
System.out.println("Employee added successfully!");
}

private static void displayAllEmployees(List<Employee> employees) {


if (employees.isEmpty()) {
System.out.println("No employees found.");
return;
}

System.out.println("\nEmployee Details:");
for (Employee employee : employees) {
System.out.println("Name: " + employee.getName());
System.out.println("ID: " + employee.getId());
System.out.println("Designation: " + employee.getDesignation());
System.out.println("Salary: $" + employee.getSalary());
System.out.println();
}
}

private static void saveEmployeesToFile(List<Employee> employees) {


try (PrintWriter writer = new PrintWriter(new FileWriter(FILE_PATH))) {
for (Employee employee : employees) {
writer.println(employee.getName() + "," + employee.getId() + "," +
employee.getDesignation() + "," + employee.getSalary());
}

System.out.println("Employee data saved to " + FILE_PATH);


} catch (IOException e) {
System.err.println("Error saving employee data to file.");
}
}
}

5. Screenshot of Output:-

6. Learning Outcomes:-
• Learn about concept of File Handling in java.
• Learn about LinkedList, Exception Handling in java.

You might also like