Guest User

Untitled

a guest
Oct 17th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.20 KB | None | 0 0
  1. public class QuarantineTest {
  2.  
  3. private Quarantine quarantine;
  4.  
  5. @Before
  6. public void setUp() {
  7. // The responsibility of the Quarantine object is to simulate diseases on a group of patients.
  8. // It is initialized with a list of patients' health status, separated by a comma.
  9. // Each health status is described by one or more characters
  10. // (in the test below, we will always have only one disease / patient)
  11. // The characters mean:
  12. // H : Healthy
  13. // F : Fever
  14. // D : Diabetes
  15. // T : Tuberculosis
  16. quarantine = new Quarantine("F,H,D,D,D,H,T");
  17.  
  18. // Quarantine provides medicines to the patients, but can not target a specific group of patient.
  19. // The same medicines are always given to all the patients.
  20.  
  21. // Then Quarantine can provide a report with this format:
  22. // "F:1 H:2 D:0 T:1 X:3"
  23. // Report give the number of patients that have the given disease.
  24. // X means Dead
  25. }
  26.  
  27. @Test
  28. public void beforeTreatment() throws Exception {
  29. assertEquals("F:1 H:2 D:3 T:1 X:0", quarantine.report());
  30. }
  31.  
  32. // people died in the Diabetes
  33. @Test
  34. public void noTreatment() throws Exception {
  35. quarantine.wait40Days();
  36. // diabetics die without insulin
  37. assertEquals("F:1 H:2 D:0 T:1 X:3", quarantine.report());
  38. }
  39.  
  40. // feaver is cured
  41. // people died in the Diabetes
  42. @Test
  43. public void aspirin() throws Exception {
  44. quarantine.aspirin();
  45. quarantine.wait40Days();
  46. // aspirin cure Fever
  47. assertEquals("F:0 H:3 D:0 T:1 X:3", quarantine.report());
  48. }
  49.  
  50. @Test
  51. public void antibiotic() throws Exception {
  52. quarantine.antibiotic();
  53. quarantine.wait40Days();
  54. // antibiotic cure Tuberculosis
  55. // but healthy people catch Fever if mixed with insulin.
  56. assertEquals("F:1 H:3 D:0 T:0 X:3", quarantine.report());
  57. }
  58.  
  59. @Test
  60. public void insulin() throws Exception {
  61. quarantine.insulin();
  62. quarantine.wait40Days();
  63. // insulin prevent diabetic subject from dying, does not cure Diabetes,
  64. assertEquals("F:1 H:2 D:3 T:1 X:0", quarantine.report());
  65. }
  66.  
  67. @Test
  68. public void antibioticPlusInsulin() throws Exception {
  69. quarantine.antibiotic();
  70. quarantine.insulin();
  71. quarantine.wait40Days();
  72. // if insulin is mixed with antibiotic, healthy people catch Fever
  73. assertEquals("F:3 H:1 D:3 T:0 X:0", quarantine.report());
  74. }
  75.  
  76. @Test
  77. public void paracetamol() throws Exception {
  78. quarantine.paracetamol();
  79. quarantine.wait40Days();
  80. // paracetamol heals fever
  81. assertEquals("F:0 H:3 D:0 T:1 X:3", quarantine.report());
  82. }
  83.  
  84. @Test
  85. public void paracetamolAndAspirin() throws Exception {
  86. quarantine.paracetamol();
  87. quarantine.aspirin();
  88. quarantine.wait40Days();
  89. // paracetamol kills subject if mixed with aspirin
  90. assertEquals("F:0 H:0 D:0 T:0 X:7", quarantine.report());
  91. }
  92.  
  93. }
  94.  
  95. package quarantine;
  96.  
  97. import java.util.ArrayList;
  98. import java.util.LinkedHashMap;
  99.  
  100. public class Quarantine {
  101.  
  102. LinkedHashMap<PatientType,Integer> patientInfoMap = new LinkedHashMap<>();
  103. private ArrayList<Medicines> appliedTreatementList = new ArrayList<>();
  104. ArrayList<ITreatment> treatementRepository = new ArrayList<>();
  105. static int treatedTBPatientCount=0;
  106. static boolean isInsulineInjected = false;
  107.  
  108. public Quarantine(String subjects) {
  109. String[] patientArr = subjects.split(",");
  110. for(String disease :patientArr){
  111. int patientCount = patientInfoMap.get(PatientType.getType(disease)) != null ? patientInfoMap.get(PatientType.getType(disease)):0;
  112. patientInfoMap.put(PatientType.getType(disease),++patientCount);
  113. }
  114. patientInfoMap.put(PatientType.DEAD,0);
  115. }
  116.  
  117. public String report() {
  118. try {
  119. final String[] result = {""};
  120. patientInfoMap.forEach((k, v) -> result[0] += PatientType.getValue(k) + ":" + v.toString() + " ");
  121. return result[0].trim();
  122. } catch (Exception e) {
  123. e.printStackTrace();
  124. }
  125. return null;
  126. }
  127.  
  128. public void wait40Days() {
  129. for(ITreatment treatment : treatementRepository){
  130. treatment.treat(patientInfoMap,appliedTreatementList);
  131. }
  132. if(!isInsulineInjected){
  133. dieDiabticPatient();
  134. }
  135. }
  136.  
  137. private void dieDiabticPatient() {
  138. patientInfoMap.put(PatientType.DEAD, patientInfoMap.get(PatientType.DIABATIC));
  139. patientInfoMap.put(PatientType.DIABATIC, 0);
  140.  
  141. }
  142.  
  143. public void aspirin() {
  144. appliedTreatementList.add(Medicines.ASPIRIN);
  145. treatementRepository.add(new TreatmentAspirin());
  146.  
  147. }
  148.  
  149. public void antibiotic() {
  150. appliedTreatementList.add(Medicines.ANTIBIOTIC);
  151. treatementRepository.add(new TreatmentAntibiotic());
  152.  
  153. }
  154.  
  155. public void insulin() {
  156. appliedTreatementList.add(Medicines.INSULIN);
  157. treatementRepository.add(new TreatmentInsulin());
  158.  
  159. }
  160.  
  161. public void paracetamol() {
  162. appliedTreatementList.add(Medicines.PARACETOL);
  163. treatementRepository.add(new TreatmentParacetamol());
  164.  
  165. }
  166.  
  167. }
  168.  
  169. package quarantine;
  170.  
  171. import java.util.ArrayList;
  172. import java.util.LinkedHashMap;
  173.  
  174. public class TreatmentAntibiotic implements ITreatment{
  175.  
  176. @Override
  177. public void treat(LinkedHashMap<PatientType, Integer> patientInfoMap, ArrayList<Medicines> appliedTreatementList) {
  178. if(appliedTreatementList.contains(Medicines.ANTIBIOTIC)){
  179. Quarantine.isInsulineInjected = false;
  180. LinkedHashMap<PatientType, Integer> map = patientInfoMap;
  181. Quarantine.treatedTBPatientCount = map.get(PatientType.TUBERCLOSIS);
  182. map.put(PatientType.HEALTHY, map.get(PatientType.HEALTHY) + map.get(PatientType.TUBERCLOSIS));
  183. map.put(PatientType.TUBERCLOSIS, 0);
  184. }
  185.  
  186.  
  187.  
  188. }
  189.  
  190. }
  191.  
  192. package quarantine;
  193.  
  194. import java.util.ArrayList;
  195. import java.util.LinkedHashMap;
  196.  
  197. interface ITreatment {
  198.  
  199.  
  200. abstract public void treat(LinkedHashMap<PatientType, Integer> patientInfoMap, ArrayList<Medicines> appliedTreatementList);
  201. }
  202.  
  203. package quarantine;
  204.  
  205. enum Medicines {
  206. INSULIN,
  207. ANTIBIOTIC,
  208. PARACETOL,
  209. ASPIRIN
  210. }
  211.  
  212. package quarantine;
  213.  
  214. public enum PatientType {
  215. DEAD("X"),
  216. DIABATIC("D"),
  217. FEVER("F"),
  218. TUBERCLOSIS("T"),
  219. HEALTHY("H");
  220. private final String disease;
  221.  
  222. PatientType(String disease) {
  223. this.disease = disease;
  224. }
  225. public static String getValue(PatientType patientType)
  226. {
  227. for (PatientType type : PatientType.values())
  228. if (type.equals(patientType))
  229. return type.disease;
  230. return null;
  231. }
  232. public static PatientType getType(String diseaseType)
  233. {
  234. for (PatientType type : PatientType.values())
  235. if (type.disease.equals(diseaseType))
  236. return type;
  237. return null;
  238. }
  239.  
  240.  
  241. }
  242.  
  243. package quarantine;
  244.  
  245. import java.util.ArrayList;
  246. import java.util.LinkedHashMap;
  247.  
  248. public class TreatmentAntibiotic implements ITreatment{
  249.  
  250. @Override
  251. public void treat(LinkedHashMap<PatientType, Integer> patientInfoMap, ArrayList<Medicines> appliedTreatementList) {
  252. if(appliedTreatementList.contains(Medicines.ANTIBIOTIC)){
  253. Quarantine.isInsulineInjected = false;
  254. LinkedHashMap<PatientType, Integer> map = patientInfoMap;
  255. Quarantine.treatedTBPatientCount = map.get(PatientType.TUBERCLOSIS);
  256. map.put(PatientType.HEALTHY, map.get(PatientType.HEALTHY) + map.get(PatientType.TUBERCLOSIS));
  257. map.put(PatientType.TUBERCLOSIS, 0);
  258. }
  259.  
  260.  
  261.  
  262. }
  263.  
  264. }
  265.  
  266. package quarantine;
  267.  
  268. import java.util.ArrayList;
  269. import java.util.LinkedHashMap;
  270.  
  271. public class TreatmentAspirin implements ITreatment{
  272.  
  273.  
  274. @Override
  275. public void treat(LinkedHashMap<PatientType, Integer> patientInfoMap, ArrayList<Medicines> appliedTreatementList) {
  276.  
  277. if(appliedTreatementList.contains(Medicines.ASPIRIN)){
  278. LinkedHashMap<PatientType, Integer> map = patientInfoMap;
  279.  
  280. if(appliedTreatementList.contains(Medicines.PARACETOL)){
  281. Quarantine.isInsulineInjected = true; //Because They are Dead
  282. map.put(PatientType.DEAD, map.get(PatientType.DEAD) + map.get(PatientType.FEVER) + map.get(PatientType.HEALTHY) + map.get(PatientType.DIABATIC) + map.get(PatientType.TUBERCLOSIS));
  283. map.put(PatientType.HEALTHY, 0);
  284. map.put(PatientType.DIABATIC, 0);
  285. map.put(PatientType.TUBERCLOSIS, 0);
  286. }else{
  287. Quarantine.isInsulineInjected = false;
  288. map.put(PatientType.HEALTHY, map.get(PatientType.HEALTHY) + map.get(PatientType.FEVER));
  289. map.put(PatientType.FEVER, 0);
  290. }
  291.  
  292. }
  293.  
  294. }
  295.  
  296. }
  297.  
  298. package quarantine;
  299.  
  300. import java.util.ArrayList;
  301. import java.util.LinkedHashMap;
  302.  
  303. public class TreatmentInsulin implements ITreatment{
  304.  
  305. @Override
  306. public void treat(LinkedHashMap<PatientType, Integer> patientInfoMap, ArrayList<Medicines> appliedTreatementList) {
  307. if(appliedTreatementList.contains(Medicines.INSULIN)){
  308. LinkedHashMap<PatientType, Integer> map = patientInfoMap;
  309. Quarantine.isInsulineInjected = true;
  310. if(appliedTreatementList.contains(Medicines.ANTIBIOTIC)){
  311. map.put(PatientType.FEVER, map.get(PatientType.HEALTHY) + map.get(PatientType.FEVER)-Quarantine.treatedTBPatientCount);
  312. map.put(PatientType.HEALTHY, Quarantine.treatedTBPatientCount);
  313.  
  314. }
  315. }
  316.  
  317. }
  318.  
  319. }
  320.  
  321. package quarantine;
  322.  
  323. import java.util.ArrayList;
  324. import java.util.LinkedHashMap;
  325.  
  326. public class TreatmentParacetamol implements ITreatment{
  327.  
  328. @Override
  329. public void treat(LinkedHashMap<PatientType, Integer> patientInfoMap, ArrayList<Medicines> appliedTreatementList) {
  330. if(appliedTreatementList.contains(Medicines.PARACETOL)){
  331. Quarantine.isInsulineInjected = false;
  332. LinkedHashMap<PatientType, Integer> map = patientInfoMap;
  333. map.put(PatientType.HEALTHY, map.get(PatientType.HEALTHY) + map.get(PatientType.FEVER));
  334. map.put(PatientType.FEVER, 0);
  335. }
  336.  
  337. }
  338.  
  339. }
  340.  
  341. public void tick() {
  342. // am I diabetic? have I been treated with insulin?
  343. if ( hasAilment(DIABETES) && !isMedicatedWith(INSULIN) ) {
  344. die(); // x_x
  345. return;
  346. }
  347.  
  348. // healthy? insulin + antibiotics = fever
  349. if ( !hasAilments() && isMedicatedWith(INSULIN) && isMedicatedWith(ANTIBIOTICS) ) {
  350. addAilment(FEVER);
  351. return;
  352. }
  353.  
  354. // vvv continue ad infinitum
  355. }
  356.  
  357. public String getCode() { return code; }
  358.  
  359. private static final Map<String, PatientType> codeMap = new HashMap<>();
  360. static {
  361. for ( PatientType type : values() ) {
  362. codeMap.put( type.code, type );
  363. }
  364. }
  365.  
  366. public static PatientType getByCode(String code) {
  367. PatientType retval = codeMap.get(code);
  368. if ( retval == null ) throw new NoSuchElementException(code);
  369. return retval;
  370. }
  371.  
  372. interface ITreatment {
  373. void treat(Map<PatientType, Integer> patientInfoMap, List<Medicines> appliedTreatmentList);
  374. }
  375.  
  376. interface ITreatment {
  377. void treat(Quarantine quarantine);
  378. }
  379.  
  380. public String report() {
  381. try {
  382. final String[] result = {""};
  383. patientInfoMap.forEach((k, v) -> result[0] += PatientType.getValue(k) + ":" + v.toString() + " ");
  384. return result[0].trim();
  385. } catch (Exception e) {
  386. e.printStackTrace(); // <--
  387. }
  388. return null; // <--
  389. }
  390.  
  391. protected Quarantine(Map<PatientType, Integer> patientCount);
  392.  
  393. public Quarantine createFromCodeString(String codeString) {
  394. Map<PatientType, Integer> patientCount = new LinkedHashMap<>(codeString.length() / 2 + 1);
  395. for ( String code : codeString.split(",") ) {
  396. PatientType type = PatientType.getByCode(code);
  397. patientCount.merge(type, 1, Integer::sum);
  398. }
  399. return new Quarantine(patientCount);
  400. }
  401.  
  402. // PatientType.java
  403. public Map<PatientType, Integer> createFrequencyMap(String codeString) {
  404. Map<PatientType, Integer> frequency = new HashMap<>(codeString.length() / 2 + 1);
  405. for ( String code : codeString.split(",") ) {
  406. PatientType type = getByCode(code);
  407. frequency.merge(type, 1, Integer::sum);
  408. }
  409. return frequency;
  410. }
  411.  
  412. // Quarantine.java
  413. public Quarantine createFromCodeString(String codeString) {
  414. return new Quarantine( PatientType.createFrequencyMap(codeString) );
  415. }
  416.  
  417. @Override
  418. public void treat(LinkedHashMap<PatientType, Integer> patientInfoMap, ArrayList<Medicines> appliedTreatementList) {
  419.  
  420. if(appliedTreatementList.contains(Medicines.ASPIRIN)){
  421. LinkedHashMap<PatientType, Integer> map = patientInfoMap;
  422.  
  423. if(appliedTreatementList.contains(Medicines.PARACETOL)){
  424. Quarantine.isInsulineInjected = true; //Because They are Dead
  425. map.put(PatientType.DEAD, map.get(PatientType.DEAD) + map.get(PatientType.FEVER) + map.get(PatientType.HEALTHY) + map.get(PatientType.DIABATIC) + map.get(PatientType.TUBERCLOSIS));
  426. map.put(PatientType.HEALTHY, 0);
  427. map.put(PatientType.DIABATIC, 0);
  428. map.put(PatientType.TUBERCLOSIS, 0);
  429. }else{
  430. Quarantine.isInsulineInjected = false;
  431. map.put(PatientType.HEALTHY, map.get(PatientType.HEALTHY) + map.get(PatientType.FEVER));
  432. map.put(PatientType.FEVER, 0);
  433. }
  434. }
  435. }
Add Comment
Please, Sign In to add comment