Procházet zdrojové kódy

implement functions regarding bubble sort.

171870002 před 5 roky
rodič
revize
eea49cffbd

+ 52 - 4
src/main/java/com/example/data_structure/common/Response.java

@@ -36,18 +36,66 @@ public class Response {
     public Response() {
     }
 
+
+    //buildSuccess
     public static Response buildSuccess(){
-        return null;
+        return new Response(true);
     }
 
     public static  Response buildSuccess(Object content,String message){
-        return null;
+        return new Response(content,message,true);
     }
 
     public static Response buildSuccess(Object content){
-        return null;
+        return new Response(content,true);
+    }
+
+    //buildFailure
+    public static Response buildFailure(){
+        return new Response(false);
+    }
+
+    public static Response buildFailure(Object content,String message){
+        return new Response(content,message,false);
+    }
+
+    public static Response buildFailure(Object content){
+        return new Response(content,false);
+    }
+
+    //getter
+    public Object getContent() {
+        return content;
+    }
+
+    public String getMessage() {
+        return message;
     }
 
-    
+    public boolean isSuccess() {
+        return success;
+    }
+
+    //setter
+    public void setContent(Object content) {
+        this.content = content;
+    }
 
+    public void setMessage(String message) {
+        this.message = message;
+    }
+
+    public void setSuccess(boolean success) {
+        this.success = success;
+    }
+
+    //toString
+    @Override
+    public String toString() {
+        return "Response{" +
+                "content=" + content +
+                ", message='" + message + '\'' +
+                ", success=" + success +
+                '}';
+    }
 }

+ 5 - 4
src/main/java/com/example/data_structure/controller/SortingController.java

@@ -7,18 +7,19 @@ import com.example.data_structure.vo.BubbleSortVO;
 import com.sun.corba.se.pept.transport.ResponseWaitingRoom;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
-import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.*;
 
 import java.util.ArrayList;
 
-@Controller
+@RestController
+@CrossOrigin
 public class SortingController {
 
     @Autowired
     SortingService sortingService;
 
-    @RequestMapping("/")
-    public Response getBubbleSortResult(String rule,ArrayList<Integer> content){
+    @GetMapping(value = "/bubble-sort/get-analysis-result")
+    public Response getBubbleSortResult(@RequestParam String rule,@RequestParam ArrayList<Integer> content){
         BubbleSortVO vo=sortingService.getBubbleSortResult(rule,content);
         Response re=Response.buildSuccess(vo);
         return re;

+ 4 - 0
src/main/java/com/example/data_structure/service/SortingService.java

@@ -40,7 +40,9 @@ public class SortingService {
         ArrayList<Bar>stepContent=BubbleStep.copyContent(initialStepContent);
         //int len=content.size();
         for(int i=len-1;i>0;i--){
+            System.out.println("i ====="+i);
             for(int j=0;j<i;j++){
+                System.out.println("j ===="+j);
                 //比较相邻两个值
                 Bar aBar=stepContent.get(j);
                 Bar bBar=stepContent.get(j+1);
@@ -90,6 +92,8 @@ public class SortingService {
         }
 
         bubbleVO.setOnSortingStates(onSortingSteps);
+        System.out.println("===============getIncBubbleSortResult=================");
+        System.out.println(onSortingSteps.toString());
         return bubbleVO;
     }
 

+ 28 - 0
src/test/java/com/example/data_structure/controller/SortingControllerTest.java

@@ -0,0 +1,28 @@
+package com.example.data_structure.controller;
+
+import com.example.data_structure.common.Response;
+import com.example.data_structure.service.SortingService;
+import com.example.data_structure.vo.BubbleSortVO;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+
+import java.util.ArrayList;
+
+@SpringBootTest
+public class SortingControllerTest {
+
+    @Autowired
+    private SortingController sortingController;
+
+    @Test
+    void getBubbleSortResult(){
+        ArrayList<Integer> content=new ArrayList<Integer>();
+        for(int i=9;i>=0;i--){
+            content.add(i);
+        }
+        Response re=sortingController.getBubbleSortResult("ascending",content);
+        System.out.println("==============sortingController======getBubbleSortingResult===============================================================");
+        System.out.println(re.toString());
+    }
+}

+ 2 - 2
src/test/java/com/example/data_structure/service/SortingServiceTest.java

@@ -22,8 +22,8 @@ public class SortingServiceTest {
         }
 
         BubbleSortVO bubbleResult=sortingService.getIncBubbleSortResult(numberList);
-        System.out.println("==============bubbleSortingResult==============");
-        System.out.println(bubbleResult.toString());
+        //System.out.println("==============bubbleSortingResult==============");
+        //System.out.println(bubbleResult.toString());
 
     }