[go: up one dir, main page]

0% found this document useful (0 votes)
6 views22 pages

201 Assignment

The document contains Java code demonstrating the concepts of abstraction, encapsulation, inheritance, and polymorphism in a hospital management system. It includes classes for medical personnel, patients, and staff, showcasing their attributes and methods. Each section illustrates how these concepts are implemented through various classes like Doctor, Nurse, and Employee.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views22 pages

201 Assignment

The document contains Java code demonstrating the concepts of abstraction, encapsulation, inheritance, and polymorphism in a hospital management system. It includes classes for medical personnel, patients, and staff, showcasing their attributes and methods. Each section illustrates how these concepts are implemented through various classes like Doctor, Nurse, and Employee.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

22107810

Abstraction
package Abstraction;

import java.util.ArrayList;

import java.util.Scanner;

public class Main {

public static void main(String[] args){

ArrayList<MedicalPersonel> medics = new ArrayList<MedicalPersonel>();

Doctor doc = new Doctor("Fred","D101","heart surgeon");

Nurse nur =new Nurse(" Mary","N101","O.P.D");

Pharmascist phar = new Pharmascist(" Wilson","P101","Phz-123-456");

medics.add(doc);

medics.add(nur);

medics.add(phar);

for (MedicalPersonel mp : medics){

mp.displayDetails();

mp.performDuties();

System.out.println(mp.getSpecialization());

package Abstraction;
public class Doctor extends MedicalPersonel {

private String specialization;

public Doctor( String name,String id ,String specialization) {

super(name,id);

this.specialization = specialization;

public void performDuties(){

System.out.println("Doctor "+getName()+":" +"Diagnoses patients,prescribes


medication, and conducts surgeries.");

public String getSpecialization() {

return specialization;

package Abstraction;

public abstract class MedicalPersonel {

private String name;

private String id;

public MedicalPersonel(String name, String id) {

this.name = name;
this.id = id;

public String getName() {

return name;

public String getId() {

return id;

abstract void performDuties();

abstract String getSpecialization();

public void displayDetails(){

System.out.println("Name: " + name + " ID: " + id);

package Abstraction;

public class Nurse extends MedicalPersonel{

public String getDepartment() {

return department;

public void setDepartment(String department) {

this.department = department;
}

private String department;

public Nurse( String name,String id,String department) {

super(name,id);

this.department = department;

public void performDuties(){

System.out.println( "Nurse "+getName()+" : Provides patient care, administers


medications, and assists doctors.");

public String getSpecialization() {

return department;

package Abstraction;

public class Pharmascist extends MedicalPersonel{

private String pharmacyLicenseId;

public String getPharmacyLicenseId() {

return pharmacyLicenseId;

public Pharmascist(String name,String id,String pharmacyLicenseId) {

super(name,id);

this.pharmacyLicenseId = pharmacyLicenseId;

public void performDuties(){


System.out.println("Pharmacist "+getName()+": "+ "Dispenses medication and
advises patients on drug usage.");

public String getSpecialization(){

return "Pharmacist";

Encapsulation
package Encapsulation;

public class Main {

public static void main(String[] args) {

Patient newPatient = new Patient("P001","John Smith","Fever",45);

Doctor newDoctor = new Doctor("D101","Dr.Alice","General Medicine");

newPatient.updateDiagnosis("Flu");

newPatient.setAge(-54);

newPatient.updateDiagnosis(null);

package Encapsulation;

public class Patient {

private String patientId;

private String name;

private String diagnosis;

private int age;


public Patient(String patientId, String name, String diagnosis, int age) {

this.patientId = patientId;

this.name = name;

this.diagnosis = diagnosis;

this.age = age;

public String getPatientId() {

return patientId;

public void setPatientId(String patientId) {

this.patientId = patientId;

public String getName() {

return name;

public void setName(String name) {

this.name = name;

public String getDiagnosis() {

return diagnosis;

}
public void setDiagnosis(String diagnosis) {

if (diagnosis == null || diagnosis.isEmpty()) {

System.out.println(" Diagnosis cannot be empty");

} else {

this.diagnosis = diagnosis;

public int getAge() {

return age;

public void setAge(int age) {

if (age < 0) {

System.out.println("Invalid age");

} else {

this.age = age;

public void updateDiagnosis(String newDiagnosis) {

if (newDiagnosis == null || newDiagnosis.isEmpty()) {

System.out.println(" Diagnosis cannot be empty");

} else {

diagnosis = newDiagnosis;

System.out.println("Diagnosis updated to " + newDiagnosis);


}

package Encapsulation;

public class Doctor {

private String doctorId;

private String name;

private String specialization;

private int patientsTreated;

public Doctor(String doctorId, String name, String specialization) {

this.doctorId = doctorId;

this.name = name;

this.specialization = specialization;

public String getDoctorId() {

return doctorId;

public void setDoctorId(String doctorId) {

this.doctorId = doctorId;

public String getName() {

return name;

public void setName(String name) {

this.name = name;

public String getSpecialization() {


return specialization;

public void setSpecialization(String specialization) {

this.specialization = specialization;

public int getPatientsTreated() {

return patientsTreated;

public void setPatientsTreated(int patientsTreated) {

this.patientsTreated = patientsTreated;

public void treatPatient(){

patientsTreated++;

System.out.println( "Patient treated successfully. Total patients treated: " +


patientsTreated);

Inheritance

package Inheritance;

public class Main {

public static void main(String[] args) {

HospitalManagementSystem hosp = new HospitalManagementSystem();

Doctor Doctor1 = new Doctor("S001","Dr. Smith","Cardiology","Cardiology",15);

Doctor Doctor2 = new Doctor("S002","Dr. Lee","Neurology","Neurology",8);


Nurse Nurse = new Nurse("S003","Nurse Kelly","Emergency","Night",5);

hosp.registerStaff(Doctor1);

hosp.registerStaff(Doctor2);

hosp.registerStaff(Nurse);

hosp.displayAllStaff();

package Inheritance;

public class Nurse extends Staff{

private String shift;

private int patientAssigned;

public void setShift(String shift){

this.shift = shift;

public String getShift(){

return shift;

public void setPatientAssigned(int patientAssigned){

this.patientAssigned = patientAssigned;

public int getPatientAssigned(){

return patientAssigned;

public Nurse(String staffId,String name,String department, String shift, int


patientAssigned) {

super(staffId,name,department);
this.shift = shift;

this.patientAssigned = patientAssigned;

public void displayDetails() {

super.displayDetails();

System.out.println("Shift : " + shift);

System.out.println("Patients Assigned : " + patientAssigned);

public String toString(){

return super.toString() + "\nShift : " + shift + "\nPatients Assigned : " +


patientAssigned;

package Inheritance;

public class Staff {

private String staffId;

private String name;

private String department;

public void setStaffId(String staffId) {

this.staffId = staffId;

public String getStaffId() {

return staffId;

public void setName(String name) {

this.name = name;
}

public String getName() {

return name;

public void setDepartment(String department) {

this.department = department;

public String getDepartment() {

return department;

public Staff(String staffId, String name, String department) {

this.staffId = staffId;

this.name = name;

this.department = department;

public void displayDetails() {

System.out.println("Staff ID: " + staffId);

System.out.println("Name: " + name);

System.out.println("Department: " + department);

public String toString() {

return "Staff ID: " + staffId + ", Name: " + name + ", Department: " + department;

package Inheritance;
public class Doctor extends Staff{

private String specialization;

private int yearsOfExperience;

public void setSpecialization(String specialization) {

this.specialization = specialization;

public String getSpecialization() {

return specialization;

public void setYearsOfExperience(int yearsOfExperience) {

this.yearsOfExperience = yearsOfExperience;

public int getYearsOfExperience() {

return yearsOfExperience;

public Doctor( String staffId,String name, String department, String specialization, int
yearsOfExperience) {

super(staffId,name,department);

this.specialization = specialization;

this.yearsOfExperience = yearsOfExperience;

public void displayDetails() {

super.displayDetails();

System.out.println("Specialization : " + specialization);

System.out.println("Years of Experience : " + yearsOfExperience +" years");

public String toString() {

return super.toString() + ",\nSpecialization : " + specialization + ",\n Years of


Experience : " + yearsOfExperience +" years" ;
}

package Inheritance;

import java.util.ArrayList;

public class HospitalManagementSystem {

ArrayList<Staff> staffList;

public HospitalManagementSystem() {

staffList = new ArrayList<>();

public void registerStaff(Staff staff) {

staffList.add(staff);

System.out.println("Staff " + staff.getName() + " has been registered.");

public void displayAllStaff() {

for (Staff staff : staffList) {

System.out.println(staff);

Polymorphisim
package Polymorphisim;

public class Main {


public static void main (String[] args){

Employee emp = new Employee("John","E001",3000.0);

TemporaryEmployee temp = new TemporaryEmployee("Jane","E002",15.0, 160);

Manager mgr = new Manager("Alice","M001",5000.0, 2000.0);

Volunteer vln = new Volunteer("Mark","V001");

Staff staff = new Staff();

staff.addStaffMember(emp);

staff.addStaffMember(temp);

staff.addStaffMember(mgr);

staff.addStaffMember(vln);

staff.getTotalCost();

staff.displayStaff();

package Polymorphisim;

public class Employee extends StaffMember{

public double getMonthlySalary() {

return monthlySalary;

public void setMonthlySalary(double monthlySalary) {

this.monthlySalary = monthlySalary;

private double monthlySalary;


public Employee(String name,String id,double monthlySalary){

super(name,id);

this.monthlySalary = monthlySalary;

@Override

public double getPay() {

return monthlySalary;

@Override

public String toString() {

return super.toString() +" "+ "monthly Salary: " + getPay() ;

package Polymorphisim;

public class Manager extends Employee{

public double getBonus() {

return bonus;

public void setBonus(double bonus) {

this.bonus = bonus;

private double bonus;


public Manager(String name, String id,double monthlySalary, double bonus) {

super(name,id,monthlySalary);

this.bonus = bonus;

public double getSalary() {

return getMonthlySalary() + bonus;

@Override

public String toString() {

return super.toString() ;

package Polymorphisim;

import java.util.ArrayList;

public class Staff {

ArrayList<StaffMember> staff;

public Staff() {

staff = new ArrayList<>();

public void addStaffMember(StaffMember staffMember) {

staff.add(staffMember);

public void displayStaff(){


for( StaffMember S : staff){

System.out.println(S);

public void getTotalCost(){

double total = 0;

for (StaffMember staffMember : staff) {

total += staffMember.getPay();

System.out.println("total cost is : "+ total);

package Polymorphisim;

public abstract class StaffMember {

public String getName() {

return name;

public void setName(String name) {

this.name = name;

}
public String getId() {

return id;

public void setId(String id) {

this.id = id;

private String name;

private String id;

public StaffMember(String name,String id){

this.name = name;

this.id = id;

abstract public double getPay();

public String toString(){

return "NAME: "+ name +" ID: "+ id;

package Polymorphisim;

public class TemporaryEmployee extends StaffMember{

private double hourlyRate;

private int hoursWorked;

public double getHourlyRate() {


return hourlyRate;

public void setHourlyRate(double hourlyRate) {

this.hourlyRate = hourlyRate;

public int getHoursWorked() {

return hoursWorked;

public void setHoursWorked(int hoursWorked) {

this.hoursWorked = hoursWorked;

public TemporaryEmployee( String name, String id,double hourlyRate, int


hoursWorked ){

super(name, id);

this.hourlyRate = hourlyRate;

this.hoursWorked = hoursWorked;

public double getPay(){

return hourlyRate*hoursWorked;

public String toString(){

return super.toString()+ " " + "salary: " +getPay();

}
}

package Polymorphisim;

public class Volunteer extends StaffMember {

public Volunteer( String name,String id ) {

super(name, id);

public double getPay(){

return 0.0;

@Override

public String toString() {

return super.toString() +" " + " salary: " + getPay();

You might also like