Commit 38686ec1 authored by Javier Hernández's avatar Javier Hernández 🤓

Fixed output3 and printRecipe

- added Double rounding problem explanation for output 3
parent 856ad7b7
buildscript { buildscript {
ext.kt_version = '1.3.50' ext.kt_version = '1.3.50'
ext.kt_logging_version = '1.6.20'
ext.jupiter_version = '5.0.2' ext.jupiter_version = '5.0.2'
repositories { repositories {
...@@ -28,11 +27,15 @@ subprojects { ...@@ -28,11 +27,15 @@ subprojects {
} }
dependencies { dependencies {
// Kotlin
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8" compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
compile "io.github.microutils:kotlin-logging:$kt_logging_version"
// Logging
compile "io.github.microutils:kotlin-logging:1.6.20"
implementation 'ch.qos.logback:logback-classic:1.2.3' implementation 'ch.qos.logback:logback-classic:1.2.3'
// This dependencies are declared in the parent build.gradle // Testing
testCompile "org.junit.jupiter:junit-jupiter-api:$jupiter_version" testCompile "org.junit.jupiter:junit-jupiter-api:$jupiter_version"
testCompile "org.junit.jupiter:junit-jupiter-params:$jupiter_version" testCompile "org.junit.jupiter:junit-jupiter-params:$jupiter_version"
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kt_version" testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kt_version"
......
package com.lm.sales.builders package com.lm.sales.builders
import com.lm.sales.model.Product import com.lm.sales.model.Product
import com.lm.sales.util.TaxesCalculator
import mu.KLogging import mu.KLogging
/** /**
...@@ -48,7 +49,7 @@ class RecipeBuilder { ...@@ -48,7 +49,7 @@ class RecipeBuilder {
if(it.presentation != null) if(it.presentation != null)
pres = it.presentation!!.name.plus(" of") pres = it.presentation!!.name.plus(" of")
// If produt is imported we write it in the recipe // If product is imported we write it in the recipe
var imported = "" var imported = ""
if(it.imported) if(it.imported)
imported = "imported" imported = "imported"
...@@ -57,7 +58,7 @@ class RecipeBuilder { ...@@ -57,7 +58,7 @@ class RecipeBuilder {
} }
logger.info { "Sales Taxes: $salesTaxes" } logger.info { "Sales Taxes: $salesTaxes" }
logger.info { "Total: $totalAmount" } logger.info { "Total: ${TaxesCalculator.round(totalAmount)}" }
} }
} }
......
...@@ -20,38 +20,49 @@ class TaxesCalculator { ...@@ -20,38 +20,49 @@ class TaxesCalculator {
/** /**
* Returns a [Taxes] object with calculated taxes * Returns a [Taxes] object with calculated taxes
* *
* @param imported * @param product [Product]
* @param price
* @param productClass
* *
* @return [Taxes] object * @return [Taxes] object
*/ */
fun calculate(product: Product): Taxes { fun calculate(product: Product): Taxes {
var taxes = 0.0 var applicableTaxes = 0
// Basic taxes: if product isn't a book, foods or medicals, it has a 10% more taxes // Basic taxes: if product isn't a book, foods or medicals, it has a 10% more taxes
if(product !is Book && product !is Foods && product !is Medicals) if(product !is Book && product !is Foods && product !is Medicals)
taxes = product.basePrice * 10 / 100 applicableTaxes = 10
// If imported, we add a 5% more to taxes // If imported, we add a 5% more to taxes
if(product.imported) if(product.imported)
taxes = taxes.plus(product.basePrice * 5 /100) applicableTaxes = applicableTaxes.plus(5)
// kotlin's round rounds automatically // kotlin's round rounds automatically
return Taxes().apply { return Taxes().apply {
taxesAmount = round(taxes) taxesAmount = round( product.basePrice * applicableTaxes / 100)
afterTaxesAmount = product.basePrice.plus(taxesAmount) afterTaxesAmount = round(product.basePrice.plus(taxesAmount))
} }
} }
/** /**
* Rounds to the closest 0.05 with a 2 decimal digits scale * Rounds to the closest 0.05
* *
* @param amount [Double] * @param amount [Double]
* *
* @return [Double]
*/ */
private fun round(amount: Double): Double{ private fun round005(amount: Double): Double{
return Math.round(amount * 20.0) / 20.0
}
/**
* Rounds towards the even side
*
* @param amount [Double]
*
* @return [Double]
*
*/
fun round(amount: Double): Double{
return BigDecimal(amount).setScale(2, RoundingMode.HALF_EVEN).toDouble() return BigDecimal(amount).setScale(2, RoundingMode.HALF_EVEN).toDouble()
} }
} }
......
...@@ -123,5 +123,11 @@ class ShoppingTests { ...@@ -123,5 +123,11 @@ class ShoppingTests {
// Adds cart products to recipe and prints it // Adds cart products to recipe and prints it
RecipeBuilder().withProducts(cart.productList).withRecipeName("Output 3:").printRecipe() RecipeBuilder().withProducts(cart.productList).withRecipeName("Output 3:").printRecipe()
// In the particular case of the the imported box of chocolates @ 11.85, it exits a problem with Doubles and rounding.
// the 5% of 11.25 its 0.5625 that isn't representable in binary form as a double. We should save the value previously
// from a String, and then set the scale and work with BigDecimals
//
// @see https://blogs.oracle.com/corejavatechtips/the-need-for-bigdecimal
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment