Skip to content

Commit

Permalink
Remove the reference to the enum. So it just references the enum valu…
Browse files Browse the repository at this point in the history
…e directly to help with readability

Fix linting issues
  • Loading branch information
georgeherby committed Aug 29, 2019
1 parent 6c20968 commit 7a4321d
Show file tree
Hide file tree
Showing 26 changed files with 236 additions and 199 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ To use this library you need to create an instance of `Calculator`, passing in t
val calculator = Calculator(
taxCodeString = "1250L", // Required
userEnteredWages = 20000.0, // Required
payPeriod = PayPeriod.YEARLY, // Required
payPeriod = YEARLY, // Required
pensionAge = false, // Optional (Default: false)
hoursPerWeek = null, // Optional (Default: null)
taxYear = 2019 // Optional (Default: Current Tax Year)
Expand Down
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ bintray {
target "**/*.kt"
ktlint()
licenseHeader license

}
}
}
Expand Down
48 changes: 26 additions & 22 deletions src/commonMain/kotlin/calculator/Calculator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ import calculator.model.BandBreakdown
import calculator.model.CalculatorResponse
import calculator.model.CalculatorResponsePayPeriod
import calculator.model.Country
import calculator.model.Country.ENGLAND
import calculator.model.PayPeriod
import calculator.model.PayPeriod.FOUR_WEEKLY
import calculator.model.PayPeriod.MONTHLY
import calculator.model.PayPeriod.WEEKLY
import calculator.model.PayPeriod.YEARLY
import calculator.model.bands.Band
import calculator.model.bands.EmployeeNIBands
import calculator.model.bands.EmployerNIBands
Expand Down Expand Up @@ -128,27 +132,27 @@ class Calculator(
country = taxCode.country,
isKCode = taxCode is KTaxCode,
weekly = CalculatorResponsePayPeriod(
payPeriod = PayPeriod.WEEKLY,
taxToPay = taxPayable.convertAmountFromYearlyToPayPeriod(PayPeriod.WEEKLY),
employeesNI = employeesNI.convertAmountFromYearlyToPayPeriod(PayPeriod.WEEKLY),
employersNI = employersNI.convertAmountFromYearlyToPayPeriod(PayPeriod.WEEKLY),
wages = yearlyWages.convertAmountFromYearlyToPayPeriod(PayPeriod.WEEKLY),
taxBreakdown = bandBreakdown.convertListOfBandBreakdownForPayPeriod(PayPeriod.WEEKLY),
taxFree = adjustTaxBands(taxBands)[0].upper.convertAmountFromYearlyToPayPeriod(PayPeriod.WEEKLY),
payPeriod = WEEKLY,
taxToPay = taxPayable.convertAmountFromYearlyToPayPeriod(WEEKLY),
employeesNI = employeesNI.convertAmountFromYearlyToPayPeriod(WEEKLY),
employersNI = employersNI.convertAmountFromYearlyToPayPeriod(WEEKLY),
wages = yearlyWages.convertAmountFromYearlyToPayPeriod(WEEKLY),
taxBreakdown = bandBreakdown.convertListOfBandBreakdownForPayPeriod(WEEKLY),
taxFree = adjustTaxBands(taxBands)[0].upper.convertAmountFromYearlyToPayPeriod(WEEKLY),
kCodeAdjustment = if (taxCode is KTaxCode) {
taxCode.amountToAddToWages.convertAmountFromYearlyToPayPeriod(PayPeriod.WEEKLY)
taxCode.amountToAddToWages.convertAmountFromYearlyToPayPeriod(WEEKLY)
} else null
),
fourWeekly = CalculatorResponsePayPeriod(
payPeriod = PayPeriod.FOUR_WEEKLY,
taxToPay = taxPayable.convertAmountFromYearlyToPayPeriod(PayPeriod.FOUR_WEEKLY),
employeesNI = employeesNI.convertAmountFromYearlyToPayPeriod(PayPeriod.FOUR_WEEKLY),
employersNI = employersNI.convertAmountFromYearlyToPayPeriod(PayPeriod.FOUR_WEEKLY),
wages = yearlyWages.convertAmountFromYearlyToPayPeriod(PayPeriod.FOUR_WEEKLY),
taxBreakdown = bandBreakdown.convertListOfBandBreakdownForPayPeriod(PayPeriod.FOUR_WEEKLY),
taxFree = adjustTaxBands(taxBands)[0].upper.convertAmountFromYearlyToPayPeriod(PayPeriod.FOUR_WEEKLY),
payPeriod = FOUR_WEEKLY,
taxToPay = taxPayable.convertAmountFromYearlyToPayPeriod(FOUR_WEEKLY),
employeesNI = employeesNI.convertAmountFromYearlyToPayPeriod(FOUR_WEEKLY),
employersNI = employersNI.convertAmountFromYearlyToPayPeriod(FOUR_WEEKLY),
wages = yearlyWages.convertAmountFromYearlyToPayPeriod(FOUR_WEEKLY),
taxBreakdown = bandBreakdown.convertListOfBandBreakdownForPayPeriod(FOUR_WEEKLY),
taxFree = adjustTaxBands(taxBands)[0].upper.convertAmountFromYearlyToPayPeriod(FOUR_WEEKLY),
kCodeAdjustment = if (taxCode is KTaxCode) {
taxCode.amountToAddToWages.convertAmountFromYearlyToPayPeriod(PayPeriod.FOUR_WEEKLY)
taxCode.amountToAddToWages.convertAmountFromYearlyToPayPeriod(FOUR_WEEKLY)
} else null
),
monthly = CalculatorResponsePayPeriod(
Expand All @@ -164,11 +168,11 @@ class Calculator(
} else null
),
yearly = CalculatorResponsePayPeriod(
payPeriod = PayPeriod.YEARLY,
taxToPay = taxPayable.convertAmountFromYearlyToPayPeriod(PayPeriod.YEARLY),
employeesNI = employeesNI.convertAmountFromYearlyToPayPeriod(PayPeriod.YEARLY),
employersNI = employersNI.convertAmountFromYearlyToPayPeriod(PayPeriod.YEARLY),
wages = yearlyWages.convertAmountFromYearlyToPayPeriod(PayPeriod.YEARLY),
payPeriod = YEARLY,
taxToPay = taxPayable.convertAmountFromYearlyToPayPeriod(YEARLY),
employeesNI = employeesNI.convertAmountFromYearlyToPayPeriod(YEARLY),
employersNI = employersNI.convertAmountFromYearlyToPayPeriod(YEARLY),
wages = yearlyWages.convertAmountFromYearlyToPayPeriod(YEARLY),
taxBreakdown = bandBreakdown,
taxFree = adjustTaxBands(taxBands)[0].upper,
kCodeAdjustment = if (taxCode is KTaxCode) taxCode.amountToAddToWages else null
Expand All @@ -179,7 +183,7 @@ class Calculator(
companion object {
fun getDefaultTaxCode() = "${(getDefaultTaxAllowance(TaxYear().currentTaxYear()) / 10)}L"

internal fun getDefaultTaxAllowance(taxYear: Int, country: Country = Country.ENGLAND) =
internal fun getDefaultTaxAllowance(taxYear: Int, country: Country = ENGLAND) =
TaxBands(country, taxYear).bands[0].upper.toInt()
}
}
23 changes: 14 additions & 9 deletions src/commonMain/kotlin/calculator/utils/Converters.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,25 @@ package calculator.utils

import calculator.model.BandBreakdown
import calculator.model.PayPeriod
import calculator.model.PayPeriod.FOUR_WEEKLY
import calculator.model.PayPeriod.HOURLY
import calculator.model.PayPeriod.MONTHLY
import calculator.model.PayPeriod.WEEKLY
import calculator.model.PayPeriod.YEARLY

internal fun Double.convertWageToYearly(
payPeriod: PayPeriod,
hoursPerWeek: Double? = null
): Double {
return when (payPeriod) {
PayPeriod.HOURLY -> {
HOURLY -> {
if (hoursPerWeek != null && hoursPerWeek > 0) this * hoursPerWeek * 52
else throw InvalidHours("The number of hours must be greater than 0 when PayPeriod is HOURLY")
}
PayPeriod.WEEKLY -> this * 52
PayPeriod.FOUR_WEEKLY -> this * 13
PayPeriod.MONTHLY -> this * 12
PayPeriod.YEARLY -> this
WEEKLY -> this * 52
FOUR_WEEKLY -> this * 13
MONTHLY -> this * 12
YEARLY -> this
}
}

Expand All @@ -46,10 +51,10 @@ internal fun Double.convertAmountFromYearlyToPayPeriod(
payPeriod: PayPeriod
): Double {
return when (payPeriod) {
PayPeriod.WEEKLY -> this / 52
PayPeriod.FOUR_WEEKLY -> this / 13
PayPeriod.MONTHLY -> this / 12
PayPeriod.YEARLY -> this
WEEKLY -> this / 52
FOUR_WEEKLY -> this / 13
MONTHLY -> this / 12
YEARLY -> this
else -> throw InvalidPayPeriod("$payPeriod is not supported")
}
}
12 changes: 8 additions & 4 deletions src/commonMain/kotlin/calculator/utils/String+Country.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@
package calculator.utils

import calculator.model.Country
import calculator.model.Country.ENGLAND
import calculator.model.Country.NONE
import calculator.model.Country.SCOTLAND
import calculator.model.Country.WALES

internal fun String.toCountry(): Country {
return when (this) {
"NT" -> Country.NONE
"NT" -> NONE
else -> when (this.first().toString()) {
"S" -> Country.SCOTLAND
"C" -> Country.WALES
else -> Country.ENGLAND
"S" -> SCOTLAND
"C" -> WALES
else -> ENGLAND
}
}
}
13 changes: 8 additions & 5 deletions src/commonMain/kotlin/calculator/utils/String+TaxCode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
*/
package calculator.utils

import calculator.model.Country
import calculator.model.Country.ENGLAND
import calculator.model.Country.NONE
import calculator.model.Country.SCOTLAND
import calculator.model.Country.WALES
import calculator.model.taxcodes.BR
import calculator.model.taxcodes.C0T
import calculator.model.taxcodes.CBR
Expand Down Expand Up @@ -57,10 +60,10 @@ internal fun String.toTaxCode(): TaxCode {
val noSpacesTaxCode = this.replace("\\s".toRegex(), "")

return when (noSpacesTaxCode.toCountry()) {
Country.SCOTLAND -> noSpacesTaxCode.matchScottishTaxCode()
Country.WALES -> noSpacesTaxCode.matchWelshTaxCode()
Country.ENGLAND -> noSpacesTaxCode.matchEnglishTaxCode()
Country.NONE -> {
SCOTLAND -> noSpacesTaxCode.matchScottishTaxCode()
WALES -> noSpacesTaxCode.matchWelshTaxCode()
ENGLAND -> noSpacesTaxCode.matchEnglishTaxCode()
NONE -> {
when (this) {
"NT" -> NTCode()
else -> throw InvalidTaxCode("$this is an invalid tax code")
Expand Down
3 changes: 1 addition & 2 deletions src/commonTest/kotlin/calculator/CalculatorTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
package calculator

import calculator.model.PayPeriod.HOURLY
import calculator.utils.InvalidHours
import kotlin.test.Test

import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import calculator.utils.InvalidHours

class CalculatorTests {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
package calculator.model

import calculator.Calculator
import calculator.model.Country.ENGLAND
import calculator.model.PayPeriod.MONTHLY
import calculator.model.PayPeriod.YEARLY
import kotlin.test.Test
import kotlin.test.assertEquals

Expand All @@ -24,7 +27,7 @@ class CalculatorResponseTests {
@Test
fun `Check Summation Of Total Deductions`() {
val response = CalculatorResponsePayPeriod(
payPeriod = PayPeriod.YEARLY,
payPeriod = YEARLY,
taxToPay = 100.0,
employeesNI = 200.0,
employersNI = 300.0,
Expand All @@ -46,14 +49,14 @@ class CalculatorResponseTests {
fun `Check Full Calculator Response`() {
val taxCode = "1250L"
val wages = 2000.00
val response = Calculator(taxCode, wages, payPeriod = PayPeriod.MONTHLY).run()
val response = Calculator(taxCode, wages, payPeriod = MONTHLY).run()

assertEquals(Country.ENGLAND, response.country)
assertEquals(ENGLAND, response.country)
assertEquals(false, response.isKCode)
assertEquals("1250L", response.taxCode)
// Year

assertEquals(PayPeriod.YEARLY, response.yearly.payPeriod)
assertEquals(YEARLY, response.yearly.payPeriod)
assertEquals(2298.2000000000003, response.yearly.taxToPay)
assertEquals(1844.1599999999999, response.yearly.employeesNI)
assertEquals(2120.784, response.yearly.employersNI)
Expand Down
18 changes: 10 additions & 8 deletions src/commonTest/kotlin/calculator/model/bands/TaxBandsTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
*/
package calculator.model.bands

import calculator.model.Country
import calculator.model.Country.ENGLAND
import calculator.model.Country.SCOTLAND
import calculator.model.Country.WALES
import calculator.utils.UnsupportedTaxYear
import kotlin.test.Test
import kotlin.test.assertEquals
Expand All @@ -25,13 +27,13 @@ class TaxBandsTests {

@Test
fun invalidYear() {
val exception = assertFailsWith<UnsupportedTaxYear> { TaxBands(Country.ENGLAND, 2017) }
val exception = assertFailsWith<UnsupportedTaxYear> { TaxBands(ENGLAND, 2017) }
assertEquals(exception.message, "2017")
}

@Test
fun bandsForEngland2019() {
val taxBand = TaxBands(Country.ENGLAND, 2019).bands[1]
val taxBand = TaxBands(ENGLAND, 2019).bands[1]
assertEquals(50000.00, taxBand.upper)
assertEquals(12509.00, taxBand.lower)
assertEquals(0.20, taxBand.percentageAsDecimal)
Expand All @@ -45,7 +47,7 @@ class TaxBandsTests {

@Test
fun bandsForWales2019() {
val taxBand = TaxBands(Country.WALES, 2019).bands[1]
val taxBand = TaxBands(WALES, 2019).bands[1]
assertEquals(50000.00, taxBand.upper)
assertEquals(12509.00, taxBand.lower)
assertEquals(0.20, taxBand.percentageAsDecimal)
Expand All @@ -59,7 +61,7 @@ class TaxBandsTests {

@Test
fun bandsForScotland2019() {
val taxBand = TaxBands(Country.SCOTLAND, 2019).bands[1]
val taxBand = TaxBands(SCOTLAND, 2019).bands[1]
assertEquals(14549.00, taxBand.upper)
assertEquals(12509.00, taxBand.lower)
assertEquals(0.19, taxBand.percentageAsDecimal)
Expand All @@ -74,23 +76,23 @@ class TaxBandsTests {

@Test
fun bandsForWales2020() {
val taxBand = TaxBands(Country.WALES, 2020).bands[0]
val taxBand = TaxBands(WALES, 2020).bands[0]
assertEquals(20000.00, taxBand.upper)
assertEquals(0.00, taxBand.lower)
assertEquals(0.10, taxBand.percentageAsDecimal)
}

@Test
fun bandsForScotland2020() {
val taxBand = TaxBands(Country.SCOTLAND, 2020).bands[0]
val taxBand = TaxBands(SCOTLAND, 2020).bands[0]
assertEquals(21000.00, taxBand.upper)
assertEquals(0.00, taxBand.lower)
assertEquals(0.15, taxBand.percentageAsDecimal)
}

@Test
fun bandsForEngland2020() {
val taxBand = TaxBands(Country.ENGLAND, 2020).bands[0]
val taxBand = TaxBands(ENGLAND, 2020).bands[0]
assertEquals(22000.00, taxBand.upper)
assertEquals(0.00, taxBand.lower)
assertEquals(0.20, taxBand.percentageAsDecimal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,30 @@
package calculator.model.taxcodes

import calculator.Calculator
import calculator.model.PayPeriod
import calculator.model.PayPeriod.YEARLY
import kotlin.test.Test
import kotlin.test.assertEquals

class BRCodeTests {
@Test
fun `BR England 100K`() {
val calculator = Calculator("BR", 100000.0, payPeriod = PayPeriod.YEARLY, taxYear = 2019).run()
val calculator = Calculator("BR", 100000.0, payPeriod = YEARLY, taxYear = 2019).run()
assertEquals(20000.0, calculator.yearly.taxToPay)
assertEquals(12608.784000000001, calculator.yearly.employersNI)
assertEquals(5964.16, calculator.yearly.employeesNI)
}

@Test
fun `BR Wales 100K`() {
val calculator = Calculator("CBR", 100000.0, payPeriod = PayPeriod.YEARLY, taxYear = 2019).run()
val calculator = Calculator("CBR", 100000.0, payPeriod = YEARLY, taxYear = 2019).run()
assertEquals(20000.0, calculator.yearly.taxToPay)
assertEquals(12608.784000000001, calculator.yearly.employersNI)
assertEquals(5964.16, calculator.yearly.employeesNI)
}

@Test
fun `BR Scotland 100K`() {
val calculator = Calculator("SBR", 100000.0, payPeriod = PayPeriod.YEARLY, taxYear = 2019).run()
val calculator = Calculator("SBR", 100000.0, payPeriod = YEARLY, taxYear = 2019).run()
assertEquals(20000.0, calculator.yearly.taxToPay)
assertEquals(12608.784000000001, calculator.yearly.employersNI)
assertEquals(5964.16, calculator.yearly.employeesNI)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package calculator.model.taxcodes

import calculator.Calculator
import calculator.model.PayPeriod
import calculator.model.PayPeriod.YEARLY
import kotlin.test.Test
import kotlin.test.assertEquals

Expand All @@ -26,7 +26,7 @@ class GeneralTaxCodeTests {
fun `NT Yearly 20k`() {
val taxCode = "NT"
val wages = 20000.00
val response = Calculator(taxCode, wages, payPeriod = PayPeriod.YEARLY).run()
val response = Calculator(taxCode, wages, payPeriod = YEARLY).run()
assertEquals(0.0, response.yearly.taxToPay)
assertEquals(1568.784, response.yearly.employersNI)
assertEquals(1364.1599999999999, response.yearly.employeesNI)
Expand Down
Loading

0 comments on commit 7a4321d

Please sign in to comment.