0% found this document useful (0 votes)
20 views5 pages

02 134232 106 119493111065 21052024 102152am

The document describes a Java program that creates Employee objects, writes them to a text file, reads and prints the file contents, updates some employee details, writes the updated details to a new file and prints it. The program contains classes for Employee and the main class that performs the file read/write operations.

Uploaded by

Anas Niaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views5 pages

02 134232 106 119493111065 21052024 102152am

The document describes a Java program that creates Employee objects, writes them to a text file, reads and prints the file contents, updates some employee details, writes the updated details to a new file and prints it. The program contains classes for Employee and the main class that performs the file read/write operations.

Uploaded by

Anas Niaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

OOP LAB 11

NAME: ANAS NIAZ


CLASS: BS CS 2B
ENROLLMENT: 02-134232-106

Lab12.java:
package lab12;
import java.io.*;
public class Lab12 {

public static void main(String[] args) {


Employee[] employees = new Employee[4];
employees[0] = new Employee("Anas", "HR", "03452452532", "HR Manager",
170000, "Active");
employees[1] = new Employee("Ali", "Finance", "034231453163","Finance
Manager", 55000, "Non-Active");
employees[2] = new Employee("Ahsan", "IT", "03415767211", "Software
Engineer", 120000, "Active");
employees[3] = new Employee("Shayan", "Marketing", "03415483748",
"Marketing Manager", 47000, "Active");
try (PrintWriter writer = new PrintWriter("EmployeeData.txt")) {
for (Employee employee : employees) {
writer.print(employee);
}
} catch (FileNotFoundException e) {
System.err.println("Error writing to file: " + e.getMessage());
}

try (BufferedReader reader = new BufferedReader(new


FileReader("EmployeeData.txt"))) {
System.out.println("Employee data:");
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.err.println("Error reading from file: " + e.getMessage());
}

System.out.println("Updating the data of the Employees");


employees[1].salary = 120000;
employees[1].status = "Active";
employees[2].name = "Anas Niaz";
employees[2].contact="030746376487";
employees[3].department ="Adminstrations";

try (PrintWriter writer = new PrintWriter("UpdatedEmployeeInfo.txt")) {


for (Employee employee : employees) {
writer.print(employee);
}
} catch (FileNotFoundException e) {
System.err.println("Error writing to file: " + e.getMessage());
}

try (BufferedReader reader = new BufferedReader(new


FileReader("UpdatedEmployeeInfo.txt"))) {
System.out.println("\nUpdated employee data:");
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.err.println("Error reading from file: " + e.getMessage());
}
}
}

Employee.java:
package lab12;
public class Employee {
String name;
String department;
String contact;
String designation;
int salary;
String status;

public Employee(String name, String department, String contact, String designation, int
salary, String status) {
this.name = name;
this.department = department;
this.contact = contact;
this.designation = designation;
this.salary = salary;
this.status = status;
}

@Override
public String toString() {
return String.format("%s, %s, %s, %s, %d, %s%n", name, department, contact, designation,
salary, status);
}
}
OUTPUT:

You might also like