Untitled

 avatar
unknown
plain_text
2 years ago
4.6 kB
6
Indexable
**address.java

public class Address {
    private String streetName;
    private String city;
    private String postalCode;
    private String state;

    public Address(String streetName, String city, String postalCode, String state) {
        this.streetName = streetName;
        this.city = city;
        this.postalCode = postalCode;
        this.state = state;
    }

    public String getFullAddress() {
        return streetName + ", " + city + ", " + postalCode + ", " + state;
    }
}


**HealthInfo.java

public class HealthInfo {
    private double height; // in centimeters
    private double weight; // in kilograms
    private double bmi;

    public HealthInfo(double height, double weight) {
        this.height = height;
        this.weight = weight;
        this.calculateBMI();
    }

    private void calculateBMI() {
        // Formula to calculate BMI: BMI = weight (kg) / (height (m))^2
        double heightInMeters = height / 100; // Convert height to meters
        this.bmi = weight / (heightInMeters * heightInMeters);
    }

    public double getHeight() {
        return height;
    }

    public double getWeight() {
        return weight;
    }

    public double getBMI() {
        return bmi;
    }
}


**Person.java

public class Person {
    private String firstName;
    private String lastName;
    private String gender;
    private Address address;

    public Person(String firstName, String lastName, String gender, Address address) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.gender = gender;
        this.address = address;
    }

    public String getFullName() {
        return firstName + " " + lastName;
    }

    public String getGender() {
        return gender;
    }

    public String getAddress() {
        return address.getFullAddress();
    }
}


**patient.java

public class Patient extends Person {
    private String patientId;
    private HealthInfo healthInfo;

    public Patient(String patientId, String firstName, String lastName, String gender, Address address,
                   double height, double weight) {
        super(firstName, lastName, gender, address);
        this.patientId = patientId;
        this.healthInfo = new HealthInfo(height, weight);
        System.out.println("Patient: " + getFullName() + " has been added");
    }

    public String getPatientId() {
        return patientId;
    }

    public double getHeight() {
        return healthInfo.getHeight();
    }

    public double getWeight() {
        return healthInfo.getWeight();
    }

    public double getBMI() {
        return healthInfo.getBMI();
    }
}


**PatientSystem.java

import java.util.ArrayList;

public class PatientSystem {
    public static void main(String[] args) {
        // Create a dynamic list to hold Patient objects
        ArrayList<Patient> patientList = new ArrayList<>();

        // Initialize THREE patients and addresses
        Address address1 = new Address("123 Main St", "City1", "12345", "State1");
        Address address2 = new Address("456 Oak St", "City2", "67890", "State2");
        Address address3 = new Address("789 Pine St", "City3", "98765", "State3");

        Patient patient1 = new Patient("P001", "John", "Doe", "Male", address1, 175.0, 70.0);
        Patient patient2 = new Patient("P002", "Jane", "Smith", "Female", address2, 160.0, 55.0);
        Patient patient3 = new Patient("P003", "Bob", "Johnson", "Male", address3, 180.0, 85.0);

        // Save patients into the dynamic list
        patientList.add(patient1);
        patientList.add(patient2);
        patientList.add(patient3);

        // Display the list of patients
        System.out.println("\nList of Patients:");
        displayRecord(patientList);

        // Remove the 2nd patient in the list
        if (patientList.size() >= 2) {
            patientList.remove(1);
        }

        // Display the updated list of patients
        System.out.println("\nList of Patients after removal:");
        displayRecord(patientList);
    }

    // Function to display the list of patients
    private static void displayRecord(ArrayList<Patient> patients) {
        for (Patient patient : patients) {
            System.out.println("Patient ID: " + patient.getPatientId());
            System.out.println("Full Name: " + patient.getFullName());
            System.out.println("Gender: " + patient.getGender());
            System.out.println("Address: " + patient.getAddress());
            System.out.println("Height: " + patient.getHeight() + " cm");
            System.out.println("Weight: " + patient.get
Editor is loading...
Leave a Comment