Skip to content

Instantly share code, notes, and snippets.

@dbousamra
Created March 12, 2013 15:31
Show Gist options
  • Save dbousamra/5143895 to your computer and use it in GitHub Desktop.
Save dbousamra/5143895 to your computer and use it in GitHub Desktop.
package com.trailblazers.freewheelers.service;
import com.google.common.collect.ImmutableMap;
import java.math.BigDecimal;
import java.util.Map;
public class TaxCalculator {
// Only UK and Ireland have VAT.
private static final BigDecimal EXCLUDED_VAT_RATE = new BigDecimal(0);
// All countries besides France, Germany, Spain have a fixed duty rate of 5%
private static final BigDecimal EXCLUDED_DUTY_RATE = new BigDecimal(0.05);
private static final Map<String, BigDecimal> vatRates = ImmutableMap.of(
"Ireland", new BigDecimal(0.18),
"United Kingdom", new BigDecimal(0.18)
);
private static final Map<String, BigDecimal> dutyRates = ImmutableMap.of(
"France", new BigDecimal(0.07),
"Germany", new BigDecimal(0.054),
"Spain", new BigDecimal(0.09)
);
public BigDecimal calculateTaxOnItem(BigDecimal price, String country) {
return calculateVATOnItem(price, country).add(calculateDutyOnItem(price, country));
}
public BigDecimal calculateVATOnItem(BigDecimal price, String country) {
BigDecimal rate = EXCLUDED_VAT_RATE;
if (vatRates.containsKey(country))
rate = vatRates.get(country);
return price.multiply(rate).setScale(2, BigDecimal.ROUND_HALF_UP);
}
public BigDecimal calculateDutyOnItem(BigDecimal price, String country) {
BigDecimal rate = getExcludedDutyRate(country);
if (dutyRates.containsKey(country))
rate = dutyRates.get(country);
return price.multiply(rate).setScale(2, BigDecimal.ROUND_HALF_UP);
}
private BigDecimal getExcludedDutyRate(String country) {
if (vatRates.containsKey(country))
return new BigDecimal(0);
else
return EXCLUDED_DUTY_RATE;
}
public static String getTaxType(String country) {
if (vatRates.keySet().contains(country)) {
return "VAT";
} else {
return "Duty";
}
}
}
package unit.com.trailblazers.freewheelers.service;
import com.trailblazers.freewheelers.service.TaxCalculator;
import org.junit.Before;
import org.junit.Test;
import java.math.BigDecimal;
import static org.junit.Assert.assertEquals;
public class TaxCalculatorTest {
private TaxCalculator taxCalculator;
private BigDecimal price = new BigDecimal(10.00);
@Before
public void setup()
{
taxCalculator = new TaxCalculator();
}
@Test
public void testCalculatedVATForIncludedCountries() throws Exception {
assertVATAmountEquals(1.8, price, "United Kingdom");
assertVATAmountEquals(1.8, price, "Ireland");
}
@Test
public void testCalculatedVATForExcludedCountry() throws Exception {
assertVATAmountEquals(0, price, "France");
}
@Test
public void testCalculatedDutyForIncludedCountries() throws Exception {
assertDutyAmountEquals(0.7, price, "France");
assertDutyAmountEquals(0.54, price, "Germany");
}
@Test
public void testCalculatedDutyForExcludedCountries() throws Exception {
assertDutyAmountEquals(0, price, "United Kingdom");
}
@Test
public void testStringOutputIsToTwoDecimalPlaces() throws Exception {
assertEquals("1.80",
taxCalculator.calculateTaxOnItem(price, "United Kingdom").toString());
}
@Test
public void testDutyAndVATTogether() throws Exception {
assertTaxAmountEquals(0.54, price, "Germany");
assertTaxAmountEquals(0.7, price, "France");
assertTaxAmountEquals(1.8, price, "Ireland");
assertTaxAmountEquals(1.8, price, "United Kingdom");
}
@Test
public void testDutyTaxForCountriesOtherThanUK() throws Exception {
assertTaxAmountEquals(0.5, price, "Austria");
assertTaxAmountEquals(0.5, price, "Poland");
assertTaxAmountEquals(0.5, price, "Switzerland");
}
private void assertVATAmountEquals(double amount, BigDecimal price, String country) {
assertEquals(new BigDecimal(amount).setScale(2, BigDecimal.ROUND_HALF_UP),
taxCalculator.calculateVATOnItem(price, country));
}
private void assertDutyAmountEquals(double amount, BigDecimal price, String country) {
assertEquals(new BigDecimal(amount).setScale(2, BigDecimal.ROUND_HALF_UP),
taxCalculator.calculateDutyOnItem(price, country));
}
private void assertTaxAmountEquals(double amount, BigDecimal price, String country) {
assertEquals(new BigDecimal(amount).setScale(2, BigDecimal.ROUND_HALF_UP),
taxCalculator.calculateTaxOnItem(price, country));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment