lundi 20 novembre 2017

Calculation of Shipping Costs in Java

I am trying to find a solution for my problem for an assignment.

Task 9: Calculation of the package postage:

Calculator.java

By clicking on the "Calculate" button, the wrong postage is currently being calculated. Find the place that is called when someone clicks on the button. Correct the calculation so that the following postage is calculated:

• Package up to 30x30x15 and up to 1kg: 3,89 EUR

• Package up to 60x30x15 and up to 2kg: 4,39 EUR

• Package up to 120x60x60 and max. Belt measure ( = L + 2xB + 2xH) <= 300cm and up to 5kg: 5,99 EUR

• Package up to 120x60x60 and max. Belt measure ( = L + 2xB + 2xH) <= 300cm and up to 10kg: 7,99 EUR

• Package up to 120x60x60 and up to 31,5kg: 14,99 EUR

The code already given, I have to change some parts, so that it returns the correct shipping price. It even works but My Problem is the last part.

"• Package up to 120x60x60 and up to 31,5kg: 14,99 EUR"

I don't know how to do it. I tried with "else { shippingcosts = 00.00 } " but that sounds stupid :) It works, that isn't the problem but I have to do it right. Right now the else statement works with 50kg but it shouldn't.

I am not sure, what do to anymore. Can someone here help me please?

public class Calculator {

public double calcShippingCosts(Packet pack) {
    double shippingCosts;

    if ((pack.length <= 300) && (pack.width <= 300) && (pack.height <= 150) && (pack.weight <= 1000)) {

        shippingCosts = 3.89;

    } else if ((pack.length <= 600) && (pack.width <= 300) && (pack.height <= 150) && (pack.weight <= 2000)) {

        shippingCosts = 4.39;

    } else if ((pack.length <= 1200) && (pack.width <= 600) && (pack.height <= 600 ) && ((pack.length + (2 * pack.width) + (2 * pack.height)) <= 3000) && (pack.weight <= 5000)) {

        shippingCosts = 5.99;

    } else if ((pack.length <= 1200) && (pack.width <= 600) && (pack.height <= 600) && ((pack.length + (2 * pack.width) + (2 * pack.height)) <= 3000) && pack.weight >= 5000 && pack.weight <= 10000) {

        shippingCosts = 7.99;

    } else {

        shippingCosts = 14.99;
    }
    return shippingCosts;
}

}

btw. A Software Engineer told me to use Else If here. First there were only If's

If I did something wrong, than I apologize. This is my first time using stackoverflow.

Aucun commentaire:

Enregistrer un commentaire