Question: Question 2: Resolve this code of question one by using text files. (in java programing language) //---------------------------------- // Worker class storing the data members and

Question 2:

Resolve this code of question one by using text files.

(in java programing language)

//----------------------------------

// Worker class storing the data members and methods of Worker objects

import java.io.Serializable;

public class Worker implements Serializable {

private int id;

private int birthYear;

private int salary;

public Worker(int id, int birthYear, int salary) {

this.id = id;

this.birthYear = birthYear;

this.salary = salary;

}

public int getBirthYear() {

return birthYear;

}

public int getSalary() {

return salary;

}

public void setSalary(int s) {

salary = s;

}

@Override

public String toString() {

return "Worker [id=" + id + ", birthYear=" + birthYear + ", salary="

+ salary + "]";

}

}

// end of Worker class

// java program implementing Worker class

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.util.ArrayList;

import java.util.Scanner;

public class WorkerReadWrite {

public static void printWorkers(ArrayList workers) {

for(Worker w: workers) {

System.out.println(w);

}

}

// method that produces new serial file of all workers in input file such that the salary of workers born before 1992 is increased by 150 (other workers salary has no change).

public static ArrayList changeSalary (String inputFile, String outputFile) throws IOException {

ObjectInputStream objectinputstream = null;

ArrayList listOfWorkers = null;

try {

FileInputStream streamIn = new FileInputStream(inputFile);

objectinputstream = new ObjectInputStream(streamIn);

listOfWorkers = (ArrayList) objectinputstream.readObject();

for(Worker w: listOfWorkers) {

if(w.getBirthYear() <= 1992) {

w.setSalary(w.getSalary() + 150);

}

}

// write to output file

FileOutputStream fout = new FileOutputStream(outputFile);

ObjectOutputStream oos = new ObjectOutputStream(fout);

oos.writeObject(listOfWorkers);

oos.close();

} catch (Exception e) {

e.printStackTrace();

} finally {

if(objectinputstream != null){

objectinputstream .close();

}

}

return listOfWorkers;

}

// method that reads N worker objects (N is user-input) into file: w1.ser

public static ArrayList writeWorkers(String fileName, int n) throws IOException {

int id,birthYear,salary;

FileOutputStream fout = new FileOutputStream(fileName);

ObjectOutputStream oos = new ObjectOutputStream(fout);

Scanner input = new Scanner(System.in);

ArrayList listOfWorkers = new ArrayList<>();

// input of id, birthYear and salary of n workers

for(int i=0; i

System.out.print(" ID for Worker-"+(i+1)+" : ");

id = input.nextInt();

System.out.print(" Birth year for Worker-"+(i+1)+" : ");

birthYear = input.nextInt();

System.out.print(" Salary for Worker-"+(i+1)+" : ");

salary = input.nextInt();

listOfWorkers.add(new Worker(id ,birthYear,salary));

}

oos.writeObject(listOfWorkers);

oos.close();

input.close();

return listOfWorkers;

}

public static void main(String[] args) throws IOException {

int n;

Scanner scan = new Scanner(System.in);// used for taking input from the user

// take input for number of workers

System.out.print(" Enter the number of workers : ");

n = scan.nextInt();

System.out.println(" Writing workers to file w1.ser");

String input = "w1.ser";

String output = "w2.ser";

ArrayList workers = writeWorkers(input, n);

printWorkers(workers);

workers = changeSalary(input, output);

System.out.println(" After changing the salary:");

printWorkers(workers);

scan.close();

}

}

// end of WorkerReadWrite class

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!