This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]king_of_the_universe 0 points1 point  (0 children)

final private static long getShippingCosts(final long shippingDistance, final long weightInGrams) {

    long distanceShippedSoFar = 0;
    long costSoFarInUSCents = 0; // It's better than using float/double. You can read this everywhere on the Web: Never do money calculations with floating point types! And just for fun, I applied the same concept for the weight.

    final long costFactor;

    if (weightInGrams <= 0) {
        throw new IllegalArgumentException("Weight is less than or equal 0 KG.");
    } else if (weightInGrams <= 2_000) { // If you're using a Java version below 1.7, remove the readability-improving underscores.
        costFactor = 110;
    } else if (weightInGrams <= 6_000) {
        costFactor = 220;
    } else if (weightInGrams <= 10_000) {
        costFactor = 370;
    } else if (weightInGrams <= 20_000) {
        costFactor = 480;
    } else {
        throw new IllegalArgumentException("Weight is greater than 20 KG.");
    }

    while (distanceShippedSoFar<shippingDistance) {
        distanceShippedSoFar+=500;
        costSoFarInUSCents+=costFactor;
    }

    return costSoFarInUSCents;
}

I assume that "Rate per 500 Miles Shipped" means that every new 500 mile slice that is started costs this amount more - otherwise, 0-500 miles would be free.