0% found this document useful (0 votes)
18 views3 pages

Java 4 Nov Ques2

The program allows a user to enter employee details like name, ID, designation and salary for n number of employees. It stores these details in an external text file. The program then displays details of employees whose salary is above Rs. 10,000 by reading from the text file.
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)
18 views3 pages

Java 4 Nov Ques2

The program allows a user to enter employee details like name, ID, designation and salary for n number of employees. It stores these details in an external text file. The program then displays details of employees whose salary is above Rs. 10,000 by reading from the text file.
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/ 3

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:

You might also like