Question One
A hospital wants to implement a patient appointment management system. Each patient has a patient ID, name, and phone number, and each appointment includes an appointment date, the doctor’s name, and the patient ID. You are required to create an abstract Person class and extend it to form a Patient class, implement an Appointment class that receives the database connection through dependency injection, write a method to book an appointment and store it in the database, and demonstrate polymorphism by displaying patient details differently for an administrator view and a receptionist view.
(20 Marks)
Answer (Click to show)
To implement a Patient Appointment Management System, the following classes are required: i. Person (Abstract Class)
- Represents a general person.
- Contains common attributes like
name.- Has an abstract method
displayDetails()to support polymorphism.ii. Patient (Child Class)
- Extends the
Personclass.- Contains
patientIDandphone.- Implements the abstract method differently depending on system view.
iii. Appointment Class
Handles appointment booking.
Receives a database connection through Dependency Injection.
Stores appointment details in the database.
See the Code Below:
// 1. ABSTRACT PERSON CLASS(Person.php)
<?php
abstract class Person {
protected $name; // Store person's name
// Constructor to initialize name
public function __construct($name){
$this->name = $name;
}
// Abstract method to demonstrate polymorphism
abstract public function displayDetails();
}
// 2. PATIENT CLASS (EXTENDS PERSON)(Patient.php)
class Patient extends Person {
private $patientID;
private $phone;
// Constructor initializes patient details
public function __construct($patientID, $name, $phone){
parent::__construct($name); // Call parent constructor
$this->patientID = $patientID;
$this->phone = $phone;
}
// Admin view (shows all details)
public function displayAdminView(){
echo "Admin View: <br>";
echo "Patient ID: ".$this->patientID."<br>";
echo "Name: ".$this->name."<br>";
echo "Phone: ".$this->phone."<br>";
}
// Receptionist view (limited details)
public function displayReceptionView(){
echo "Reception View: <br>";
echo "Name: ".$this->name."<br>";
echo "Phone: ".$this->phone."<br>";
}
// Implementation of abstract method
public function displayDetails(){
$this->displayReceptionView();
}
// Getter for patient ID (used during appointment booking)
public function getPatientID(){
return $this->patientID;
}
}
// 3. APPOINTMENT CLASS(Appointment.php)
class Appointment {
private $pdo; // Database connection (Dependency Injection)
// Constructor receives database connection
public function __construct($pdo){
$this->pdo = $pdo;
}
// Method to book an appointment
public function bookAppointment($date, $doctor, $patientID){
// SQL query using prepared statement
$sql = "INSERT INTO appointments (appointment_date, doctor_name, patient_id)
VALUES (:date, :doctor, :patientID)";
// Prepare statement
$stmt = $this->pdo->prepare($sql);
// Bind parameters
$stmt->bindParam(":date", $date);
$stmt->bindParam(":doctor", $doctor);
$stmt->bindParam(":patientID", $patientID);
// Execute query
if($stmt->execute()){
echo "Appointment booked successfully.<br>";
} else {
echo "Failed to book appointment.<br>";
}
}
}
// 4. DATABASE CLASS(Database.php)
class Database {
private $pdo;
private $dsn = "mysql:host=localhost;dbname=hospital";
private $dbusername = "root";
private $dbpassword = ""
// Constructor creates database connection
public function __construct(){
try{
$this->pdo = new PDO($dsn,$dbusername,$dbpassword);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
echo "Connection failed: ".$e->getMessage();
}
}
// Method to return the PDO object
public function getConnection(){
return $this->pdo;
}
}
// 5.DEMONSTRATION(index.php)
//Create a Database Object
$db = new Database();
//Get a PDO connection
$pdo = $db->getConnection();
// Create patient object
$patient = new Patient(101, "John Doe", "0712345678");
// Demonstrate polymorphism
$patient->displayAdminView();
echo "<br>";
$patient->displayReceptionView();
// Create appointment object (Dependency Injection)
$appointment = new Appointment($pdo);
// Book appointment
$appointment->bookAppointment("2026-03-10", "Dr. Smith", $patient->getPatientID());
?>Question Two
Amazon College is set to launch a web system designed to automate its business processes, developed using PHP and MySQL. This system features a database server at IP Address 10.10.10.1, where the “amazon_db” database is hosted, alongside a code server at IP Address 10.10.10.2. After the system has been deployed it was observed that the performance was poor as the system loads very slowly especially when the information are retrieved from the server. To address these issues the implementation of AJAX technology is proposed, enabling asynchronous data loading to improve response times. Assume the goal is to retrieve information from the “student” table, including fields such as First Name, Last Name, Program, Gender, and Registration Number. Write a script hosted at the source code server to show how you connect to the database, load data from the server using AJAX and display to the client in a table format. Use jQuery methods to send the AJAX requests. For database access, the credentials are: username “amazon_college” and password “amazon_123”.
(15 Marks)
Answer(Click to Show)
i. System Summary:
- Database Server:
10.10.10.1- Code Server:
10.10.10.2- Database Name:
amazon_db- Table:
studentii. Fields Retrieved:
- First Name
- Last Name
- Program
- Gender
- Registration Number
iii. The implementation involves three components:
- HTML interface
- jQuery AJAX request
- PHP script to fetch data from the database
Code : i. HTML Interface with AJAX (index.html)
- This page runs on the source code server (10.10.10.2) and requests student data asynchronously.
<!DOCTYPE html>
<html>
<head>
<title>Amazon College Students</title>
<!-- Load jQuery library -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function(){
// When button is clicked, AJAX request is sent
$("#loadData").click(function(){
$.ajax({
url: "fetch_students.php", // PHP script to fetch data
type: "GET", // HTTP request type
success: function(response){
// Display returned data inside the table body
$("#studentTable tbody").html(response);
},
error: function(){
alert("Error loading data from server");
}
});
});
});
</script>
</head>
<body>
<h2>Amazon College Student Records</h2>
<!-- Button to trigger AJAX request -->
<button id="loadData">Load Student Data</button>
<br><br>
<!-- Table to display results -->
<table border="1" id="studentTable">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Program</th>
<th>Gender</th>
<th>Registration Number</th>
</tr>
</thead>
<tbody>
<!-- AJAX will load data here -->
</tbody>
</table>
</body>
</html>ii. PHP Script to Fetch Data (fetch_students.php)
- This script runs on the code server (10.10.10.2) and connects to the database server
10.10.10.1.
<?php
// 1. DATABASE CONNECTION
// Database server details
$host = "10.10.10.1";
$dbname = "amazon_db";
$username = "amazon_college";
$password = "amazon_123";
try{
// Create PDO connection
$pdo = new PDO("mysql:host=$host;dbname=$dbname",$username,$password);
// Enable error reporting
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e){
// Display error if connection fails
echo "Database Connection Failed: " . $e->getMessage();
}
//2. FETCH DATA FROM STUDENT TABLE
// SQL query to retrieve students
$sql = "SELECT first_name, last_name, program, gender, reg_number FROM student";
// Prepare query
$stmt = $pdo->prepare($sql);
// Execute query
$stmt->execute();
// Fetch all records
$students = $stmt->fetchAll(PDO::FETCH_ASSOC);
//3. DISPLAY DATA AS TABLE ROWS
// Loop through results and output table rows
foreach($students as $row){
echo "<tr>";
echo "<td>".$row['first_name']."</td>";
echo "<td>".$row['last_name']."</td>";
echo "<td>".$row['program']."</td>";
echo "<td>".$row['gender']."</td>";
echo "<td>".$row['reg_number']."</td>";
echo "</tr>";
}
?>