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
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
ObjectInputStream objectinputstream = null;
ArrayList
try {
FileInputStream streamIn = new FileInputStream(inputFile);
objectinputstream = new ObjectInputStream(streamIn);
listOfWorkers = (ArrayList
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
int id,birthYear,salary;
FileOutputStream fout = new FileOutputStream(fileName);
ObjectOutputStream oos = new ObjectOutputStream(fout);
Scanner input = new Scanner(System.in);
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 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
Get step-by-step solutions from verified subject matter experts
