Skip to content

Commit

Permalink
Resolve ktlintand detekt issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasschuerg committed Feb 12, 2020
1 parent 764ceff commit 6806140
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 25 deletions.
14 changes: 11 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ buildscript {
}

plugins {
id("io.gitlab.arturbosch.detekt").version("1.4.0")
id("org.jlleitschuh.gradle.ktlint") version("9.1.1")
id("io.gitlab.arturbosch.detekt").version("1.5.1")
id("org.jlleitschuh.gradle.ktlint") version("9.2.0")
}

subprojects {
apply plugin: "org.jlleitschuh.gradle.ktlint" // Version should be inherited from parent
apply plugin: "org.jlleitschuh.gradle.ktlint"
apply plugin: "io.gitlab.arturbosch.detekt"
}

Expand All @@ -27,6 +27,14 @@ allprojects {
jcenter()
maven { url 'https://jitpack.io' }
}

detekt {
buildUponDefaultConfig = true

reports {
html.enabled = true
}
}
}

task clean(type: Delete) {
Expand Down
4 changes: 2 additions & 2 deletions money/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {

defaultConfig {
minSdkVersion 14
versionCode 7
versionName "0.7.2"
versionCode 8
versionName "0.8.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

Expand Down
8 changes: 6 additions & 2 deletions money/src/main/java/de/tobiasschuerg/money/Currencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ package de.tobiasschuerg.money
* Created by Tobias Schürg on 18.10.2017.
*/

@Suppress("MagicNumber")
object Currencies {

val EURO = Currency("EUR", "Euro", 1.0) // base rate; of course we're europe
val USDOLLAR = Currency("USD", "United States Dollar", 1.09) // as of 12.02.2019
// base rate; of course we're europe
val EURO = Currency("EUR", "Euro", 1.0)

// as of 12.02.2019
val USDOLLAR = Currency("USD", "United States Dollar", 1.09)

// ...
}
9 changes: 4 additions & 5 deletions money/src/main/java/de/tobiasschuerg/money/Currency.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ data class Currency(
}

fun getFormatter(): NumberFormat {
try {
return CurrencyHelper.getCurrencyFormatter(currencyCode)
} catch (throwable: Throwable) {
// throwable.printStackTrace()
return DecimalFormat("#.########## " + currencyCode.getSymbol())
return try {
CurrencyHelper.getCurrencyFormatter(currencyCode)
} catch (throwable: IllegalStateException) {
DecimalFormat("#.########## " + currencyCode.getSymbol())
}
}

Expand Down
8 changes: 5 additions & 3 deletions money/src/main/java/de/tobiasschuerg/money/Money.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package de.tobiasschuerg.money

import java.lang.IllegalArgumentException
import java.math.BigDecimal
import java.math.MathContext
import java.math.RoundingMode
Expand All @@ -10,6 +9,7 @@ import java.math.RoundingMode
*
* Created by Tobias Schürg on 28.07.2017.
*/
@Suppress("TooManyFunctions")
data class Money(val amount: BigDecimal = BigDecimal.ZERO, val currency: Currency) {

constructor(amount: Double, currency: Currency) : this(BigDecimal(amount), currency)
Expand Down Expand Up @@ -75,7 +75,7 @@ data class Money(val amount: BigDecimal = BigDecimal.ZERO, val currency: Currenc
fun isNegative(): Boolean = amount.signum() == -1

fun isZero(): Boolean {
return amount.setScale(5, RoundingMode.HALF_DOWN).signum() == 0
return amount.setScale(MoneyConfig.scale, RoundingMode.HALF_DOWN).signum() == 0
}

fun isNonZero(): Boolean = !isZero()
Expand All @@ -85,7 +85,9 @@ data class Money(val amount: BigDecimal = BigDecimal.ZERO, val currency: Currenc
override fun equals(other: Any?): Boolean {
return if (other is Money) {
val codeEquals = currency.currencyCode == other.currency.currencyCode
val amountEquals = amount.setScale(MoneyConfig.scale, RoundingMode.HALF_DOWN) == other.amount.setScale(MoneyConfig.scale, RoundingMode.HALF_DOWN)
val thisAmount = amount.setScale(MoneyConfig.scale, RoundingMode.HALF_DOWN)
val otherAmount = other.amount.setScale(MoneyConfig.scale, RoundingMode.HALF_DOWN)
val amountEquals = thisAmount == otherAmount
codeEquals && amountEquals
} else {
false
Expand Down
5 changes: 3 additions & 2 deletions money/src/main/java/de/tobiasschuerg/money/MoneyConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package de.tobiasschuerg.money
/**
* Configuration for proper calculations.
*
* Make this a data class and for initialization before usage?
* TODO: Make this a data class and for initialization before usage?
*
* Created by Tobias Schürg on 21.10.2017.
*/
Expand All @@ -13,5 +13,6 @@ object MoneyConfig {
* Scale for comparing.
* See **BigDecimal.setScale**
*/
val scale = 10
@Suppress("MagicNumber")
var scale = 10
}
1 change: 1 addition & 0 deletions money/src/main/java/de/tobiasschuerg/money/MoneyList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import java.math.BigDecimal
*
* Created by Tobias Schürg on 18.10.2017.
*/
@Suppress("TooManyFunctions")
data class MoneyList(private val currency: Currency, private val autoConvert: Boolean = false) : MutableList<Money> {

private val list = mutableListOf<Money>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package de.tobiasschuerg.money

import de.tobiasschuerg.money.Currencies.EURO
import de.tobiasschuerg.money.Currencies.USDOLLAR
import java.math.BigDecimal
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import java.math.BigDecimal

class MoneyArithmeticsTest {

Expand Down Expand Up @@ -73,7 +73,6 @@ class MoneyArithmeticsTest {
assertEquals(result2Minus, result1Minus)
}


@Test
fun `test multiplying by two`() {
val amount = 7.23
Expand Down Expand Up @@ -112,4 +111,4 @@ class MoneyArithmeticsTest {
assertEquals(dollarConverted.toDouble(), dollarCalculated.toDouble(), 0.0001)
assertEquals(dollarConverted.toFloat(), dollarCalculated.toFloat())
}
}
}
3 changes: 1 addition & 2 deletions money/src/test/java/de/tobiasschuerg/money/MoneyListTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,4 @@ class MoneyListTest {
Assert.assertEquals(1, sublist.min()?.amount?.intValueExact())
Assert.assertEquals(6, sublist.max()?.amount?.intValueExact())
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package de.tobiasschuerg.money.sample

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import de.tobiasschuerg.money.Currencies
import de.tobiasschuerg.money.Currency
import de.tobiasschuerg.money.Money
import de.tobiasschuerg.money.MoneyList
import kotlinx.android.synthetic.main.activity_sample.*

@Suppress("MagicNumber")
class SampleActivity : AppCompatActivity() {

@SuppressLint("SetTextI18n")
Expand All @@ -25,7 +26,6 @@ class SampleActivity : AppCompatActivity() {
// another way of creating a money from predefined
val usDollar = Currencies.USDOLLAR.withRate(1.17705) // Oct. 17 2017, 21:00


// create a money object and use it for calculations.
val savedMoney = Money(1337, euro)
val birthdayMoney = Money(125, usDollar)
Expand All @@ -35,7 +35,6 @@ class SampleActivity : AppCompatActivity() {
val bitcoinMoney = savedMoney.convertInto(bitcoin)
log("I could invest my $availableMoney and get $bitcoinMoney instead")


// Summing up:
val moneyList = MoneyList(usDollar)
moneyList.add(Money(100.01, usDollar))
Expand Down

0 comments on commit 6806140

Please sign in to comment.