操作數組的相關程序

    /**
     *  * 判斷修改會員等級的合法性
     * 1.當sorts大於數據庫中所有的sorts值時 (1,3) 5
     * 2.當sorts小於於數據庫中所有的sorts值時 1 (3,5)
     * 3.當sorts等於數據庫中所有的sorts值時 1 (1,3,5)
     * 4.當sorts在數據庫中能找到大於和小於它的值時 3 (1,5)
     * 說明:1.首先判斷list的長度,(大於1,等於1,等於0)三種情況
     *        2.當list.size()>1,將數據庫中的sort字段組成字符串,再轉換成數組 
     *        2.1當sorts等於數據庫中所有的sorts值時 1 (1,3,5)
     *        2.2得到該數組中的最大,最小值
     *        2.3.當sorts大於數據庫中所有的sorts值時 (1,3) 5
     *        2.4當sorts小於於數據庫中所有的sorts值時 1 (3,5)
     *        2.5當sorts在數據庫中能找到大於和小於它的值時 3 (1,2,4,5)    
     *        2.5.1 拆分(1,2,4,5)得到3之前的數據組成 數組 [1,2] ,再得到 3之前一位數據 進行判斷
     *        2.5.2 拆分(1,2,4,5)得到3之後的數據組成 數組 [4,5]   ,再得到3之後一位數據 進行判斷
     * @param sorts  會員等級排序
     * @param limitMax 所需累計最小值  (用最小值保存累計數值,最大值留着以後用)
     * @param upd   用於判斷是否是修改驗證 0 :不是  1:是
     * @param req
     * @param resp
     * @return
     */
    @RequestMapping("/leveljudgetwo.jhtml")
    @ResponseBody
    public String levelJudgeTwo(@RequestParam int sorts,
                 @RequestParam int limitMax,@RequestParam int upd,@RequestParam String                                    sortflag,HttpServletRequest req,
            HttpServletResponse resp) {
        int shop_id = 2;
        String str = "yes";
        String str1 = "";
        boolean flag = true;
        List<MemberLevel> list = this.memberLevelService.listObjectAll();//遍歷所有的對象
        System.out.println("sorts------------------"+sorts);
        System.out.println("limitMax------------------"+limitMax);

        /*
         * 當list.size()>1,將數據庫中的sort字段組成字符串,再轉換成數組
         */
        if (list.size() > 1) {
            for (MemberLevel level : list) {
                int sortsTwo = level.getSort();
                str1 = str1 + sortsTwo + ",";
            }
            str1 = str1.substring(0, str1.length() - 1);
            String[] arr = str1.split(",");

            String [] arrOne = {sortflag};

            String[] arrResult = this.memberLevelService.arrContrast(arr, arrOne);     

            for(int i=0;i<arrResult.length;i++){
                if(sorts==Integer.parseInt(arrResult[i])){
                    flag = false;
                }
            }

            //              for (int i = 0; i < arr.length; i++) {
            ////                    System.out.println("arr[]--------"+arr[i]);
            //                  /*
            //                   * 3.當sorts等於數據庫中所有的sorts值時 1 (1,3,5)
            //                   */
            //                  if (sorts == Integer.parseInt(arr[i])) {//當數據庫中已經有了該等級的sort, flag=false
            //                      flag = false;
            //                      break;
            //                  }
            //              }

            /*
             * 得到該數組中的最大,最小值
             */
            int nMax = Integer.parseInt(arr[0]);//最大值
            int nMin = Integer.parseInt(arr[0]);//最小值
            for (int i = 0; i < arr.length; i++) {
                if (nMin > Integer.parseInt(arr[i])) {
                    nMin = Integer.parseInt(arr[i]);
                }

                if (nMax < Integer.parseInt(arr[i])) {
                    nMax = Integer.parseInt(arr[i]);
                }
            }
            MemberLevel level2 = this.memberLevelService.getObjectBySort(shop_id, nMax);//通過店鋪ID 和排序號找到對象
            MemberLevel level3 = this.memberLevelService.getObjectBySort(shop_id, nMin);//通過店鋪ID 和排序號找到對象
            int limit_max1 = level2.getLimit_max();//所需累計最小值  (用最小值保存累計數值,最大值留着以後用)
            int limit_max2 = level3.getLimit_max();//所需累計最小值  (用最小值保存累計數值,最大值留着以後用)

            /*
             * 1.當sorts等於於數據庫中所有的sorts值時 (1,3,5) 5   
             */
            if (sorts >= nMax) {
                int max = Integer.parseInt(arr[0]);         //最大值    
                int second =Integer.parseInt(arr[1]);      //次大值   
                for(int i = 0;i < arr.length;i++)  
                {  
                    if(Integer.parseInt(arr[i]) > max)       //更新最大值和次大值   
                    {  
                        second = max;      
                        max = Integer.parseInt(arr[i]);  
                    }  
                    else if(Integer.parseInt(arr[i]) < max && Integer.parseInt(arr[i]) > second)  //更新次大值    
                    {  
                        second = Integer.parseInt(arr[i]);  //得到3的值
                    }  
                    MemberLevel level5 = this.memberLevelService.getObjectBySort(shop_id, second);//通過店鋪ID 和排序號找到對象
                    int limit_max5 = level5.getLimit_max();//所需累計最小值  (用最小值保存累計數值,最大值留着以後用)

                    if (limitMax <= limit_max5) {//不滿足條件,flag=false
                        flag = false;
                    }
                }
            }




            /*
             * 2.當sorts等於最小值數據庫中所有的sorts值時 1 (1,3,5)
             */
            if (sorts <= nMin) {

                int min = Integer.parseInt(arr[0]);         //最大值    
                int second =Integer.parseInt(arr[1]);      //次大值  
                if (min > second) {  //找到數組中第二小的值
                    min = Integer.parseInt(arr[1]);  
                    second = Integer.parseInt(arr[0]);  
                }  
                for (int i = 2; i < arr.length; ++ i) {  
                    if (Integer.parseInt(arr[i]) < min) {  
                        second = min;  
                        min = Integer.parseInt(arr[i]);  
                    } else if (Integer.parseInt(arr[i]) < second) {  
                        second = Integer.parseInt(arr[i]);  
                    }  
                }  
                MemberLevel level6 = this.memberLevelService.getObjectBySort(shop_id, second);//通過店鋪ID 和排序號找到對象
                int limit_max6 = level6.getLimit_max();//所需累計最小值  (用最小值保存累計數值,最大值留着以後用)


                if (limitMax >= limit_max6) {//不滿足條件,flag=false
                    flag = false;
                }
            }


            System.out.println("flag----------------------"+flag);

            /*
             * 4.當sorts在數據庫中能找到大於和小於它的值時 3 (1,2,3,4,5)
             */
            String str2 = "";
            String str3 = "";
            if (nMin < sorts && sorts < nMax) {
                for (int j = 0; j < arr.length; j++) {
                    int p1 = 0;
                    int p2 = 0;
                    /*
                     * sorts前面的數據組成數組
                     */
                    if (Integer.parseInt(arr[j])< sorts) {
                        str2 = str2 + arr[j] + ",";

                        p1++;

                    }


                    if (p1 > 1) {//sort之前的數字超過1個
                        str2 = str2.substring(0, str2.length() - 1);
                        System.out.println("str2----------"+str2);
                        String[] arr2 = str2.split(",");

                        int nMax2 = Integer.parseInt(arr[0]);
                        for (int i = 1; i < arr2.length; i++) {

                            if (nMax2 < Integer.parseInt(arr2[i])) {
                                nMax2 = Integer.parseInt(arr2[i]);
                            }
                        }


                        MemberLevel level7 = this.memberLevelService.getObjectBySort(shop_id, nMax2);
                        int limit_max7 = level7.getLimit_max();

                        if (limitMax < limit_max7) {
                            flag = false;
                        }

                    } else {//sort之前的數字只有1個

                        if (limitMax <= limit_max1) {
                            flag = false;
                        }
                    }

                    /*
                     * sorts後面的數據組成數組
                     */
                    if (Integer.parseInt(arr[j]) >sorts) {
                        str3 = str3 + arr[j] + ",";

                        p2++;

                    }

                    if (p2 > 1) {//sort之前的數字超過1個
                        str3 = str3.substring(0, str3.length() - 1);
                        System.out.println("str3----------"+str3);

                        String[] arr2 = str3.split(",");

                        int nMax3 = Integer.parseInt(arr[0]);
                        for (int i = 1; i < arr2.length; i++) {

                            if (nMax3 < Integer.parseInt(arr2[i])) {
                                nMax3= Integer.parseInt(arr2[i]);
                            }
                        }

                        MemberLevel level8 = this.memberLevelService.getObjectBySort(shop_id, nMax3);
                        int limit_max8 = level8.getLimit_max();

                        if (limitMax >= limit_max8) {
                            flag = false;
                        }

                    } else {//sort之後只有一個數

                        if (limitMax >=limit_max2) {
                            flag = false;
                        }
                    }

                }


            }

        }

        if(!flag){//flag狀態用於判斷
            str = "no";
        }       
        return str;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章