Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.97 KB | None | 0 0
  1. public interface MalfunctionHandler
  2. {
  3. public void processMalfunction(Malfunction malfunciton);
  4. public void setNextHandler(MalfunctionHandler handler);
  5. }
  6.  
  7. public enum Severity
  8. {
  9. TRIVIAL, LOW, MEDIUM, HIGH
  10. }
  11.  
  12. public class Malfunction
  13. {
  14. /**
  15. * severity is a type of Severity
  16. */
  17. Severity severity;
  18.  
  19. /**
  20. * @param description describes the severity of the problem
  21. */
  22. String description;
  23.  
  24. Malfunction(Severity severity, String description)
  25. {
  26. if(description == null)
  27. {
  28. description = "No description available. Probably serious.";
  29. }
  30.  
  31. if(description.isEmpty())
  32. {
  33. description = "No description available. Probably serious.";
  34. }
  35.  
  36. this.severity = severity;
  37. this.description = description;
  38. }
  39.  
  40. public Severity getSeverity()
  41. {
  42. return severity;
  43. }
  44.  
  45. public String getDescription()
  46. {
  47. return description;
  48. }
  49.  
  50. public void setSeverity(Severity severity)
  51. {
  52. this.severity = severity;
  53. }
  54.  
  55. public void setDescription(String description)
  56. {
  57. this.description = description;
  58. }
  59. }
  60.  
  61. public class SpaceMonkey implements MalfunctionHandler
  62. {
  63.  
  64. Severity severity;
  65. MalfunctionHandler next;
  66. File read = new File("expected-bronze.txt");
  67. File f = new File("log-bronze.txt");
  68.  
  69. SpaceMonkey(Severity severity)
  70. {
  71. this.severity = severity;
  72. System.out.println(FileUtility.readFile(read));
  73. }
  74.  
  75. @Override
  76. public void processMalfunction(Malfunction malfunction)
  77. {
  78. if (malfunction.getSeverity() == Severity.LOW)
  79. {
  80. final String[] splits = FileUtility.readFile(read).split("problem.");
  81. for (String asset : splits)
  82. {
  83. if (asset.contains("Space monkey"))
  84. {
  85. FileUtility.writeFile(f, asset + "problem.");
  86. System.out.println(asset + "problem.");
  87. }
  88. }
  89. }
  90. else
  91. {
  92. next.setNextHandler(next);
  93. }
  94. }
  95.  
  96. @Override
  97. public void setNextHandler(MalfunctionHandler next)
  98. {
  99. this.next = next;
  100. }
  101. }
  102.  
  103. public class ServiceRobot implements MalfunctionHandler
  104. {
  105.  
  106. Severity severity;
  107. MalfunctionHandler next;
  108. File read = new File("expected-bronze.txt");
  109. File f = new File("log-bronze.txt");
  110.  
  111. ServiceRobot(Severity severity)
  112. {
  113. this.severity = severity;
  114. }
  115.  
  116.  
  117. @Override
  118. public void processMalfunction(Malfunction malfuntion)
  119. {
  120.  
  121.  
  122. if (this.severity == severity)
  123. {
  124. String[] splits = FileUtility.readFile(read).split("problem.");
  125. for(String asset : splits)
  126. {
  127. if(asset.contains("Service robot"))
  128. {
  129. FileUtility.writeFile(f, asset + "problem.");
  130. System.out.println(asset + "problem.");
  131.  
  132. }
  133. }
  134. }
  135. else
  136. {
  137. next.setNextHandler(next);
  138. }
  139. }
  140.  
  141. @Override
  142. public void setNextHandler(MalfunctionHandler next)
  143. {
  144. this.next = next;
  145. }
  146.  
  147. }
  148.  
  149. public class Engineer implements MalfunctionHandler
  150. {
  151.  
  152. Severity severity;
  153. MalfunctionHandler next;
  154. File read = new File("expected-bronze.txt");
  155.  
  156. Engineer(Severity severity)
  157. {
  158. this.severity = severity;
  159. }
  160.  
  161.  
  162. @Override
  163. public void processMalfunction(Malfunction malfuntion)
  164. {
  165. File f = new File("log-bronze.txt");
  166.  
  167. if (this.severity == severity)
  168. {
  169. String[] splits = FileUtility.readFile(read).split("problem.");
  170. for(String asset : splits)
  171. {
  172. if(asset.contains("Engineer"))
  173. {
  174. FileUtility.writeFile(f, asset + "problem.");
  175. System.out.println(asset + "problem.");
  176.  
  177. }
  178. }
  179. }
  180. else
  181. {
  182. next.setNextHandler(next);
  183. }
  184. }
  185.  
  186. @Override
  187. public void setNextHandler(MalfunctionHandler next)
  188. {
  189. this.next = next;
  190. }
  191.  
  192. }
  193.  
  194. public class Captain implements MalfunctionHandler
  195. {
  196. Severity severity;
  197. MalfunctionHandler next;
  198. File read = new File("expected-bronze.txt");
  199.  
  200. Captain(Severity severity)
  201. {
  202. this.severity = severity;
  203. }
  204. @Override
  205. public void processMalfunction(Malfunction malfuntion)
  206. {
  207. File f = new File("log-bronze.txt");
  208.  
  209. if (this.severity == severity)
  210. {
  211. String[] splits = FileUtility.readFile(read).split("problem.");
  212. for(String asset : splits)
  213. {
  214. if(asset.contains("Captain"))
  215. {
  216. FileUtility.writeFile(f, asset + "problem.");
  217. System.out.println(asset + "problem.");
  218.  
  219. }
  220. }
  221. }
  222. else
  223. {
  224. next.setNextHandler(next);
  225. }
  226. }
  227.  
  228. @Override
  229. public void setNextHandler(MalfunctionHandler next)
  230. {
  231. this.next = next;
  232. }
  233.  
  234. }
  235.  
  236. public class FileUtility
  237. {
  238.  
  239. /** This method appends a single string to a text file.
  240. *
  241. * @param f The file to write to
  242. * @param entry The string to append
  243. */
  244. public static void writeFile(File f, String entry)
  245. {
  246. try
  247. {
  248. final BufferedWriter out = new BufferedWriter(new FileWriter(f, true));
  249. out.write(entry);
  250. out.close();
  251. } catch (IOException e)
  252. {
  253. System.err.println("Problem writing to the file");
  254. }
  255. }
  256.  
  257. /** This method resets the named text file.
  258. *
  259. * @param f The file to reset
  260. */
  261. public static void resetFile(File f) {
  262. try {
  263. final BufferedWriter out = new BufferedWriter(new FileWriter(f, false));
  264. out.write("");
  265. out.close();
  266. } catch (IOException e) {
  267. System.err.println("Problem reset the file");
  268. }
  269. }
  270.  
  271. /** This method reads the contents of a text file.
  272. *
  273. * @param f The file to read from
  274. * @return the contents of the text file as a single string
  275. */
  276. public static String readFile(File f) {
  277. final StringBuilder sb = new StringBuilder();
  278. try {
  279. final Scanner scanner = new Scanner(f);
  280. while (scanner.hasNextLine()) {
  281. sb.append(scanner.nextLine());
  282. }
  283. scanner.close();
  284. } catch (FileNotFoundException e) {
  285. System.err.println("Problem reading from file");
  286. }
  287. return sb.toString();
  288. }
  289. }
  290.  
  291.  
  292. public class MalfunctionHandlerTest {
  293.  
  294. /**
  295. * No-args constructor.
  296. */
  297. public MalfunctionHandlerTest() {
  298. }
  299.  
  300. /**
  301. * Test of processMalfunction method.
  302. */
  303. @Test
  304. public void testProcessMalfunction() {
  305.  
  306. // Instanciate malfunction handlers
  307. final SpaceMonkey sm = new SpaceMonkey(Severity.TRIVIAL);
  308. final ServiceRobot sr = new ServiceRobot(Severity.LOW);
  309. final Engineer e = new Engineer(Severity.MEDIUM);
  310. final Captain c = new Captain(Severity.HIGH);
  311.  
  312. // Construct chain of responsbility
  313. sm.setNextHandler(sr);
  314. sr.setNextHandler(e);
  315. e.setNextHandler(c);
  316.  
  317. // Create malfunctions
  318. final Malfunction m1 = new Malfunction(Severity.HIGH, "Life support error. Oxygen "
  319. + "Recycling unit damaged, running at half efficiency");
  320. final Malfunction m2 = new Malfunction(Severity.LOW, "Communications error. Cannot "
  321. + "find Jazz FM");
  322. final Malfunction m3 = new Malfunction(Severity.MEDIUM, "Power supply error. Solar Panel "
  323. + "2 damaged, running at 31.3333% efficiency");
  324. final Malfunction m4 = new Malfunction(Severity.MEDIUM, "Thermal regulation error. Sensor "
  325. + "damaged, manual temperature regulation needed");
  326. final Malfunction m5 = new Malfunction(Severity.TRIVIAL, "Trash can full on C-Desk.");
  327. final Malfunction m6 = new Malfunction(Severity.LOW, "Shower plug hole full of monkey hair");
  328. final Malfunction m7 = new Malfunction(Severity.HIGH, "Proximity alert. Collision imminent");
  329.  
  330. // Clean log file
  331. FileUtility.resetFile(new File("log-bronze.txt"));
  332.  
  333. // Process malfunctions
  334. sm.processMalfunction(m1);
  335. sm.processMalfunction(m2);
  336. sm.processMalfunction(m3);
  337. sm.processMalfunction(m4);
  338. sm.processMalfunction(m5);
  339. sm.processMalfunction(m6);
  340. sm.processMalfunction(m7);
  341.  
  342. // Check log file
  343. final String actualOutput = FileUtility.readFile(new File("log-bronze.txt"));
  344. final String expectedOutput = FileUtility.readFile(new File("expected-bronze.txt"));
  345. assertEquals(actualOutput, expectedOutput);
  346. }
  347. }
  348.  
  349. public void processMalfunction(Malfunction malfunction) {
  350. doSomething();
  351. this.next.processMalfunction(malfunction);
  352. }
  353.  
  354. Link1 start = new Link1();
  355. Link2 link2 = new Link2();
  356. start.setNextHandler(link2);
  357. Link3 link3 = new Link3();
  358. link2.setNextHandler(link3);
  359. ...
  360.  
  361. next.setNextHandler(next);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement