Java 2.4
Java 2.4
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;
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("\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();
}
}
5. Screenshot of Output:-
6. Learning Outcomes:-
• Learn about concept of File Handling in java.
• Learn about LinkedList, Exception Handling in java.