---
title: "Health care center class diagram to code"
subject: "Object Oriented Analysis and Design"
chapter: 4
key: "semester_6/object_oriented_analysis_and_design:health-care-class-diagram-code"
source: https://www.bctengineer.com/q/semester_6/object_oriented_analysis_and_design/health-care-class-diagram-code
site: BCT Engineering
---

# Health care center class diagram to code

Object Oriented Analysis and Design · Chapter 4: Implementation

- Asked in: 2072 Asoj, 10 marks
- Video: [How to draw class diagram for hospital management system?](https://www.youtube.com/watch?v=NoO7DHR08f0) — MADE_EASY_FOR_YOU
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/object_oriented_analysis_and_design/health-care-class-diagram-code
- Whole chapter: https://www.bctengineer.com/md/semester_6/object_oriented_analysis_and_design/chapter-4

## Question

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.

## 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.

## Answer

**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

```text
 +--------------------+ 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)

```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.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Where is the 'computer cross-checks for conflict' requirement implemented?
   A. In Prescription.hasConflict(), comparing the new medications with the patient's current medications
   B. In Scheduler.arrange()
   C. In Pharmacy.receive()
   D. In Appointment.cancel()
   Answer: A. hasConflict() compares each prescribed medication against the patient's current medications. (Key point: Conflict cross-check)

2. How is 'forwarded electronically to pharmacy or else printed' coded?
   A. Always call print()
   B. if (ph != null) rx.forwardTo(ph); else rx.print();
   C. Throw an exception
   D. Loop over all appointments
   Answer: B. The choice is a conditional: forward to the pharmacy if one is given, otherwise print. (Key point: Forward or print rule)

3. What is the role of the Scheduler class?
   A. It prescribes medication
   B. It is used by the patient to arrange and cancel appointments with a physician
   C. It stores drug dosage
   D. It prints prescriptions
   Answer: B. Scheduler.arrange creates an Appointment and adds it to the patient; cancel marks it cancelled and removes it. (Key point: Scheduler role)

4. Which set of attributes does Medication hold according to the answer?
   A. Name, dosage amount, number of doses, refills
   B. Date, patient, physician
   C. Appointment date and cancelled flag
   D. Pharmacy name and address
   Answer: A. The drug info specified by the physician is name, dosage amount, number of doses and refills. (Key point: Drug info attributes)

## Related in Object Oriented Analysis and Design

- [Video rental shop class diagram](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Avideo-rental-class-diagram) — Object Oriented Analysis and Design, chapter 2
- [Marina class diagram implementation code](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Amarina-class-diagram-implementation-code) — Object Oriented Analysis and Design, chapter 4
- [Development process and mapping design to code](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Adevelopment-process-map-design-code) — Object Oriented Analysis and Design, chapter 4
- [Map sequence diagram to pseudo code](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aorder-model-mapping-pseudocode) — Object Oriented Analysis and Design, chapter 4
- [XYZ Marina class diagram](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Amarina-class-diagram) — Object Oriented Analysis and Design, chapter 2
- [KUKL customer system design and code](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Akukl-use-case-class-diagram-code) — Object Oriented Analysis and Design, chapter 4

## Related in other subjects

- [Notes and summary: primary health care](https://www.bctengineer.com/question.md?key=semester_5%2Fcommunication_english%3Anotes-summary-primary-health-care) — Communication English, chapter 1
- [Minutes: Public Health Care Society first meeting](https://www.bctengineer.com/question.md?key=semester_5%2Fcommunication_english%3Aminutes-public-health-care-society-first-meeting) — Communication English, chapter 5
- [Patient information store and retrieve](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Apatient-file-program) — Object Oriented Programming, chapter 8
- [Peak demand of load center from load pattern](https://www.bctengineer.com/question.md?key=semester_8%2Ftransmission_and_distribution_system_design%3Apeak-demand-load-center-load-pattern) — Transmission and Distribution System Design, chapter 6, asked 2×
