Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
3.5 kB
1
Indexable
Never
import java.util.Date;

public class Inpatient {
    private static int hospitalNumberCounter = 1000;
    private String name;
    private int age;
    private int hospitalNumber;
    private Date admissionDate;
    private double roomRent;

    // Default constructor
    public Inpatient() {
        this.hospitalNumber = hospitalNumberCounter++;
    }

    // Copy constructor
    public Inpatient(Inpatient other) {
        this.name = other.name;
        this.age = other.age;
        this.hospitalNumber = hospitalNumberCounter++;
        this.admissionDate = other.admissionDate;
        this.roomRent = other.roomRent;
    }

    // Parameterized constructor
    public Inpatient(String name, int age, Date admissionDate, double roomRent) {
        this.name = name;
        this.age = age;
        this.hospitalNumber = hospitalNumberCounter++;
        this.admissionDate = admissionDate;
        this.roomRent = roomRent;
    }

    // Method to input a record
    public void inputRecord(String name, int age, Date admissionDate, double roomRent) {
        this.name = name;
        this.age = age;
        this.admissionDate = admissionDate;
        this.roomRent = roomRent;
    }

    // Method to display the record
    public void displayRecord() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Hospital Number: " + hospitalNumber);
        System.out.println("Admission Date: " + admissionDate);
        System.out.println("Room Rent: " + roomRent);
    }

    // Method to determine if the admission date is today's date
    public boolean isAdmissionDateToday() {
        Date today = new Date();
        return admissionDate.equals(today);
    }

    public static void main(String[] args) {
        // Testing default constructor
        Inpatient patient1 = new Inpatient();
        patient1.displayRecord();

        // Testing copy constructor
        Inpatient patient2 = new Inpatient(patient1);
        patient2.displayRecord();

        // Testing parameterized constructor
        Date admissionDate = new Date();
        Inpatient patient3 = new Inpatient("John Doe", 35, admissionDate, 200.0);
        patient3.displayRecord();

        // Testing inputRecord and isAdmissionDateToday methods
        Inpatient patient4 = new Inpatient();
        patient4.inputRecord("Alice Smith", 45, admissionDate, 250.0);
        patient4.displayRecord();
        System.out.println("Is admission date today? " + patient4.isAdmissionDateToday());

        // Creating an array of inpatient records
        Inpatient[] patients = new Inpatient[3];
        patients[0] = patient1;
        patients[1] = patient2;
        patients[2] = patient3;

        // Displaying all records
        for (Inpatient patient : patients) {
            patient.displayRecord();
        }

        // Checking if two patients have the same information except for hospital number
        if (patient1.equals(patient2)) {
            System.out.println("Patient 1 and Patient 2 have the same information.");
        } else {
            System.out.println("Patient 1 and Patient 2 have different information.");
        }

        // Determining the number of records admitted today
        int recordsAdmittedToday = 0;
        for (Inpatient patient : patients) {
            if (patient.isAdmissionDateToday()) {
                recordsAdmittedToday++;
            }
        }
        System.out.println("Number of records admitted today: " + recordsAdmittedToday);
    }
}
Leave a Comment