I am creating a calculator to calculate shipping costs. The code goes something like this:
class ShippingCalc { public static void main(String[] args) { int weight = 30; if (weight < 10) { System.out.println("Shipping costs $1."); } else if (weight < 20) { System.out.println("Shipping costs $2."); } else { System.out.println("Shipping costs $3."); } } } This is all great but I want to create a calculator that can calculate based on already set values. For example, something that says:
if (weight < 250) { // print("Shipping cost is $1); } else if (weight < 499) { // print("Shipping cost is $2); } else if (weight < 749) { // print...etc and it keeps going This will be based on user input that is why I don't want to already have any constraints like above. Is it possible to make such a calculator in Java that no matter how much the weight, it calculates the shipping costs appropriately and gives out the answer.
If yes, then how do I go about it?
...etcbit. Show the pattern fully please. And I have made no assumptions about this being homework as my recommendations are valid for homework or home work (coming from another hobbiest).