Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
L
lm-challenge
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Javier Hernández
lm-challenge
Commits
38686ec1
Commit
38686ec1
authored
Oct 17, 2019
by
Javier Hernández
🤓
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed output3 and printRecipe
- added Double rounding problem explanation for output 3
parent
856ad7b7
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
36 additions
and
15 deletions
+36
-15
build.gradle
build.gradle
+6
-3
RecipeBuilder.kt
...ib/src/main/kotlin/com/lm/sales/builders/RecipeBuilder.kt
+3
-2
TaxesCalculator.kt
...-lib/src/main/kotlin/com/lm/sales/util/TaxesCalculator.kt
+21
-10
ShoppingTests.kt
lm-challenge-lib/src/test/kotlin/shopping/ShoppingTests.kt
+6
-0
No files found.
build.gradle
View file @
38686ec1
buildscript
{
ext
.
kt_version
=
'1.3.50'
ext
.
kt_logging_version
=
'1.6.20'
ext
.
jupiter_version
=
'5.0.2'
repositories
{
...
...
@@ -28,11 +27,15 @@ subprojects {
}
dependencies
{
// Kotlin
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'
// T
his dependencies are declared in the parent build.gradle
// T
esting
testCompile
"org.junit.jupiter:junit-jupiter-api:$jupiter_version"
testCompile
"org.junit.jupiter:junit-jupiter-params:$jupiter_version"
testCompile
"org.jetbrains.kotlin:kotlin-test-junit:$kt_version"
...
...
lm-challenge-lib/src/main/kotlin/com/lm/sales/builders/RecipeBuilder.kt
View file @
38686ec1
package
com.lm.sales.builders
import
com.lm.sales.model.Product
import
com.lm.sales.util.TaxesCalculator
import
mu.KLogging
/**
...
...
@@ -48,7 +49,7 @@ class RecipeBuilder {
if
(
it
.
presentation
!=
null
)
pres
=
it
.
presentation
!!
.
name
.
plus
(
" of"
)
// If produt is imported we write it in the recipe
// If produ
c
t is imported we write it in the recipe
var
imported
=
""
if
(
it
.
imported
)
imported
=
"imported"
...
...
@@ -57,7 +58,7 @@ class RecipeBuilder {
}
logger
.
info
{
"Sales Taxes: $salesTaxes"
}
logger
.
info
{
"Total: $
totalAmount
"
}
logger
.
info
{
"Total: $
{TaxesCalculator.round(totalAmount)}
"
}
}
}
...
...
lm-challenge-lib/src/main/kotlin/com/lm/sales/util/TaxesCalculator.kt
View file @
38686ec1
...
...
@@ -20,38 +20,49 @@ class TaxesCalculator {
/**
* Returns a [Taxes] object with calculated taxes
*
* @param imported
* @param price
* @param productClass
* @param product [Product]
*
* @return [Taxes] object
*/
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
if
(
product
!
is
Book
&&
product
!
is
Foods
&&
product
!
is
Medicals
)
taxes
=
product
.
basePrice
*
10
/
10
0
applicableTaxes
=
1
0
// If imported, we add a 5% more to taxes
if
(
product
.
imported
)
taxes
=
taxes
.
plus
(
product
.
basePrice
*
5
/
100
)
applicableTaxes
=
applicableTaxes
.
plus
(
5
)
// kotlin's round rounds automatically
return
Taxes
().
apply
{
taxesAmount
=
round
(
taxes
)
afterTaxesAmount
=
product
.
basePrice
.
plus
(
taxesAmount
)
taxesAmount
=
round
(
product
.
basePrice
*
applicableTaxes
/
100
)
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]
*
* @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
()
}
}
...
...
lm-challenge-lib/src/test/kotlin/shopping/ShoppingTests.kt
View file @
38686ec1
...
...
@@ -123,5 +123,11 @@ class ShoppingTests {
// Adds cart products to recipe and prints it
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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment