Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
State Pattern - Example of Treatment of pateint with different states…
… ie - stable, unstable and dischargable state pateint
- Loading branch information
Showing
4 changed files
with
244 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/bin |
79 changes: 79 additions & 0 deletions
79
...ternMedicalTreatmentExample/src/com/premaseem/statePattern/ClientForMedicalTreatment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.premaseem.statePattern; | ||
|
||
import java.util.Scanner; | ||
|
||
public class ClientForMedicalTreatment { | ||
|
||
public static void main(String[] args) { | ||
Scanner scan = new Scanner(System.in); | ||
int repeatRunFlag = 1; | ||
|
||
System.out.println("This is State Pattern example where in treatment would be dependent on the state of the patient ) "); | ||
TreatmentContext context = new TreatmentContext(); | ||
|
||
// applyOxygen | ||
|
||
while (repeatRunFlag == 1) { | ||
// Uncomment below line to take input of patient type | ||
// takePatientTypeInput(scan, context); | ||
|
||
context.setState(context.unStablePatientState); | ||
System.out.println("What treatment should be given to this patient with current state " + context.getState().getClass().getSimpleName()); | ||
System.out.println(" Press 1 for admiting in ICU"); | ||
System.out.println(" Press 2 for prescribing general Medicine "); | ||
System.out.println(" Press 3 for supply of Oxygen"); | ||
System.out.println(" Press 4 for supply of Normal food"); | ||
System.out.println(" Press 5 for recommending a walk "); | ||
|
||
int treatmentType = scan.nextInt(); | ||
|
||
switch (treatmentType) { | ||
case 1: | ||
context.admitInICU(); | ||
break; | ||
case 2: | ||
context.prescribeGeneralMedicine(); | ||
break; | ||
case 3: | ||
context.supplyOxygen(); | ||
break; | ||
case 4: | ||
context.supplyNormalFood(); | ||
break; | ||
|
||
case 5: | ||
context.recommendWalking(); | ||
break; | ||
} | ||
|
||
System.out.println("Current state of Pateint is " + context.getState().getClass().getSimpleName()); | ||
System.out.println("Press 1 for further treamemt and 0 for EXIT .... "); | ||
try { | ||
repeatRunFlag = scan.nextInt(); | ||
} catch (Exception e) { | ||
repeatRunFlag = 0; | ||
} | ||
|
||
} | ||
} | ||
|
||
private static void takePatientTypeInput(Scanner scan, TreatmentContext context) { | ||
System.out.println("Please choose state of your patient before starting treatment "); | ||
System.out.println(" Press 1 for unstalbe state patient"); | ||
System.out.println(" Press 2 for stable state patient "); | ||
System.out.println(" Press 3 for dischargable state patient "); | ||
int entType = scan.nextInt(); | ||
|
||
switch (entType) { | ||
case 1: | ||
context.setState(context.stablePatientState); | ||
break; | ||
case 2: | ||
context.setState(context.unStablePatientState); | ||
break; | ||
case 3: | ||
context.setState(context.dischargePatientState); | ||
break; | ||
} | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
StatePatternMedicalTreatmentExample/src/com/premaseem/statePattern/PatientHealthState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package com.premaseem.statePattern; | ||
|
||
public interface PatientHealthState { | ||
|
||
void supplyOxygen(); | ||
|
||
void supplyOralFood(); | ||
|
||
void recommendWalking(); | ||
|
||
void prescribeGeneralMedicine(); | ||
|
||
void admitInICU(); | ||
} | ||
|
||
class StablePatientState implements PatientHealthState { | ||
|
||
TreatmentContext context; | ||
|
||
public StablePatientState(TreatmentContext context) { | ||
this.context = context; | ||
} | ||
|
||
@Override | ||
public void supplyOxygen() { | ||
System.out.println("Stable pateint do not need oxygen "); | ||
} | ||
|
||
@Override | ||
public void supplyOralFood() { | ||
System.out.println(" Required at this stage and lets pateint to become healthy and get discharged "); | ||
context.setState(context.stablePatientState); | ||
} | ||
|
||
@Override | ||
public void recommendWalking() { | ||
System.out.println(" Do not walk while pateient get discharge "); | ||
} | ||
|
||
@Override | ||
public void prescribeGeneralMedicine() { | ||
System.out.println(" will help in improvement "); | ||
} | ||
|
||
@Override | ||
public void admitInICU() { | ||
System.out.println(" No need of ICU "); | ||
context.setState(context.unStablePatientState); | ||
} | ||
} | ||
|
||
class UnStablePatientState implements PatientHealthState { | ||
|
||
TreatmentContext context; | ||
|
||
public UnStablePatientState(TreatmentContext context) { | ||
this.context = context; | ||
} | ||
|
||
@Override | ||
public void supplyOxygen() { | ||
System.out.println("UnStable pateint do need oxygen "); | ||
context.setState(context.stablePatientState); | ||
} | ||
|
||
@Override | ||
public void supplyOralFood() { | ||
System.out.println(" supply blood and liquids - no oral food please "); | ||
context.setState(context.stablePatientState); | ||
} | ||
|
||
@Override | ||
public void recommendWalking() { | ||
System.out.println(" Do not dare "); | ||
} | ||
|
||
@Override | ||
public void prescribeGeneralMedicine() { | ||
System.out.println(" needs advanded medicine other wise patient will become unstable "); | ||
} | ||
|
||
@Override | ||
public void admitInICU() { | ||
System.out.println(" Correct step "); | ||
} | ||
} | ||
|
||
class DischargePatientState implements PatientHealthState { | ||
|
||
TreatmentContext context; | ||
|
||
public DischargePatientState(TreatmentContext context) { | ||
this.context = context; | ||
} | ||
|
||
@Override | ||
public void supplyOxygen() { | ||
System.out.println("Will make patient unstable ... "); | ||
context.setState(context.unStablePatientState); | ||
} | ||
|
||
@Override | ||
public void supplyOralFood() { | ||
System.out.println(" enjoy "); | ||
// context.setState(context.); | ||
} | ||
|
||
@Override | ||
public void recommendWalking() { | ||
System.out.println(" Enjoy "); | ||
} | ||
|
||
@Override | ||
public void prescribeGeneralMedicine() { | ||
System.out.println(" no need "); | ||
} | ||
|
||
@Override | ||
public void admitInICU() { | ||
System.out.println(" no need"); | ||
context.setState(context.unStablePatientState); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
StatePatternMedicalTreatmentExample/src/com/premaseem/statePattern/TreatmentContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.premaseem.statePattern; | ||
|
||
public class TreatmentContext { | ||
|
||
PatientHealthState state; | ||
StablePatientState stablePatientState = new StablePatientState(this); | ||
UnStablePatientState unStablePatientState = new UnStablePatientState(this); | ||
DischargePatientState dischargePatientState = new DischargePatientState(this); | ||
|
||
|
||
public PatientHealthState getState() { | ||
return state; | ||
} | ||
|
||
public void setState(PatientHealthState state) { | ||
this.state = state; | ||
} | ||
|
||
|
||
void prescribeGeneralMedicine(){ | ||
state.prescribeGeneralMedicine(); | ||
} | ||
|
||
void admitInICU(){ | ||
state.admitInICU(); | ||
} | ||
|
||
|
||
void supplyOxygen(){ | ||
state.supplyOxygen(); | ||
} | ||
|
||
void supplyNormalFood(){ | ||
state.supplyOralFood(); | ||
} | ||
|
||
void recommendWalking(){ | ||
state.recommendWalking(); | ||
} | ||
|
||
} |