Home > Mobile >  Is there a code on java that allows for two if statements with one independent statement for Shippin
Is there a code on java that allows for two if statements with one independent statement for Shippin

Time:10-12

I am very new to java and have been stuck on a program that I've been trying to create. For background knowledge purposes, the program is for a company called "Ship It" which is a package shipping company. The user enters the weight of the package, and the distance it will travel. Depending on the weight, the company charges a fee per 200 miles.

0 < weight <= 3 pounds $1.00 charge

3 < weight <= 6 pounds $2.00 charge

6 < weight <= 10 pounds $3.00 charge

10 < weight <= 20 pounds $4.00 charge

20 < weight <= 30 pounds $5.00 charge

So far, this is the code I have:

public static void main(String[] args) 
{   
    Scanner kb = new Scanner (System.in);
    
    //Variables
    double costWithoutCharge = 0, weight, distance = 0;
    
    //Introduction to ShipIt
    System.out.print("\t\t******Welcome to ShipIt******\n");
    System.out.print("***We are a low-charge, secure shipping company for packages"  
                        "up to 30 pounds***");

    //User Enters Weight of Package
    System.out.print("\n\nEnter the weight of the package (1.0 - 30.0 pounds): ");
    weight = kb.nextDouble();
    System.out.print("");
    

    // User Enters distance the package will travel
    System.out.print("Enter the miles to the destination (1 - 2000 miles): ");
    distance = kb.nextInt();
    System.out.print("");
    
    //Weight If-else Statement
    if (weight >30.0)
        System.out.println ("\nSorry, you have entered invalid data - program terminated");
    if (weight >30.0)
        System.exit((int) weight);
    
    //Distance Restriction if-else
    if (distance >2000)
        System.out.println ("\nSorry, you have entered invalid data - program terminated");
    if (distance >2000)
        System.exit((int) distance);    
    
    costWithoutCharge = distance / 200;
    
    //If else 
    if (weight <0 || weight <=3)
    {
        System.out.println ("The cost to ship the package is: "  "$"   (costWithoutCharge)*1.00);
    }

    else if (weight <3 || weight <= 6)
    {
        System.out.println ("The cost to ship the package is: "  "$"   (costWithoutCharge)*2.00);
    }

    else if (weight <6 || weight <= 10)
    {
        System.out.println ("The cost to ship the package is: "  "$"   (costWithoutCharge)*3.00);
    }

    else if (weight <10 || weight <= 20)
    {
        System.out.println ("The cost to ship the package is: "  "$"   (costWithoutCharge)*4.00);
    }
    else {
        System.out.println ("The cost to ship the package is: "  "$"   (costWithoutCharge)*5.00);
    }

    kb.close();

}

}

As of now, if I put a value like 1001, the cost to ship is $15.015, but it should be $18 since the charge is multiplied per 200 miles. I am on the fence if I need to do a new equation for the charge per 200 miles dilemma or if it can be supported with another if-statement?

I feel as though I have tried everything but I can't seem to solve this ): I am in dire need of help! Please!

CodePudding user response:

First

if (weight <0 || weight <=3)

should be

if (0 < weight && weight <=3)

However the code should be easier to maintain, use a table of limits:

double[][] weightsAndCharges = {
    {3, 1.00},
    {6, 2.00},
    {10, 3.00},
    {20, 4.00},
    {30, 5.00}
};

double charge = 10.00;
for (double[] weightAndCharge : weightAndCharges) {
    if (weight <= weightAndCharge[0]) {
        charge = weightAndCharge[1];
        break;
    }
}
System.out.printf("The cost to ship the package is: $%0.2f%n", charge*distanceRatio);

CodePudding user response:

The answer is some basic math.

What you are thinking of is a combinatorial explosion: If you layer a whole batch of if/elseif statements inside each of your weight if statements for e.g. if (miles < 200) ... else if (miles >= 200 && miles < 400) - then think of it in dimensions: You have the 'miles' dimension which currently is adding 10 options (1-200, 200-399, 400-599, etc), the weight dimension which adds 5.

The amount of ifs you'd need here is then A*B: 50 ifs.

That's a ton, and clearly not what you want.

Math to the rescue!

You really just want to calculate costPerSegment * segments.

Calculate those 2 values individually, and now it's just A B: 15 ifs. Given that you can actually use math itself to turn the miles number into the # of segments you need to charge (it's just division by 200 for the miles part, no lookup table involved), we're down to 5 ifs.

Note also your code is buggy. Your weight if statement have their > and < reversed. But the else if hides the problem. I fixed that problem in the snippet below.

double costPerSegment;

if (weight <=3) {
  costPerSegment = 1.0;
} else if (weight <= 6) {
  costPerSegment = 2.0;
} else if (weight <= 10) {
  costPerSegment = 3.0;
} else if (weight <= 20) {
  costPerSegment = 4.0;
} else {
  costPerSegment = 5.0;
}

// Casting a positive double to an int automatically rounds down.
int segments = (int) miles / 200;

double cost = costPerSegment * segments;

CodePudding user response:

The weight is missing from your example. it sounds like in your example you have:

  • distance 1001
  • weight between 6 and 10, resulting in a $3 charge per "beginning 200 miles"

From your code, 15.015 gets returned.

It appears you want to calculate the "beginning 200 miles", so you could achieve that by rounding up:

costWithoutCharge = Math.ceil( distance / 200 );

On another note, you may want to remove the common parts from your if/then/else block. That is, only perform the calculation but not the System.out.println inside each clause.

CodePudding user response:

This line is causing the problem for input distance 1001

costWithoutCharge = distance / 200; // result = 5,005

As far as I understood you want to have here just 5

So the simpliest solution would be to declare costWithoutCharge as int and than

costWithoutCharge = (int) distance / 200; // result = 5

Or if you want to keep costWithoutCharge as double you can use Math lib to round it

costWithoutCharge = Math.round(distance / 200); // result = 5

CodePudding user response:

The logic seems ok. If you're taking a weight=10 and distance=1001, it's obvious that it would be (1001/200) = 5.005 and then (5.005*3) = 15.015. Though your code is clumsy , you have misplaced ">", "<" signs and also you don't need to mention print statements multiple times inside, just put a general one for all cases. Also, incase you want round-off the value it won't be 18 in any case if you take 1001 as distance. Anyways if you want to round it off, you can just typecast it to int.

double charge;

if (weight <=3) {
  charge = 1.0;
} else if (weight >3 || weight <= 6) {
  charge = 2.0;
} else if (weight >6 || weight <= 10) {
  charge = 3.0;
} else if (weight >10 || weight <= 20) {
  charge = 4.0;
} else {
  charge = 5.0;
}


costWithoutCharge = (int) distance / 200; // Casting double to int

double total_cost = costWithoutCharge * charge;
  • Related