Untitled

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

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 = getNextHospitalNumber();
    }

    // Copy constructor
    public Inpatient(Inpatient other) {
        this.name = other.name;
        this.age = other.age;
        this.hospitalNumber = getNextHospitalNumber();
        this.admissionDate = new Date(other.admissionDate.getTime()); // Assuming Date is mutable
        this.roomRent = other.roomRent;
    }

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

    private int getNextHospitalNumber() {
        return hospitalNumberCounter++;
    }

    public void inputRecord(String name, int age, Date admissionDate, double roomRent) {
        this.name = name;
        this.age = age;
        this.admissionDate = new Date(admissionDate.getTime());
        this.roomRent = roomRent;
    }

    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);
    }

    public boolean isAdmittedToday() {
        Date today = new Date();
        return admissionDate.getDate() == today.getDate() &&
                admissionDate.getMonth() == today.getMonth() &&
                admissionDate.getYear() == today.getYear();
    }
}

public class Main {
    public static void main(String[] args) {
        // Part a) Testing default, copy, and parameterized constructors
        Inpatient patient1 = new Inpatient();
        Inpatient patient2 = new Inpatient(patient1);
        Inpatient patient3 = new Inpatient("John Doe", 25, new Date(), 150.0);

        // Part b) Incrementing hospital number
        System.out.println("Hospital Number for patient1: " + patient1.getNextHospitalNumber());
        System.out.println("Hospital Number for patient2: " + patient2.getNextHospitalNumber());
        System.out.println("Hospital Number for patient3: " + patient3.getNextHospitalNumber());

        // Part c) Methods to input, display, and check admission date
        patient1.inputRecord("Alice Smith", 30, new Date(), 200.0);
        patient1.displayRecord();
        System.out.println("Admitted today? " + patient1.isAdmittedToday());

        // Part e) Creating an array of inpatient records
        Inpatient[] patientArray = new Inpatient[3];
        patientArray[0] = new Inpatient("Bob Johnson", 40, new Date(), 180.0);
        patientArray[1] = new Inpatient("Mary Williams", 35, new Date(), 220.0);
        patientArray[2] = new Inpatient("Eva Davis", 28, new Date(), 190.0);

        // Displaying all records
        for (Inpatient patient : patientArray) {
            patient.displayRecord();
            System.out.println("Admitted today? " + patient.isAdmittedToday());
        }

        // Part f) Checking for same information except hospital number
        Inpatient patient4 = new Inpatient("Alice Smith", 30, new Date(), 200.0);
        System.out.println("Patient1 and Patient4 have the same information except for hospital number: " +
                patient1.equals(patient4));

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