Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.40 KB | None | 0 0
  1. package main.server.logic.handler;
  2.  
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5.  
  6. import main.server.logic.handler.model.Output;
  7. import main.server.logic.model.Course;
  8. import main.server.logic.model.Student;
  9. import main.server.logic.model.University;
  10. import main.utilities.Config;
  11.  
  12. public class OutputHandler {
  13.  
  14. public static final int WAITING = 0;
  15. public static final int FINISHWAITING = 1;
  16. public static final int CLERK = 2;
  17. public static final int CLERKLOGIN = 3;
  18. public static final int STUDENT = 4;
  19. public static final int STUDENTLOGIN = 5;
  20. public static final int CREATECOURSE = 6;
  21. public static final int CREATESTUDENT = 7;
  22. public static final int CANCELCOURSE = 8;
  23. public static final int DELETECOURSE = 9;
  24. public static final int DELETESTUDENT = 10;
  25. public static final int SELECTCOURSE = 11;
  26. public static final int REGISTERFORCOURSE = 12;
  27. public static final int DROPCOURSE = 13;
  28. public static final int DEREGISTERCOURSE = 14;
  29.  
  30. public Output clerkLogin(String input) {
  31. Output output = new Output("", 0);
  32. if (input.equalsIgnoreCase(Config.CLERK_PASSWORD)) {
  33. output.setOutput("What can I do for you? Menu: Create Course/Student, Delete Course/Student, Cancel Course, Dean's List, Query.");
  34. output.setState(CLERK);
  35. } else {
  36. output.setOutput("Wrong password! Please input the password:");
  37. output.setState(CLERKLOGIN);
  38. }
  39. return output;
  40. }
  41.  
  42. public Output studentLogin(String input) {
  43. Output output = new Output("", 0);
  44. String[] strArray = null;
  45. strArray = input.split(",");
  46. boolean result = true;
  47. if (strArray.length != 2) {
  48. output.setOutput("Your input should be in this format: 'student number, name'");
  49. output.setState(STUDENTLOGIN);
  50. } else {
  51. String number = strArray[0].trim();
  52. String name = strArray[1].trim();
  53. Pattern pattern = Pattern.compile("[0-9]*");
  54. Matcher isNum = pattern.matcher(number);
  55. if (!isNum.matches()) {
  56. output.setOutput("Your input items should be in correct format.");
  57. output.setState(STUDENTLOGIN);
  58. } else if (Integer.parseInt(strArray[0]) < 100000000
  59. || Integer.parseInt(strArray[0]) > 999999999) {
  60. output.setOutput("The length of student number must be 9.");
  61. output.setState(STUDENTLOGIN);
  62. } else {
  63. result = University.getInstance().LookupStudent(
  64. Integer.parseInt(number), name);
  65. if (result) {
  66. output.setOutput("What can I do for you? Menu: Select Course, Register for Course, Drop Course, Deregister Course, Query.");
  67. University.getInstance().setCurrentstudent(
  68. Integer.parseInt(number));
  69. output.setState(STUDENT);
  70. } else {
  71. output.setOutput("Invalid student number or student name.");
  72. output.setState(STUDENTLOGIN);
  73. }
  74. }
  75. }
  76. return output;
  77. }
  78.  
  79. public Output createCourse(String input) {
  80. Output output = new Output("", 0);
  81. String[] strArray = null;
  82. strArray = input.split(",");
  83. boolean result = true;
  84. /*
  85. long current = System.currentTimeMillis();
  86. int a = (int) ((current - StartServer.start) / (Config.STIMULATED_DAY) - Config.OVERDUE);
  87. int b = (int) ((current - StartServer.start) / (Config.STIMULATED_DAY) - Config.TERM_LASTS);
  88. */
  89. if (Config.TERM_ENDS) {
  90. output.setOutput("Term ends!");
  91. output.setState(CLERK);
  92. } else if (!Config.REGISTRATION_STARTS) {
  93. if (strArray.length != 8) {
  94. output.setOutput("Your input should be in this format: 'title, course code, capsize, enforce prereqs(y/n), number of midterms, number of assignments, has a final(y/n), is project course(y/n)'");
  95. output.setState(CREATECOURSE);
  96. } else {
  97. String title = strArray[0].trim();
  98. String code = strArray[1].replace(" ", "");
  99. String cap = strArray[2].replace(" ", "");
  100. String enforcePrereqs = strArray[3].replace(" ", "");
  101. String numofmidterms = strArray[4].replace(" ", "");
  102. String numofassignments = strArray[5].replace(" ", "");
  103. String hasafinal = strArray[6].replace(" ", "");
  104. String isprojectcourse = strArray[7].replace(" ", "");
  105. Pattern pattern = Pattern.compile("[0-9]*");
  106. Matcher isNumcode = pattern.matcher(code);
  107. Matcher isNumcap = pattern.matcher(cap);
  108. Matcher isNumnumofmidterms = pattern.matcher(numofmidterms);
  109. Matcher isNumnumofassignments = pattern.matcher(numofassignments);
  110. if (!isNumcode.matches()
  111. || !isNumcap.matches()
  112. || !isNumnumofmidterms.matches()
  113. || !isNumnumofassignments.matches()
  114. || !(enforcePrereqs.equalsIgnoreCase("y") || enforcePrereqs
  115. .equalsIgnoreCase("n"))
  116. || !(hasafinal.equalsIgnoreCase("y") || hasafinal
  117. .equalsIgnoreCase("n"))
  118. || !(isprojectcourse.equalsIgnoreCase("y") || isprojectcourse
  119. .equalsIgnoreCase("n"))) {
  120. output.setOutput("Your input items should be in correct format.");
  121. output.setState(CREATECOURSE);
  122. } else if (Integer.parseInt(code) < 100000
  123. || Integer.parseInt(code) > 999999) {
  124. output.setOutput("The length of course code must be 6.");
  125. output.setState(CREATECOURSE);
  126. } else if (Integer.parseInt(numofmidterms) < 0
  127. || Integer.parseInt(numofmidterms) > 2
  128. || Integer.parseInt(numofassignments) < 0
  129. || Integer.parseInt(numofassignments) > 5) {
  130. output.setOutput("The unmber of midterms should range from 0 to 2, the number of assignments should range from 0 to 5");
  131. output.setState(CREATECOURSE);
  132. } else {
  133. boolean enforcep, hasf, isp;
  134. if (enforcePrereqs.equalsIgnoreCase("y")) {
  135. enforcep = true;
  136. } else {
  137. enforcep = false;
  138. }
  139. if (hasafinal.equalsIgnoreCase("y")) {
  140. hasf = true;
  141. } else {
  142. hasf = false;
  143. }
  144. if (isprojectcourse.equalsIgnoreCase("y")) {
  145. isp = true;
  146. } else {
  147. isp = false;
  148. }
  149. result = University.getInstance().CreateCourse(title,
  150. Integer.parseInt(code), Integer.parseInt(cap),
  151. enforcep, Integer.parseInt(numofmidterms),
  152. Integer.parseInt(numofassignments), hasf, isp);
  153. if (result) {
  154. output.setOutput("Success!");
  155. } else {
  156. output.setOutput("The course already exists!");
  157. }
  158. output.setState(CLERK);
  159. }
  160. }
  161. } else {
  162. output.setOutput("Overdue!");
  163. output.setState(CLERK);
  164. }
  165. return output;
  166. }
  167.  
  168. public Output createStudent(String input) {
  169. Output output = new Output("", 0);
  170. String[] strArray = null;
  171. strArray = input.split(",");
  172. boolean result = true;
  173. /*
  174. long current = System.currentTimeMillis();
  175. int a = (int) ((current - StartServer.start) / (Config.STIMULATED_DAY) - Config.OVERDUE);
  176. int b = (int) ((current - StartServer.start) / (Config.STIMULATED_DAY) - Config.TERM_LASTS);
  177. */
  178. if (Config.TERM_ENDS) {
  179. output.setOutput("Term ends!");
  180. output.setState(CLERK);
  181. } else if (!Config.REGISTRATION_STARTS) {
  182. if (strArray.length != 3) {
  183. output.setOutput("Your input should be in this format: 'student number, name, is fulltime(y/n)'");
  184. output.setState(CREATESTUDENT);
  185. } else {
  186. String number = strArray[0].trim();
  187. String name = strArray[1].trim();
  188. String isfulltime = strArray[2].trim();
  189. Pattern pattern = Pattern.compile("[0-9]*");
  190. Matcher isNum = pattern.matcher(number);
  191. if (!isNum.matches()
  192. || !(isfulltime.equalsIgnoreCase("y") || isfulltime
  193. .equalsIgnoreCase("n"))) {
  194. output.setOutput("Your input items should be in correct format.");
  195. output.setState(CREATESTUDENT);
  196. } else if (Integer.parseInt(number) < 100000000
  197. || Integer.parseInt(number) > 999999999) {
  198. output.setOutput("The length of student number must be 9.");
  199. output.setState(CREATESTUDENT);
  200. } else {
  201. boolean isf;
  202. if (isfulltime.equalsIgnoreCase("y")) {
  203. isf = true;
  204. } else {
  205. isf = false;
  206. }
  207. result = University.getInstance().CreateStudent(
  208. Integer.parseInt(number), name, isf);
  209. if (result) {
  210. output.setOutput("Success!");
  211. } else {
  212. output.setOutput("The student already exists!");
  213. }
  214. output.setState(CLERK);
  215. }
  216. }
  217. } else {
  218. output.setOutput("Overdue!");
  219. output.setState(CLERK);
  220. }
  221. return output;
  222. }
  223.  
  224. public Output cancelCourse(String input) {
  225. Output output = new Output("", 0);
  226. String code = input.trim();
  227. Pattern pattern = Pattern.compile("[0-9]*");
  228. Matcher isNum = pattern.matcher(code);
  229. boolean result = true;
  230. /*
  231. long current = System.currentTimeMillis();
  232. int b = (int) ((current - StartServer.start) / (Config.STIMULATED_DAY) - Config.TERM_LASTS);
  233. */
  234. if (Config.TERM_ENDS) {
  235. output.setOutput("Term ends!");
  236. output.setState(CLERK);
  237. } else if (Config.REGISTRATION_ENDS) {
  238. if (input.replace(" ", "").equalsIgnoreCase("") || !isNum.matches()) {
  239. output.setOutput("Your input should be in correct format.");
  240. output.setState(CANCELCOURSE);
  241. } else if (Integer.parseInt(code) < 100000
  242. || Integer.parseInt(code) > 999999) {
  243. output.setOutput("The length of course code must be 6.");
  244. output.setState(CANCELCOURSE);
  245. } else {
  246. if (University.getInstance()
  247. .CheckCourse(Integer.parseInt(code)) == false) {
  248. output.setOutput("The course does not exist!");
  249. output.setState(CANCELCOURSE);
  250. } else {
  251. Course c = (Course) University.getInstance().GetCourse(
  252. Integer.parseInt(code));
  253. result = University.getInstance().CancelCourse(c);
  254. if (result) {
  255. output.setOutput("Success!");
  256. } else {
  257. output.setOutput("Fail to cancel!");
  258. }
  259. output.setState(CLERK);
  260. }
  261. }
  262. } else {
  263. output.setOutput("Course cannot be canceled before registration ends!");
  264. output.setState(CLERK);
  265. }
  266. return output;
  267. }
  268.  
  269. public Output deleteCourse(String input) {
  270. Output output = new Output("", 0);
  271. String code = input.trim();
  272. Pattern pattern = Pattern.compile("[0-9]*");
  273. Matcher isNum = pattern.matcher(code);
  274. boolean result = true;
  275. /*
  276. long current = System.currentTimeMillis();
  277. int b = (int) ((current - StartServer.start) / (Config.STIMULATED_DAY) - Config.TERM_LASTS);
  278. */
  279. if (Config.TERM_ENDS) {
  280. output.setOutput("Term ends!");
  281. output.setState(CLERK);
  282. } else if (!Config.REGISTRATION_STARTS) {
  283. if (input.replace(" ", "").equalsIgnoreCase("") || !isNum.matches()) {
  284. output.setOutput("Your input should be in correct format.");
  285. output.setState(DELETECOURSE);
  286. } else if (Integer.parseInt(code) < 100000
  287. || Integer.parseInt(code) > 999999) {
  288. output.setOutput("The length of course code must be 6.");
  289. output.setState(DELETECOURSE);
  290. } else {
  291. if (University.getInstance().CheckCourse(Integer.parseInt(code)) == false) {
  292. output.setOutput("The course does not exist!");
  293. } else {
  294. Course c = University.getInstance().GetCourse(
  295. Integer.parseInt(code));
  296. result = University.getInstance().DestroyCourse(c);
  297. if (result) {
  298. output.setOutput("Success!");
  299. } else {
  300. output.setOutput("Fail to delete!");
  301. }
  302. }
  303. output.setState(CLERK);
  304. }
  305. } else {
  306. output.setOutput("Overdue!");
  307. output.setState(CLERK);
  308. }
  309. return output;
  310. }
  311.  
  312. public Output deleteStudent(String input) {
  313. Output output = new Output("", 0);
  314. String number = input.trim();
  315. Pattern pattern = Pattern.compile("[0-9]*");
  316. Matcher isNum = pattern.matcher(number);
  317. boolean result = true;
  318. /*
  319. long current = System.currentTimeMillis();
  320. int b = (int) ((current - StartServer.start) / (Config.STIMULATED_DAY) - Config.TERM_LASTS);
  321. */
  322. if (Config.TERM_ENDS) {
  323. output.setOutput("Term ends!");
  324. output.setState(CLERK);
  325. } else if (!Config.REGISTRATION_STARTS) {
  326. if (input.replace(" ", "").equalsIgnoreCase("") || !isNum.matches()) {
  327. output.setOutput("Your input should be in correct format.");
  328. output.setState(DELETESTUDENT);
  329. } else if (Integer.parseInt(number) < 100000000
  330. || Integer.parseInt(number) > 999999999) {
  331. output.setOutput("The length of student number must be 9.");
  332. output.setState(DELETESTUDENT);
  333. } else {
  334. if (University.getInstance().CheckStudent(Integer.parseInt(number)) == false) {
  335. output.setOutput("The student does not exist!");
  336. } else {
  337. Student s = University.getInstance().GetStudent(
  338. Integer.parseInt(number));
  339. result = University.getInstance().DestroyStudent(s);
  340. if (result) {
  341. output.setOutput("Success!");
  342. } else {
  343. output.setOutput("Fail to delete!");
  344. }
  345. }
  346. output.setState(CLERK);
  347. }
  348. } else {
  349. output.setOutput("Overdue!");
  350. output.setState(CLERK);
  351. }
  352. return output;
  353. }
  354.  
  355. public Output selectCourse(String input) {
  356. Output output = new Output("", 0);
  357. String code = input.trim();
  358. Pattern pattern = Pattern.compile("[0-9]*");
  359. Matcher isNum = pattern.matcher(code);
  360. boolean result = true;
  361. /*
  362. long current = System.currentTimeMillis();
  363. int b = (int) ((current - StartServer.start) / (Config.STIMULATED_DAY) - Config.TERM_LASTS);
  364. */
  365. if (Config.TERM_ENDS) {
  366. output.setOutput("Term ends!");
  367. output.setState(STUDENT);
  368. } else if (Config.REGISTRATION_ENDS){
  369. output.setOutput("Course cannot be selected after registration ends!");
  370. output.setState(STUDENT);
  371. } else if (input.replace(" ", "").equalsIgnoreCase("") || !isNum.matches()) {
  372. output.setOutput("Your input should be in correct format.");
  373. output.setState(SELECTCOURSE);
  374. } else if (Integer.parseInt(code) < 100000
  375. || Integer.parseInt(code) > 999999) {
  376. output.setOutput("The length of course code must be 6.");
  377. output.setState(SELECTCOURSE);
  378. } else if (!University.getInstance()
  379. .CheckCourse(Integer.parseInt(code))) {
  380. output.setOutput("The course does not exist!");
  381. output.setState(SELECTCOURSE);
  382. } else {
  383. int studentnumber = University.getInstance().getCurrentstudent();
  384. Student student = (Student) University.getInstance().GetStudent(
  385. studentnumber);
  386. result = student.SelectCourse(University.getInstance().GetCourse(
  387. Integer.parseInt(code)));
  388. if (result) {
  389. output.setOutput("Success!");
  390. } else {
  391. output.setOutput("Unable to select this course!");
  392. }
  393. output.setState(STUDENT);
  394. }
  395. return output;
  396. }
  397.  
  398. public Output registerforCourse(String input) {
  399. Output output = new Output("", 0);
  400. String code = input.trim();
  401. Pattern pattern = Pattern.compile("[0-9]*");
  402. Matcher isNum = pattern.matcher(code);
  403. boolean result = true;
  404. /*
  405. long current = System.currentTimeMillis();
  406. int a = (int) ((current - StartServer.start) / (Config.STIMULATED_DAY) - Config.OVERDUE);
  407. int b = (int) ((current - StartServer.start) / (Config.STIMULATED_DAY) - Config.TERM_LASTS);
  408. */
  409. if (Config.TERM_ENDS) {
  410. output.setOutput("Term ends!");
  411. output.setState(STUDENT);
  412. } else if (!Config.REGISTRATION_STARTS) {
  413. output.setOutput("Registration has not started!");
  414. output.setState(STUDENT);
  415. } else if (Config.REGISTRATION_ENDS) {
  416. output.setOutput("Registration has finished!");
  417. output.setState(STUDENT);
  418. } else {
  419. if (input.replace(" ", "").equalsIgnoreCase("") || !isNum.matches()) {
  420. output.setOutput("Your input should be in correct format.");
  421. output.setState(REGISTERFORCOURSE);
  422. } else if (Integer.parseInt(code) < 100000
  423. || Integer.parseInt(code) > 999999) {
  424. output.setOutput("The length of course code must be 6.");
  425. output.setState(REGISTERFORCOURSE);
  426. } else if (!University.getInstance().CheckCourse(
  427. Integer.parseInt(code))) {
  428. output.setOutput("The course does not exist!");
  429. output.setState(REGISTERFORCOURSE);
  430. } else {
  431. int studentnumber = University.getInstance()
  432. .getCurrentstudent();
  433. Student student = University.getInstance().GetStudent(
  434. studentnumber);
  435. Course course = University.getInstance().GetCourse(
  436. Integer.parseInt(code));
  437. result = University.getInstance().RegisterStudentForCourse(
  438. student, course);
  439. if (result) {
  440. output.setOutput("Success!");
  441. } else {
  442. output.setOutput("Unable to register for this course!");
  443. }
  444. output.setState(STUDENT);
  445. }
  446. }
  447. return output;
  448. }
  449.  
  450. public Output dropCourse(String input) {
  451. Output output = new Output("", 0);
  452. String code = input.trim();
  453. Pattern pattern = Pattern.compile("[0-9]*");
  454. Matcher isNum = pattern.matcher(code);
  455. boolean result = true;
  456. /*
  457. long current = System.currentTimeMillis();
  458. int b = (int) ((current - StartServer.start) / (Config.STIMULATED_DAY) - Config.TERM_LASTS);
  459. */
  460. if (Config.TERM_ENDS) {
  461. output.setOutput("Term ends!");
  462. output.setState(STUDENT);
  463. } else if (!Config.REGISTRATION_ENDS) { //XXX: This line was changed from !Config.REGISTRATION_STARTS->!Config.REGISTRATION_ENDS
  464. output.setOutput("Course cannot be dropped before registration ends!");
  465. output.setState(STUDENT);
  466. } else if (input.replace(" ", "").equalsIgnoreCase("") || !isNum.matches()) {
  467. output.setOutput("Your input should be in correct format.");
  468. output.setState(DROPCOURSE);
  469. } else if (Integer.parseInt(code) < 100000
  470. || Integer.parseInt(code) > 999999) {
  471. output.setOutput("The length of course code must be 6.");
  472. output.setState(DROPCOURSE);
  473. } else if (!University.getInstance().CheckCourse(Integer.parseInt(code))) {
  474. output.setOutput("The course does not exist!");
  475. output.setState(DROPCOURSE);
  476. } else {
  477. int studentnumber = University.getInstance().getCurrentstudent();
  478. Student student = (Student) University.getInstance().GetStudent(studentnumber);
  479. /*Course course = University.getInstance().GetCourse(Integer.parseInt(code));
  480. boolean result2 = University.getInstance().DeRegisterStudentFromCourse(student, course);*/
  481. result = student.DropCourse(University.getInstance().GetCourse(Integer.parseInt(code)));
  482. if (result /*&& result2*/) {
  483. output.setOutput("Success!");
  484. } else {
  485. output.setOutput("Unable to drop this course!");
  486. }
  487. output.setState(STUDENT);
  488. }
  489. return output;
  490. }
  491.  
  492. public Output deregisterCourse(String input) {
  493. Output output = new Output("", 0);
  494. String code = input.trim();
  495. Pattern pattern = Pattern.compile("[0-9]*");
  496. Matcher isNum = pattern.matcher(code);
  497. boolean result = true;
  498. /*
  499. long current = System.currentTimeMillis();
  500. int b = (int) ((current - StartServer.start) / (Config.STIMULATED_DAY) - Config.TERM_LASTS);
  501. */
  502. if (Config.TERM_ENDS) {
  503. output.setOutput("Term ends!");
  504. output.setState(STUDENT);
  505. } else if (!Config.REGISTRATION_STARTS) {
  506. output.setOutput("Registration has not started!");
  507. output.setState(STUDENT); //XXX: removed constraint that the Registration period had to have ended to proceed
  508. } else {
  509. if (input.replace(" ", "").equalsIgnoreCase("") || !isNum.matches()) {
  510. output.setOutput("Your input should be in correct format.");
  511. output.setState(DEREGISTERCOURSE);
  512. } else if (Integer.parseInt(code) < 100000
  513. || Integer.parseInt(code) > 999999) {
  514. output.setOutput("The length of course code must be 6.");
  515. output.setState(DEREGISTERCOURSE);
  516. } else if (!University.getInstance().CheckCourse(
  517. Integer.parseInt(code))) {
  518. output.setOutput("The course does not exist!");
  519. output.setState(DEREGISTERCOURSE);
  520. } else {
  521. int studentnumber = University.getInstance()
  522. .getCurrentstudent();
  523. Student student = University.getInstance().GetStudent(
  524. studentnumber);
  525. Course course = University.getInstance().GetCourse(
  526. Integer.parseInt(code));
  527. result = University.getInstance().DeRegisterStudentFromCourse(
  528. student, course);
  529. if (result) {
  530. output.setOutput("Success!");
  531. } else {
  532. output.setOutput("Unable to deregister from this course!");
  533. }
  534. output.setState(STUDENT);
  535. }
  536. }
  537. return output;
  538. }
  539.  
  540. public Output deansList() {
  541. Output output = new Output("", 0);
  542. /*
  543. long current = System.currentTimeMillis();
  544. int b = (int) ((current - StartServer.start) / (Config.STIMULATED_DAY) - Config.TERM_LASTS);
  545. */
  546. if (!Config.TERM_ENDS) {
  547. output.setOutput("Dean's list not generated!");
  548. output.setState(CLERK);
  549. } else {
  550. /*
  551. for (int i=0; i<University.getInstance().getCourses().size(); i++) {
  552. University.getInstance().MarkStudents(University.getInstance().getCourses().get(i));
  553. }
  554. */
  555. String o = "";
  556. for (int i=0; i<University.getInstance().DeansList().size(); i++) {
  557. o = o + "\n" + University.getInstance().DeansList().get(i).toString();
  558. }
  559. output.setOutput(o);
  560. output.setState(CLERK);
  561. }
  562. return output;
  563. }
  564.  
  565. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement