Templates by BIGtheme NET

Java Anti Patterns

Description :
There are many anti patterns in java, one of them is calculations and conversions to the weights.
Use Case :
Before instructing to Che (Container Handling Equipment, like STRADDLE, RMG, RTG, Quay-Crane),
We need to check, whether this Che is lift capacity is greater than the container
or containers(In case of twins) weight.

CheCapacityCheck.java


import javax.measure.quantity.Mass;
import javax.measure.unit.Unit;

import java.util.Arrays;
import java.util.List;

import org.jscience.physics.amount.Amount;

import static javax.measure.unit.SI.HECTO;
import static javax.measure.unit.SI.KILOGRAM;

public class CheCapacityCheck {

    public static void main(String[] args) {
        final Amount cheCapacity = Amount.valueOf(cheCapacityInLong, _unit);

        //Create two containers with weights
        Container c1  = new Container("C1", new Long(500));
        Container c2  = new Container("C2", new Long(400));

        //Caluclate total containers weights.
        Amount totalWeight = getTotalContainerWeight(Arrays.asList(c1, c2));

        System.out.println("Containers Total weight is: " + totalWeight );
        isCheIsLiftCapable( cheCapacity, totalWeight );

        //Change Second container wight
        c2.setWeight(new Long(600));
        totalWeight = getTotalContainerWeight(Arrays.asList(c1, c2));

        System.out.println("Containers Total weight is: " + totalWeight );
        isCheIsLiftCapable( cheCapacity, totalWeight );
    }

    static void isCheIsLiftCapable( final Amount inCheCapacity, final Amount inTotalWeight){
        if( inTotalWeight.isGreaterThan(inCheCapacity) ){
            System.out.println("*******Che is not Lift Capable *************");
        }else{
            System.out.println("*******Che is Lift Capable *************");
        }
    }

    static Amount getTotalContainerWeight(final List inContainerList){
        Amount totalWeight = Amount.valueOf(0, _unit);
        for( Container c : inContainerList){
            final Amount unitWeight = Amount.valueOf( c.getWeight(), _unit);
            totalWeight = totalWeight.plus(unitWeight);
        }
        return totalWeight;
    }

    public static class Container {
        private String containerId;
        private Long weight;

        public Container(final String inContainerId, final Long inweight) {
            containerId = inContainerId;
            weight = inweight;
        }

        public String getContainerId() {
            return containerId;
        }

        public Long getWeight() {
            return weight;
        }

        public void setWeight( final Long inweight ) {
            weight = inweight;
        }
    }

    private static final Long cheCapacityInLong = new Long(1000);
    private static final Unit _unit = HECTO(KILOGRAM);
}

Out Put:
Containers Total weight is: 900 kg*100
*******Che is Lift Capable *************
Containers Total weight is: 1100 kg*100
*******Che is not Lift Capable *************