Write a java program to create an employee record for n people and store those details in file and
display the required details.
Employee record should include:
• Employee Name:
• Employee id:
• Designation:
• Salary:
Display all those employees having salary above Rs. 10000.
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Employee {
String name;
int id;
String designation;
double salary;
public Employee(String name, int id, String designation, double salary) {
this.name = name;
this.id = id;
this.designation = designation;
this.salary = salary;
}
@Override
public String toString() {
return "Employee Name: " + name + "\nEmployee id: " + id +
"\nDesignation: " + designation + "\nSalary: " + salary;
}
}
public class EmployeeRecord {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of employees: ");
int n = scanner.nextInt();
List<Employee> employees = new ArrayList<>();
for (int i = 0; i < n; i++) {
System.out.println("Enter details for employee " + (i + 1) + ":");
System.out.print("Employee Name: ");
scanner.nextLine(); // Consume the newline character
String name = scanner.nextLine();
System.out.print("Employee id: ");
int id = scanner.nextInt();
System.out.print("Designation: ");
scanner.nextLine();
String designation = scanner.nextLine();
System.out.print("Salary: ");
double salary = scanner.nextDouble();
Employee employee = new Employee(name, id, designation, salary);
employees.add(employee);
}
// Write employee details to a file
try (PrintWriter writer = new PrintWriter("employee_details.txt")) {
for (Employee employee : employees) {
writer.println(employee.toString() + "\n");
}
} catch (IOException e) {
System.err.println("Error writing to the file: " +
e.getMessage());
}
System.out.println("\nEmployees with a salary above 10,000:");
for (Employee employee : employees) {
if (employee.salary > 10000) {
System.out.println(employee.toString());
}
}
}
}
Input:
Output:
File: