java 临终关怀项目
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 临终关怀项目相关的知识,希望对你有一定的参考价值。
// Import all Java utils.
import java.util.*;
// Improt java lang.
import java.lang.*;
//
// System for managing shifts.
//
public class ShiftSystem {
/**
* List of nurses.
*/
private NursesList nurseList;
/**
* Hospice this system is working on.
*/
private Hospice hospice;
/**
* Constructor.
*/
public ShiftSystem() {
// Instantiate nurse list.
this.nurseList = new NursesList();
// Create a new hospice instance.
this.hospice = new Hospice();
}
/**
* Starts counting the time.
*/
public void start() {
System.out.println("Starting time ... \033[32m OK \033[0m");
// Create a timer.
Timer timer = new Timer();
// Run timer.
timer.scheduleAtFixedRate(new TimerTask() {
//
// Advance a *minute* every second.
//
@Override
public void run() {
advanceMinute();
}
}, 100, 100);
// }, 1000, 1000);
}
/**
* Advances a minute in the system.
*/
public void advanceMinute() {
nurseList.advanceMinute();
}
/**
* Adds a nurse to the system.
*/
public void addNurse(Nurse nurse) {
this.nurseList.addNurse(nurse);
}
/**
* Returns how many nurses are there in the hospice.
*/
public int nursesInHospice() {
return nurseList.nursesInHospice();
}
/**
* Return the nurse that is currently in turn.
*/
public Nurse nurseInTurn() {
return nurseList.nurseInTurn();
}
/**
* Returns how much time is remaining for a
* nurse to start their turn.
*/
public int timeBeforeTurnIsOn(String nurseId) {
return nurseList.timeBeforeTurnIsOn(nurseId);
}
/**
* Occupies a nurse.
*
* @return { Nurse || null } nurse currently in turn
* or null if they're occupied already.
*/
public Nurse occupyNurse() {
return nurseList.occupyNurse();
}
/**
* Frees a nurse.
*/
public boolean freeNurse() {
return nurseList.freeNurse();
}
}
// Import all Java utils.
import java.util.*;
// Improt java lang.
import java.lang.*;
// List of nurses.
//
// The list of nurses is actually an array list of `Nurse` objects
// but we're wrapping it on this class in order to call useful
// methods on it like adding a nurse to the list and others.
public class NursesList {
/**
* List of nurses in turn.
*/
private ArrayList<Nurse> listOfNursesInTurn;
/**
* List of nurses in hospice.
*/
private ArrayList<Nurse> listOfNurses;
/**
* Elapsed time in minutes
*/
private int elapsedTime = 0;
/**
* Constructor.
*/
public NursesList() {
// By default we're going to allow 200 nurses at most.
int listLength = 200;
// Initialize list of nurses and nurses in turn.
this.listOfNursesInTurn = new ArrayList<Nurse>();
this.listOfNurses = new ArrayList<Nurse>();
}
/**
* Adds a nurse to the hospice and to the back of
* the list of nurses in turn.
*/
public boolean addNurse(Nurse nurse) {
this.listOfNursesInTurn.add(nurse);
this.listOfNurses.add(nurse);
// Show the state of nurse list.
printNurseList();
return true;
}
/**
* Advance one minute in time.
*/
public void advanceMinute() {
// Track time.
this.elapsedTime += 1;
// Go to the first nurse in the list and reduce their turn time.
if (listOfNursesInTurn.size() > 0) {
listOfNursesInTurn.get(0).advanceTime();
// If the nurse in the head finished their turn, then,
// rotate list.
if (listOfNursesInTurn.get(0).turnFinished()) {
listOfNursesInTurn.get(0).resetRemainingTime();
Collections.rotate(listOfNursesInTurn, listOfNursesInTurn.size() -1);
}
}
// If working day is off for this nurse, then take them
// out of the hospice.
for (int i = this.listOfNurses.size() - 1; i >= 0; i--)
if (listOfNurses.get(i).isWorkingDayOff(this.elapsedTime)) {
this.removeNurseFromTurn(listOfNurses.get(i).getId());
this.listOfNurses.remove(i);
}
// Print the state of nurses list.
printNurseList();
}
/**
* Removes a nurse from the list of nurses in turn.
*/
private void removeNurseFromTurn(String nurseId) {
for (int i = this.listOfNursesInTurn.size() - 1; i >= 0; i--)
if (listOfNursesInTurn.get(i).getId().equals(nurseId))
listOfNursesInTurn.remove(i);
}
/**
* Returns the number of nurses that are there in the hospice.
*/
public int nursesInHospice() {
return this.listOfNurses.size();
}
/**
* Returns the nurse that is currently in turn.
*/
public Nurse nurseInTurn() {
if (listOfNursesInTurn.size() > 0)
return listOfNursesInTurn.get(0);
return null;
}
/**
* Returns how many time is remaining for a nurse
* to start their turn.
*/
public int timeBeforeTurnIsOn(String nurseId) {
// Gets the specified nurse, find their index
// in the list of nurses in turn, and sum the minutes
// from all the nurses in front of them. That time
// should be the remaining time overall.
return 0;
}
/**
* Occupies the specified nurse.
*/
public Nurse occupyNurse() {
// TODO: Get nurse in turn and occupy them.
return null;
}
/**
* Frees a nurse
*/
public boolean freeNurse() {
// TODO: Get nurse in turn and free them.
return true;
}
/**
* Returns a nurse in the hospice given their ID.
*/
private Nurse getNurseById(String id) {
if (listOfNurses.size() <= 0)
return null;
for (int i = 0; i < listOfNurses.size(); i++)
if (listOfNurses.get(i).getId().equals(id))
return listOfNurses.get(i);
return null;
}
/**
* Prints the state of nurse list.
*/
private void printNurseList() {
System.out.println("");
System.out.println("\033[32m ------- Hospice -------- \033[0m");
System.out.println("Elapsed mins -> " + this.elapsedTime);
for (int i = 0; i < this.listOfNurses.size(); i++) {
System.out.println(i + " -> off at " + this.listOfNurses.get(i).getEndOfWorkingDay());
}
System.out.println("------- List of nurses in turn --------");
for (int i = 0; i < this.listOfNursesInTurn.size(); i++) {
System.out.println(listOfNursesInTurn.get(i).getId() + " -> " + listOfNursesInTurn.get(i).timeLeftInTurn() + " mins left.");
}
System.out.println("---------------------------");
System.out.println("");
}
}
// Import all Java utils.
import java.util.*;
// Improt java lang.
import java.lang.*;
// Nurse class.
//
// Hours are counted from 0 to 24.
//
public class Nurse {
/**
* Id
*/
private String id;
/**
* Whether this nurse is occupied with a pacient.
*/
private boolean occupied = false;
/**
* Time in which the working day of
* this nurse finishes.
*/
public int endOfWorkingDay;
/**
* Length of this nurse's turn in minutes.
* Default it to an hour.
*/
private int turnDuration;
/**
* Time this nurse has left in turn.
*/
private int remainingTimeInTurn;
/**
* Constructor.
*/
public Nurse() {
// Generate a random id for this nurse.
this.id = UUID.randomUUID().toString();
// Hours are counted from 0 to 24.
this.endOfWorkingDay = 1;
// Default turn duration to 10.
this.turnDuration = 10;
// Set remaining time in turn equal to `turnDuration`.
this.remainingTimeInTurn = this.turnDuration;
}
/**
* Returns nurse Id.
*/
public String getId() {
return this.id;
}
/**
* Get end of working day in minutes.
*/
public int getEndOfWorkingDay() {
return this.endOfWorkingDay * 60;
}
/**
* Occupies a nurse.
*/
public boolean occupy() {
// Return false if this nurse is already occupied.
if (this.occupied)
return false;
this.occupied = true;
return true;
}
/**
* Checks if this nuerse is occupied.
*/
public boolean occupied() {
return this.occupied;
}
/*
* Advances time on minute which means substracting one
* unit from `remainingTimeInTurn`.
*/
public int advanceTime() {
this.remainingTimeInTurn -= 1;
return this.remainingTimeInTurn;
}
/**
* Reset reimaining time in
*/
public void resetRemainingTime() {
this.remainingTimeInTurn = this.turnDuration;
}
/**
* How much time does this nurse have left in their turn.
*/
public int timeLeftInTurn() {
return this.remainingTimeInTurn;
}
/**
* Checks whether this nurse has finished thier turn,
* Which means that they have `remainingTimeInTurn` as 0.
*/
public boolean turnFinished() {
return this.remainingTimeInTurn <= 0;
}
/**
* Checks whether the working day of this nurse
* if off given a time in minutes.
*/
public boolean isWorkingDayOff(int time) {
return time >= this.endOfWorkingDay * 60;
}
/**
* Nurse string representation.
*/
public String toString() {
return "Nurse: " + this.id;
}
}
// Import all Java utils.
import java.util.*;
// Improt java lang.
import java.lang.*;
//
// Hospice class.
//
public class Hospice {
/**
* Name
*/
private String name;
/**
* PossibleNames to name the hospice randomly.
*/
private String[] possibleNames = {
"Hospice Mavericks",
"Yosemite's Hospice",
"Hospice Leopard",
"Tiger's Hospice"
};
/**
* Constructor.
*/
public Hospice() {
// Name
Random rand = new Random();
int min = 0;
int max = 2;
int i = rand.nextInt((max - min) + 1) + min;
name = possibleNames[i];
}
/**
* String representation.
*/
public String toString() {
return name;
}
}
// Import all Java utils.
import java.util.*;
// Improt java lang.
import java.lang.*;
// Excercises.
public class App {
// Main method
public static void main( String[] args ) {
// Instantiate the system.
ShiftSystem system = new ShiftSystem();
// Start counting the time.
system.start();
// Manual use case
// ---------------------------
// Create 3 nurses.
Nurse n1 = new Nurse();
Nurse n2 = new Nurse();
Nurse n3 = new Nurse();
// Add first nurse.
system.addNurse(n1);
// Do things on time.
Timer timer = new Timer();
// Add second nurse.
timer.schedule(new TimerTask() {
@Override
public void run() {
system.addNurse(n2);
}
}, 2000);
// Add third nurse.
timer.schedule(new TimerTask() {
@Override
public void run() {
system.addNurse(n3);
}
}, 4000);
// There are other methods that can be called.
//
// // Number of nurses in the hospice.
// system.nursesInHospice();
//
// // Current nurse in turn.
// system.nurseInTurn();
//
// // Time before a nurse is in turn.
// system.timeBeforeTurnIsOn(n1.getId());
//
// // Occupy a nurse.
// // Occupies the nurse currently in turn.
// system.occupyNurse();
//
// // Frees a nurse.
// system.freeNurse();
//
}
}
以上是关于java 临终关怀项目的主要内容,如果未能解决你的问题,请参考以下文章