Reputation:
So I am doing a practice test, and this is the question:
Write a Java method that takes one double value pay as argument and prints to the standard output the tax rate corresponding to the value of pay entered. Use the table below to determine the tax rate. (6 points)
If pay more than 100,000, tax rate is 40%
If pay more than 60,000 and less than or equal to 100,000, tax rate is 30%
If pay more than 30,000 and less than or equal to 60,000, tax rate is 20%
If pay more than 15,000 and less than or equal to 30,000, tax rate is 10%
If pay more than 5,000 and less than or equal to 15,000, tax rate is 5%
If pay is less than or equal to 5,000, tax rate is 0
This is the code I Wrote, which works:
public class Two {
public static void main(String[] args){
double j= Double.parseDouble(args[0]);
Two.taxrate(j);}
public static void taxrate(double pay)
{
if (pay> 100000) {System.out.println(pay*.4);}
else if (pay>60000) {System.out.println(pay*.3);}
else if (pay>30000) {System.out.println(pay*.2);}
else if (pay>15000) {System.out.println (pay*.1);}
else if (pay>5000) {System.out.println(pay*.05);}
else System.out.println(0.0);
}}
What I want to know is this: is this the best way to answer this question? We have learned how to design data types, and this question seems easy. I feel I'm overlooking something.
Upvotes: 2
Views: 464
Reputation: 2399
You could always go about it something like this. It avoids the massive if block, and also adds nicer structure.
import java.util.HashMap;
import java.util.Map;
public class Two {
private static Map<Double, Double> rates = new HashMap<Double, Double>();
static {
rates.put(100000d, 0.4d);
rates.put(60000d, 0.3d);
rates.put(30000d, 0.2d);
rates.put(15000d, 0.1d);
rates.put(5000d, 0.05d);
}
public static void main(String[] args) {
double j = Double.parseDouble(args[0]);
System.out.println(rate(j));
}
private static double rate(double pay) {
for(Double d: rates.keySet())
if(pay > d) return pay * rates.get(d);
return 0.0d;
}
}
Upvotes: 1
Reputation: 310850
Use a table that gives you lower and/or upper bound and multiplier. Then just scan the table until you find the appropriate row and use that multiplier.
In a real application the table would be in a database, as the powers that be can change the ranges, number of ranges, and multipliers any time.
Upvotes: 3
Reputation: 22332
As EJP suggested, you want a "table" of salary to tax rate. In a full scale application, this information will likely be stored in a database rather than in memory, but going for something in-between statically defined and a full scale database would be to use a Map
. Specifically, using a NavigableMap
will allow very simple access to such calculations:
NavigableMap<Double, Double> payToTaxRate = new TreeMap<Double, Double>();
// e.g., loaded from a file:
payToTaxRate.put(100000, 0.4);
payToTaxRate.put(60000, 0.3);
payToTaxRate.put(30000, 0.2);
payToTaxRate.put(15000, 0.1);
payToTaxRate.put(5000, 0.05);
payToTaxRate.put(Double.MIN_VALUE, 0);
System.out.prinln(pay * payToTaxRate.floorEntry(pay).getValue());
Naturally, this is overkill for an assignment and the if
statements would likely be faster than the overhead of the map. However, in a world where tax brackets are literally added from year-to-year (not so often, thankfully), a more dynamic approach like the above is helpful.
Upvotes: 3