Advertisement
Guest User

Controller

a guest
Feb 21st, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4.  
  5. public class Controller implements IController {
  6.  
  7. private IService service;
  8. private IResponse response;
  9.  
  10.  
  11. public Controller() {
  12. this.service = null;
  13. this.response = null;
  14. }
  15.  
  16. public IService getService() {
  17. return this.service;
  18. }
  19.  
  20. public void setService(IService service) {
  21. this.service = service;
  22. }
  23.  
  24. public IResponse getResponse() {
  25. return this.response;
  26. }
  27.  
  28. public void setResponse(IResponse response) {
  29. this.response = response;
  30. }
  31.  
  32.  
  33. /*
  34. *
  35. * */
  36. public IResponse getStudent(Student student) {
  37. int respCode = 0;
  38. String respMsg = "";
  39. if(student.equals(service.getStudent(student))) {
  40. respCode = 200;
  41. respMsg = "OK";
  42. response.setContent(student);
  43. }
  44. else if(!student.equals(service.getStudent(student))) {
  45. respCode = 404;
  46. respMsg = "NOT_FOUND";
  47. }
  48. else if(student.getPID() == null) {
  49. respCode = 400;
  50. respMsg = "BAD_REQUEST";
  51. }
  52. else {
  53. respCode = 400;
  54. respMsg = "BAD_REQUEST";
  55. }
  56. response.setResponseCode(respCode);
  57. response.setResponseString(respMsg);
  58. return response;
  59. }
  60.  
  61.  
  62. /*
  63. *
  64. * */
  65. public IResponse addStudent(Student student) {
  66. int respCode = 0;
  67. String respMsg = "";
  68. if(getStudent(student).getContent() == student) {
  69. service.updateStudent(student);
  70. respCode = 202;
  71. respMsg = "ACCEPTED";
  72. }
  73. else if(!(getStudent(student).getContent() == student)) {
  74. service.addStudent(student);
  75. respCode = 201;
  76. respMsg = "CREATED";
  77. }
  78. else if(student.getName() != null && student.getEmail() != null) {
  79. service.addStudent(student);
  80. respCode = 201;
  81. respMsg = "CREATED";
  82. }
  83. else {
  84. respCode = 400;
  85. respMsg = "BAD_REQUEST";
  86. }
  87.  
  88. response.setResponseCode(respCode);
  89. response.setResponseString(respMsg);
  90. return response;
  91. }
  92.  
  93.  
  94. /*
  95. *
  96. * */
  97. public IResponse removeStudent(Student student) {
  98. int respCode = 0;
  99. String respMsg = "";
  100. if(getStudent(student).getContent() == student) {
  101. service.removeStudent(student);
  102. respCode = 200;
  103. respMsg = "OK";
  104. }
  105. else if(!(getStudent(student).getContent() == student)) {
  106. respCode = 200;
  107. respMsg = "OK";
  108. }
  109. else {
  110. respCode = 400;
  111. respMsg = "BAD_REQUEST";
  112. }
  113.  
  114. response.setResponseCode(respCode);
  115. response.setResponseString(respMsg);
  116. return response;
  117. }
  118.  
  119.  
  120. /*
  121. *
  122. * */
  123. public IResponse updateStudent(Student student) {
  124. int respCode = 0;
  125. String respMsg = "";
  126. if(service.updateStudent(student)) {
  127. respCode = 200;
  128. respMsg = "OK";
  129. }
  130. else if(service.getStudent(student) == null) {
  131. respCode = 404;
  132. respMsg = "NOT_FOUND";
  133. }
  134. else {
  135. respCode = 400;
  136. respMsg = "BAD_REQUEST";
  137. }
  138.  
  139. response.setResponseCode(respCode);
  140. response.setResponseString(respMsg);
  141. return response;
  142. }
  143.  
  144.  
  145. /*
  146. *
  147. * */
  148. public IResponse getAllStudent() {
  149. int respCode = 0;
  150. String respMsg = "";
  151. if(!service.getAllStudent().isEmpty()) {
  152. respCode = 200;
  153. respMsg = "OK";
  154. response.setListContent(service.getAllStudent());
  155. }
  156. else {
  157. respCode = 404;
  158. respMsg = "NOT_FOUND";
  159. ArrayList<Student> newList = new ArrayList<Student>(0);
  160. response.setListContent(newList);
  161. }
  162.  
  163. response.setResponseCode(respCode);
  164. response.setResponseString(respMsg);
  165. return response;
  166. }
  167.  
  168. public static void main(String[] args) {
  169. Controller C = new Controller();
  170. Service S = new Service();
  171. Database D = new Database();
  172. IResponse R = new StudentResponseObj();
  173.  
  174. }
  175.  
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement