Skip to main content

Health care center class diagram to code

Short answer

Class diagram: Patient 1–* Appointment *–1 Physician, Patient 1 MedicalHistory, Patient * Medication; Scheduler arranges/cancels appointments. Medication holds drug info; Prescription checks conflicts using Patient’s current meds and history, then forwards to Pharmacy or prints. Code uses collections for associations and conflict detection methods.

Draw the class diagram and map the design into code for "Health Care Center" as following: Patient can arrange and cancel appointment with physician using scheduler. Physician decides to prescribe Medication for patient. Physician Specifies Drug Info: Medication name, Dosage Amount, Number-Doses and Refills. Computer Cross-Checks for Conflict between Medication and Current Medications/Medical History. Prescription forwarded Electronically to Pharmacy or Else Printed for Patient.

Classes found from the description: Patient, Physician, Scheduler, Appointment, Medication (holds the drug info: name, dosage amount, number of doses, refills), MedicalHistory, Prescription and Pharmacy. The "computer cross-check" is the hasConflict() method of Prescription, which checks each prescribed medication against the patient's current medications and medical history.

Class diagram

 +--------------------+ 1     * +-------------+ *     1 +--------------+
 |      Patient       |---------| Appointment |---------|  Physician   |
 |--------------------|         |-------------|         |--------------|
 | -name              |         | -date       |         | -name        |
 |--------------------|         | -cancelled  |         |--------------|
 | +getCurrentMeds()  |         |-------------|         | +prescribe() |
 | +getHistory()      |         | +cancel()   |         +--------------+
 +--------------------+         +-------------+                | 1
    | 1          | 1                   ^ creates/cancels       | writes
    | takes      | has                 |                       | *
    |            | 1            +-------------+        +-----------------+
    |    +-----------------+    |  Scheduler  |        |  Prescription   |
    |    | MedicalHistory  |    |-------------|        |-----------------|
    |    |-----------------|    | +arrange()  |        | -date           |
    |    | -conditions     |    | +cancel()   |        |-----------------|
    |    |-----------------|    +-------------+        | +hasConflict()  |
    |    | +conflictsWith()|    (Patient uses          | +forwardTo()    |
    |    +-----------------+     Scheduler)            | +print()        |
    | *                                                +-----------------+
    |                                                   | *       | *
 +-------------------+ 1..*       lists             |         | 0..1
 |    Medication     |----------------------------------+    +------------+
 |-------------------|                                       |  Pharmacy  |
 | -name             |                                       |------------|
 | -dosageAmount     |                                       | +receive() |
 | -numberOfDoses    |                                       +------------+
 | -refills          |
 +-------------------+

Code (Java)

import java.util.*;

class Medication {
    private String name; private double dosageAmount;
    private int numberDoses, refills;
    private Set<String> interactingDrugs = new HashSet<>();   // drug names
    private Set<String> contraindications = new HashSet<>();  // conditions/allergies
    public Medication(String n, double d, int doses, int refills) {
        name = n; dosageAmount = d; numberDoses = doses; this.refills = refills;
    }
    public String getName() { return name; }
    public void addInteraction(String drug) { interactingDrugs.add(drug); }
    public void addContraindication(String cond) { contraindications.add(cond); }
    public boolean interactsWith(Medication other) {
        return name.equalsIgnoreCase(other.name) || interactingDrugs.contains(other.name);
    }
    public boolean isContraindicatedFor(String condition) {
        return contraindications.contains(condition);
    }
}

class MedicalHistory {
    private List<String> conditions = new ArrayList<>();  // past illnesses, allergies
    public void addCondition(String c) { conditions.add(c); }
    public boolean conflictsWith(Medication m) {
        for (String c : conditions)
            if (m.isContraindicatedFor(c)) return true;
        return false;
    }
}

class Patient {
    private String name;
    private List<Medication> currentMedications = new ArrayList<>();
    private MedicalHistory history = new MedicalHistory();
    private List<Appointment> appointments = new ArrayList<>();
    public Patient(String name) { this.name = name; }
    public List<Medication> getCurrentMedications() { return currentMedications; }
    public MedicalHistory getHistory() { return history; }
    public void addAppointment(Appointment a) { appointments.add(a); }
    public void removeAppointment(Appointment a) { appointments.remove(a); }
}

class Appointment {
    private Date date; private Patient patient; private Physician physician;
    private boolean cancelled;
    public Appointment(Date d, Patient p, Physician ph) { date = d; patient = p; physician = ph; }
    public void cancel() { cancelled = true; }
}

class Scheduler {                     // used by the patient to arrange / cancel
    public Appointment arrange(Patient p, Physician ph, Date d) {
        Appointment a = new Appointment(d, p, ph);
        p.addAppointment(a);
        return a;
    }
    public void cancel(Patient p, Appointment a) { a.cancel(); p.removeAppointment(a); }
}

class Pharmacy {
    public void receive(Prescription rx) { System.out.println("Pharmacy got Rx"); }
}

class Prescription {
    private Date date = new Date();
    private Patient patient; private Physician physician;
    private List<Medication> medications = new ArrayList<>();
    public Prescription(Patient p, Physician ph) { patient = p; physician = ph; }
    public void add(Medication m) { medications.add(m); }

    public boolean hasConflict() {     // computer cross-check
        for (Medication m : medications) {
            for (Medication cur : patient.getCurrentMedications())   // current medications
                if (m.interactsWith(cur) || cur.interactsWith(m)) return true;
            if (patient.getHistory().conflictsWith(m)) return true; // medical history
        }
        return false;
    }
    public void forwardTo(Pharmacy ph) { ph.receive(this); }   // electronic
    public void print() { System.out.println("Prescription printed"); }
}

class Physician {
    private String name;
    public Physician(String n) { name = n; }
    public Prescription prescribe(Patient p, Medication m, Pharmacy ph) {
        Prescription rx = new Prescription(p, this);
        rx.add(m);
        if (rx.hasConflict()) { System.out.println("Conflict!"); return null; }
        if (ph != null) rx.forwardTo(ph); else rx.print();
        return rx;
    }
}

Associations were mapped to references and lists; the cross-check against current medications and medical history was mapped to hasConflict(); and the rule "forward electronically, or else print" was mapped to the if (ph != null) choice.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Object Oriented Analysis and Design chapter 4 questions →