Skip to content

Commit

Permalink
added min(), max(), average() and median() to MoneyList
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasschuerg committed Nov 5, 2017
1 parent 63ec831 commit 231212a
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-rc1'
classpath 'com.android.tools.build:gradle:3.0.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'

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

import android.util.Log
import java.util.*


Expand All @@ -16,7 +15,7 @@ data class CurrencyCode(val code: String) {
try {
symbol = java.util.Currency.getInstance(code).symbol
} catch (ia: IllegalArgumentException) {
Log.w("Currency Code", "Could not find symbol for $code")
// Log.w("Currency Code", "Could not find symbol for $code")
symbol = code
}
return symbol
Expand Down
23 changes: 23 additions & 0 deletions money/src/main/java/de/tobiasschuerg/money/MoneyList.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.tobiasschuerg.money

import java.math.BigDecimal

/**
* List of money which provides additional features such as sum, average, median etc...
*
Expand Down Expand Up @@ -104,4 +106,25 @@ class MoneyList(private val currency: Currency, val autoConvert: Boolean = false
}

fun sum(): Money = sum

fun min(): Money? = list.minBy(Money::amount)

fun max(): Money? = list.maxBy(Money::amount)

fun median(): Money? {
if (list.isNotEmpty()) {
val index: Int = list.size / 2
return list.sortedBy(Money::amount)[index]
} else {
return null
}
}

fun average(): Money? {
return if (list.isNotEmpty()) {
sum / BigDecimal(list.size)
} else {
null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,5 @@ class MoneyArithmeticsTest {
val result1Minus = Money(amount1 - amount2, EURO)
val result2Minus = money1 - money2
Assert.assertEquals(result2Minus, result1Minus)


}
}
43 changes: 43 additions & 0 deletions money/src/test/java/de/tobiasschuerg/money/MoneyListTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package de.tobiasschuerg.money

import org.junit.Assert
import org.junit.Test

/**
* Created by Tobias Schürg on 05.11.2017.
*/
class MoneyListTest {

private val currency = Currency("TST", "TEST", 1.0)
private val list = MoneyList(currency)

init {
list.add(Money(3, currency))
list.add(Money(2, currency))
list.add(Money(6, currency))
list.add(Money(1, currency)) // min
list.add(Money(4, currency))
list.add(Money(9, currency)) // max
list.add(Money(7, currency))
list.add(Money(5, currency))
list.add(Money(8, currency))
}

@Test
fun minMax() {
Assert.assertEquals(Money(1, currency), list.min())
Assert.assertEquals(Money(9, currency), list.max())
Assert.assertEquals(Money(5, currency), list.average())
Assert.assertEquals(Money(5, currency), list.median())
}

@Test
fun minMaxEmptyList() {
val emptyList = MoneyList(currency)
Assert.assertNull(emptyList.min())
Assert.assertNull(emptyList.max())
Assert.assertNull(emptyList.average())
Assert.assertNull(emptyList.median())
}

}

0 comments on commit 231212a

Please sign in to comment.