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

Question #289887

A hospital stores information about its doctors in a text file using the following format:


DoctorID;DoctorName;DoctorSpecialization;DoctorDepartment;DoctorSalary



Write a program with the following functionalities:


Write a Doctor class to store all the relevant information of the Doctor as mentioned above. toString() method to display the doctor’s information is also required.


Assume that the doctors data is stored in file at path “D://Doctors.txt”, write a program that reads doctors information one line at a time and store it in the objects of Doctor Class.


Write a Hospital class containing the ArrayList of Doctor objects read from the file. Write a sort method in the Hospital class that sorts all the Doctors in ascending order w.r.t. their salary and print their details.

1
Expert's answer
2022-01-22T16:40:24-0500
public class Doctor {
    private int id;
    private String name;
    private String specialization;
    private String department;
    private double salary;

    public Doctor(int id, String name, String specialization, String department, double salary) {
        this.id = id;
        this.name = name;
        this.specialization = specialization;
        this.department = department;
        this.salary = salary;
    }

    public double getSalary() {
        return salary;
    }

    @Override
    public String toString() {
        return "Doctor{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", specialization='" + specialization + '\'' +
                ", department='" + department + '\'' +
                ", salary=" + salary +
                '}';
    }
}


import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Comparator;

public class Hospital {
    private ArrayList<Doctor> doctors;

    public Hospital() {
        doctors = new ArrayList<>();
    }

    public void sort() {
        doctors.sort(Comparator.comparing(Doctor::getSalary));
        for (Doctor doctor : doctors) {
            System.out.println(doctor);
        }
    }

    public void readData() {
        try (BufferedReader reader = new BufferedReader(new FileReader("D://Doctors.txt"))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] data = line.split(";");
                doctors.add(new Doctor(Integer.parseInt(data[0]), data[1], data[2], data[3], Double.parseDouble(data[4])));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

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