Answer to Question #296393 in Java | JSP | JSF for Subhan khan

Question #296393

write an employee class storing above mentioned information ,employee id, employee salary name,title write a piece of code that creates an arraylist of 100 objects of employee and store the information in the file wrte a method that reads the above stored information from file and prints the employee detail


1
Expert's answer
2022-02-11T00:27:42-0500
public class Employee {
    private int id;
    private double salary;
    private String name;
    private String title;

    public Employee(int id, double salary, String name, String title) {
        this.id = id;
        this.salary = salary;
        this.name = name;
        this.title = title;
    }

    @Override
    public String toString() {
        return id + ";" + salary + ";" + name + ";" + title;
    }
}


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;

public class Main {

    static void readData(String fileName) {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)))) {
            String data;
            while ((data = reader.readLine()) != null) {
                System.out.println(data);
            }
        } catch (Exception e) {
        }
    }

    public static void main(String[] args) {
        ArrayList<Employee> employees = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            employees.add(new Employee(i, i * 10, Integer.toString(i), Integer.toString(i)));
        }
        try (PrintWriter writer = new PrintWriter("data.txt")) {
            for (Employee employee : employees) {
                writer.println(employee);
            }
        } catch (Exception e) {
        }
        readData("data.txt");
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS