Be a doctor in Java with this heartmonitor
With this application, you can play doctor and monitor the patient's Heartbeat. Watch out if the pulse get to low or to high, then you need to take action in realtime.
Press 'w' and enter to increase the pulse or press 's' and enter to decrease the pulse. Will your patient survive?
import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;
public class HeartMonitor {
private Monitor monitor;
private Thread thread;
public HeartMonitor(){
monitor = new Monitor();
thread = new Thread(monitor);
thread.start();
Scanner sc = new Scanner(System.in);
while(true){
try {
if(!isAlive()){
return;
}
String input = sc.nextLine();
switch(input){
case "w":
increasePulse();
break;
case "s":
decreasePulse();
break;
default:
continue;
}
} catch ( Exception e) {
sc.close();
return;
}
}
}
private boolean isAlive(){
if(monitor.getPulse().get() < 30 || monitor.getPulse().get() > 180){
thread.interrupt();
return false;
}
return true;
}
private void increasePulse(){
monitor.setPulse(5);
}
private void decreasePulse(){
monitor.setPulse(-5);
}
public class Monitor implements Runnable {
private Random random;
private volatile AtomicInteger pulse;
public Monitor() {
random = new Random();
pulse = new AtomicInteger(80);
}
private void changePulse(){
boolean decision = new Random().nextBoolean() ? true : false;
if(decision){
pulse.addAndGet(random.nextInt(5) + 2);
} else {
pulse.addAndGet(-random.nextInt(5) + 2);
}
}
public AtomicInteger getPulse(){
return pulse;
}
public void setPulse(int diff){
pulse.addAndGet(diff);
}
@Override
public void run(){
while(true){
try {
changePulse();
if(pulse.get() >= 30 && pulse.get() <= 50 || pulse.get() >= 160 && pulse.get() <= 180){
System.out.println("! DANGER ! Patients Pulse " + pulse);
} else if(pulse.get() < 30 || pulse.get() > 180){
System.out.println("Oh no... Patient is Death!");
return;
} else {
System.out.println("Patients Pulse " + pulse);
}
Thread.sleep(1000);
} catch (InterruptedException e){ }
}
}
}
public static void main(String[] args){
new HeartMonitor();
}
}
0
0
0.000
!LOL
!invest_vote
!BEER
lolztoken.com
Because they are so good at it.
Credit: reddit
@ozelot47, I sent you an $LOLZ on behalf of dotwin1981
(7/10)
ENTER @WIN.HIVE'S DAILY DRAW AND WIN HIVE!
!LUV
@dotwin1981, @ozelot47(1/10) sent LUV. | connect | community | HiveWiki | NFT | <>< daily
! help
(no space) to get help on Hive. Info@dotwin1981 denkt du hast ein Vote durch @investinthefutur verdient!
@dotwin1981 thinks you have earned a vote of @investinthefutur !