0

so i have these two classes called shipment and insurance, one calculates the price for shipping and the other adds the insurance. The shipment logic is working just fine but for some reason the if statements in Insurance class is not working i dont know whats going on. The output for the Insurance cost is always 2.45. why does it do this?

Shipment class:

 package theshipment; public class Shipment extends Main { protected double weight = Double.parseDouble(savedArgs[1]); protected double shippingCost; protected double methodCost; protected String method = savedArgs[2]; public void calculateShippingCost(){ if (weight<=10||weight>=1){ if (method.equalsIgnoreCase("T")) methodCost = weight * 3.00; else if (method.equalsIgnoreCase("A")) methodCost = weight * 4.00; else if (method.equalsIgnoreCase("M")) methodCost = weight * 2.00; else{} }else if (weight<=20||weight>=10.1){ if (method.equalsIgnoreCase("T")) methodCost = weight * 2.45; else if (method.equalsIgnoreCase("A")) methodCost = weight * 3.00; else if (method.equalsIgnoreCase("M")) methodCost = weight * 1.75; else{} }else if (weight>20){ if (method.equalsIgnoreCase("T")) methodCost = weight * 1.95; else if (method.equalsIgnoreCase("A")) methodCost = weight * 2.50; else if (method.equalsIgnoreCase("M")) methodCost = weight * 1.55; else{} }else{} } } 

And the insurance class:

package theshipment; public class Insurance extends Shipment{ private void calculateInsurance(){ if (methodCost<=10.0||methodCost>=0.0) shippingCost = methodCost + 2.45; else if(methodCost<=30.0||methodCost>=10.1) shippingCost = methodCost + 3.95; else if(methodCost>=30.01) shippingCost = methodCost + 5.55; else{} } public void run(){ calculateShippingCost(); calculateInsurance(); } public String displayOrder(){ return ("Method cost: " + methodCost + " " + "Insurance cost: " + (shippingCost-methodCost) + " Total shipping cost: " + shippingCost); } } 
1
  • 1
    The easiest way to fix this is with the debugger. Get used to using the debugger in your IDE if you have one and also consider writing some unit tests JUnit Commented Feb 18, 2015 at 15:57

1 Answer 1

10

methodCost<=10.0||methodCost>=0.0 that means it will be true when methodCost will be bigger than 0 OR smaller than 10,

I believe you want range not all numbers change it to methodCost<=10.0 && methodCost>=0.0

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.