package org.xander.katas;

public class CalculateLength {
    private static final int bigBrickLength = 5;
    private static final int littleBrickLength = 1;
    private boolean result;

    boolean measure(int totalLength, int bigBrickNumber, int littleBrickNumber) {
        if (totalLength <= 0) {
            return false;
        }
        if (bigBrickNumber == 0 && littleBrickNumber == 0) {
            return false;
        }


        if (bigBrickNumber == 0) {
            return oneOfNumbersIsZero(totalLength, littleBrickNumber, littleBrickLength);
        }

        if (littleBrickNumber == 0) {
            return oneOfNumbersIsZero(totalLength, bigBrickNumber, bigBrickLength);
        }

        if (totalLength > bigBrickNumber * bigBrickLength + littleBrickNumber * littleBrickLength) {
            result = false;
        } else if (totalLength == bigBrickNumber * bigBrickLength + littleBrickNumber * littleBrickLength) {
            result = true;
        } else {
            int leftOverForSmallBricks = calculateLeftOverForSmallBricks(totalLength, bigBrickNumber);

            if ((totalLength <= bigBrickNumber * bigBrickLength && totalLength >= littleBrickNumber * littleBrickLength)
               || (totalLength >= bigBrickNumber * bigBrickLength && totalLength >= littleBrickNumber * littleBrickLength)) {

                if (!result) {
                    if (leftOverForSmallBricks < littleBrickLength * littleBrickNumber) {
                        result = leftOverBricksCouldBeBuildFromExistingOnes(leftOverForSmallBricks);
                    } else result = leftOverForSmallBricks == littleBrickLength * littleBrickNumber;
                }
            }

            if ((totalLength <= bigBrickNumber * bigBrickLength && totalLength <= littleBrickNumber * littleBrickLength)
               || (totalLength >= bigBrickNumber * bigBrickLength && totalLength <= littleBrickNumber * littleBrickLength)) {

                if(!result){
                    result = leftOverBricksCouldBeBuildFromExistingOnes(leftOverForSmallBricks);
                }
            }
        }
        return result;
    }

    private int calculateLeftOverForSmallBricks(int totalLength, int bigBrickNumber) {
        int numberOfBricks = 0;
        if (totalLength % bigBrickLength == 0) {
            int requiredNumberOfBigBricks = totalLength / bigBrickLength;
            if (requiredNumberOfBigBricks == bigBrickNumber) {
                result = true;
            } else if (requiredNumberOfBigBricks < bigBrickNumber) {
                numberOfBricks = requiredNumberOfBigBricks;
            }
        } else {
            int leftOverOfBigBricks = totalLength % bigBrickLength;
            int numberOfBigBricksNeeded = (totalLength - leftOverOfBigBricks) / bigBrickLength;
            if (numberOfBigBricksNeeded <= bigBrickNumber) {
                numberOfBricks = numberOfBigBricksNeeded;
            }
        }
        return totalLength - numberOfBricks * bigBrickLength;
    }

    private boolean leftOverBricksCouldBeBuildFromExistingOnes(int leftOverForSmallBricks) {
        return leftOverForSmallBricks % littleBrickLength == 0;
    }

    private Boolean oneOfNumbersIsZero(int totalLength, int brickNumber, int brickLength) {
        if (totalLength == brickNumber * brickLength) {
            return true;
        }
        if (totalLength > brickNumber * brickLength) {
            return false;
        }

        if (totalLength < brickNumber * brickLength) {
            int leftOverOfBricks = brickNumber * brickLength - totalLength;
            if (totalLength % brickLength == 0) {
                int requiredNumber = totalLength / brickLength;
                if (requiredNumber <= brickNumber) {
                    return true;
                }
            }
        }
        return false;
    }
}

@@ -51,16 +51,22 @@
 ength %7B%0A
+    //
     priv
@@ -98,20 +98,16 @@
 rick
-Length
  = 5;%0A
+//
     
@@ -146,47 +146,239 @@
 rick
-Length = 1;%0A    private boolean result;
+ = 1;%0A    private int littleBrick;%0A    private int bigBrick;%0A    private boolean result;%0A%0A    public CalculateLength(int bigBrick, int littleBrick) %7B%0A        this.bigBrick = bigBrick; %0A        this.littleBrick = littleBrick; %0A    %7D
 %0A%0A  
@@ -623,17 +623,16 @@
      %7D%0A%0A
-%0A
         
@@ -735,30 +735,24 @@
  littleBrick
-Length
 );%0A        %7D
@@ -866,22 +866,16 @@
 bigBrick
-Length
 );%0A     
@@ -923,38 +923,32 @@
 umber * bigBrick
-Length
  + littleBrickNu
@@ -957,38 +957,32 @@
 er * littleBrick
-Length
 ) %7B%0A            
@@ -1056,22 +1056,16 @@
 bigBrick
-Length
  + littl
@@ -1086,30 +1086,24 @@
  littleBrick
-Length
 ) %7B%0A        
@@ -1291,38 +1291,32 @@
 umber * bigBrick
-Length
  && totalLength 
@@ -1341,38 +1341,32 @@
 er * littleBrick
-Length
 )%0A              
@@ -1402,38 +1402,32 @@
 umber * bigBrick
-Length
  && totalLength 
@@ -1452,38 +1452,32 @@
 er * littleBrick
-Length
 )) %7B%0A%0A          
@@ -1549,38 +1549,32 @@
 ks %3C littleBrick
-Length
  * littleBrickNu
@@ -1709,16 +1709,42 @@
  %7D else 
+%7B%0A                        
 result =
@@ -1781,22 +1781,16 @@
 tleBrick
-Length
  * littl
@@ -1803,16 +1803,38 @@
 Number;%0A
+                    %7D%0A
         
@@ -1907,38 +1907,32 @@
 umber * bigBrick
-Length
  && totalLength 
@@ -1965,22 +1965,16 @@
 tleBrick
-Length
 )%0A      
@@ -2026,22 +2026,16 @@
 bigBrick
-Length
  && tota
@@ -2076,22 +2076,16 @@
 tleBrick
-Length
 )) %7B%0A%0A  
@@ -2100,16 +2100,17 @@
       if
+ 
 (!result
@@ -2110,16 +2110,17 @@
 !result)
+ 
 %7B%0A      
@@ -2431,30 +2431,24 @@
 h %25 bigBrick
-Length
  == 0) %7B%0A   
@@ -2502,38 +2502,32 @@
 ength / bigBrick
-Length
 ;%0A            if
@@ -2818,38 +2818,32 @@
 ength %25 bigBrick
-Length
 ;%0A            in
@@ -2912,30 +2912,24 @@
 ) / bigBrick
-Length
 ;%0A          
@@ -3111,30 +3111,24 @@
 s * bigBrick
-Length
 ;%0A    %7D%0A%0A   
@@ -3264,30 +3264,24 @@
  littleBrick
-Length
  == 0;%0A    %7D

Update CalculateLength.java