Be a doctor in Java with this heartmonitor

avatar

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
5 comments
avatar

!LOL
!invest_vote
!BEER

0
0
0.000