Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class Controller implements IController {
- private IService service;
- private IResponse response;
- public Controller() {
- this.service = null;
- this.response = null;
- }
- public IService getService() {
- return this.service;
- }
- public void setService(IService service) {
- this.service = service;
- }
- public IResponse getResponse() {
- return this.response;
- }
- public void setResponse(IResponse response) {
- this.response = response;
- }
- /*
- *
- * */
- public IResponse getStudent(Student student) {
- int respCode = 0;
- String respMsg = "";
- if(student.equals(service.getStudent(student))) {
- respCode = 200;
- respMsg = "OK";
- response.setContent(student);
- }
- else if(!student.equals(service.getStudent(student))) {
- respCode = 404;
- respMsg = "NOT_FOUND";
- }
- else if(student.getPID() == null) {
- respCode = 400;
- respMsg = "BAD_REQUEST";
- }
- else {
- respCode = 400;
- respMsg = "BAD_REQUEST";
- }
- response.setResponseCode(respCode);
- response.setResponseString(respMsg);
- return response;
- }
- /*
- *
- * */
- public IResponse addStudent(Student student) {
- int respCode = 0;
- String respMsg = "";
- if(getStudent(student).getContent() == student) {
- service.updateStudent(student);
- respCode = 202;
- respMsg = "ACCEPTED";
- }
- else if(!(getStudent(student).getContent() == student)) {
- service.addStudent(student);
- respCode = 201;
- respMsg = "CREATED";
- }
- else if(student.getName() != null && student.getEmail() != null) {
- service.addStudent(student);
- respCode = 201;
- respMsg = "CREATED";
- }
- else {
- respCode = 400;
- respMsg = "BAD_REQUEST";
- }
- response.setResponseCode(respCode);
- response.setResponseString(respMsg);
- return response;
- }
- /*
- *
- * */
- public IResponse removeStudent(Student student) {
- int respCode = 0;
- String respMsg = "";
- if(getStudent(student).getContent() == student) {
- service.removeStudent(student);
- respCode = 200;
- respMsg = "OK";
- }
- else if(!(getStudent(student).getContent() == student)) {
- respCode = 200;
- respMsg = "OK";
- }
- else {
- respCode = 400;
- respMsg = "BAD_REQUEST";
- }
- response.setResponseCode(respCode);
- response.setResponseString(respMsg);
- return response;
- }
- /*
- *
- * */
- public IResponse updateStudent(Student student) {
- int respCode = 0;
- String respMsg = "";
- if(service.updateStudent(student)) {
- respCode = 200;
- respMsg = "OK";
- }
- else if(service.getStudent(student) == null) {
- respCode = 404;
- respMsg = "NOT_FOUND";
- }
- else {
- respCode = 400;
- respMsg = "BAD_REQUEST";
- }
- response.setResponseCode(respCode);
- response.setResponseString(respMsg);
- return response;
- }
- /*
- *
- * */
- public IResponse getAllStudent() {
- int respCode = 0;
- String respMsg = "";
- if(!service.getAllStudent().isEmpty()) {
- respCode = 200;
- respMsg = "OK";
- response.setListContent(service.getAllStudent());
- }
- else {
- respCode = 404;
- respMsg = "NOT_FOUND";
- ArrayList<Student> newList = new ArrayList<Student>(0);
- response.setListContent(newList);
- }
- response.setResponseCode(respCode);
- response.setResponseString(respMsg);
- return response;
- }
- public static void main(String[] args) {
- Controller C = new Controller();
- Service S = new Service();
- Database D = new Database();
- IResponse R = new StudentResponseObj();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement