Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/get best3 cities for buying soft drinks #83

Open
wants to merge 169 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
169 commits
Select commit Hold shift + click to select a range
a49cf8e
update gitignore to delete idea folder
AmeerAmjed Feb 7, 2023
46fe840
add fake data
mosayed01 Feb 9, 2023
c9362c5
add test cases for cheapest banana price
mosayed01 Feb 9, 2023
ccc6037
add implementation for cheapest banana price
mosayed01 Feb 9, 2023
32c7ac1
refactor: rename class cheapest banana price
mosayed01 Feb 9, 2023
52ac2e0
add fake data
Feb 9, 2023
0a93ebe
add test cases for matched city
Feb 9, 2023
ac6bc35
add fake data
Ethaar7 Feb 9, 2023
4b9f576
add test cases
Ethaar7 Feb 9, 2023
f31f887
update misspell countries names
TarekIdrees Feb 9, 2023
0f1eae0
first implementation of execute function
TarekIdrees Feb 9, 2023
4a52911
add implementation test to actual code
Ethaar7 Feb 9, 2023
b61d6e1
update version review
Abanoub-Nagy Feb 10, 2023
d56567e
add enum class in package
Ethaar7 Feb 10, 2023
79c8049
apply the required changes in the review and improve Option to Bedroo…
Ethaar7 Feb 10, 2023
a732164
Merge remote-tracking branch 'origin/feature/city_has_the_highest_dif…
Ethaar7 Feb 10, 2023
5234893
improve Option to BedroomOption as required changes in the review
Ethaar7 Feb 10, 2023
5f5e975
Merge remote-tracking branch 'origin/feature/city_has_the_highest_dif…
Ethaar7 Feb 10, 2023
3dcae3e
update version review
Abanoub-Nagy Feb 10, 2023
2d73e57
change return type from empty string to null in case of the countries…
Feb 10, 2023
99622e5
handle the case of countries names
Feb 10, 2023
f3bc9cf
add fake data and test cases
davidsamuelx Feb 10, 2023
db21962
update new version review
Abanoub-Nagy Feb 10, 2023
0517997
improve code
AmeerAmjed Feb 10, 2023
24e0577
improve code
AmeerAmjed Feb 10, 2023
9524918
complete the logic of feature
TarekIdrees Feb 10, 2023
a2ec718
improve fake data and test cases
davidsamuelx Feb 10, 2023
80ebd39
add implementation for cheapest city internet
davidsamuelx Feb 10, 2023
56e0b5c
add two test case
Abanoub-Nagy Feb 10, 2023
d965a42
improve logic of feature
Abanoub-Nagy Feb 10, 2023
9e7d460
improve code to be readable
Ethaar7 Feb 10, 2023
683fc83
add test case for best city interactor
Feb 10, 2023
5c81c52
create GetCitiesAverageSalary class
Ashraf-El-Fallah Feb 11, 2023
ba43931
add test cases for average salaries
Ashraf-El-Fallah Feb 11, 2023
4192cf2
add fake data source
Ashraf-El-Fallah Feb 11, 2023
3f5bbf9
make the code more readable by rename naming of variables and functions
Feb 11, 2023
3595ca7
improve code to be readable
davidsamuelx Feb 11, 2023
cb160ff
rename test case and delete unnecessary dependency
davidsamuelx Feb 11, 2023
b1deb1c
complete tests
Feb 11, 2023
3c61e63
Delete FakeDataSource.kt
Ali873-debug Feb 11, 2023
02d7c85
complete Implementation
Feb 11, 2023
ef5e92e
apply kotlin scope functions
TarekIdrees Feb 11, 2023
c091e0e
improve code
Feb 11, 2023
a8a8483
improve code phase two
Feb 11, 2023
95a401e
improve code to be short and readable
AmeerAmjed Feb 11, 2023
cfe53fc
initialize classes
Feb 11, 2023
b2c1a41
add fake data
Feb 11, 2023
42873e8
add test cases for average fruits and vegetabls
Feb 11, 2023
6606ac5
complete execute function
Feb 11, 2023
c7e1a13
fix expected data in test class
Ali873-debug Feb 11, 2023
477ba5d
add fake data
Ali873-debug Feb 11, 2023
23b891d
implement execute function for get cities average salary
Ali873-debug Feb 11, 2023
de6a255
Merge remote-tracking branch 'origin/feature/cities_average_salary' i…
Ali873-debug Feb 11, 2023
8f979f1
change toFormalCase function name to toChangeFormatCityName to be rea…
AmeerAmjed Feb 12, 2023
9456741
improve execute function
Feb 12, 2023
6e5c1cf
add new test case
Ali873-debug Feb 12, 2023
2d91220
improve code readability
TarekIdrees Feb 12, 2023
8dd1bd8
change exception to illegal argument exception
Ali873-debug Feb 12, 2023
6c4934b
add new test case and improve logic code
davidsamuelx Feb 12, 2023
69a568d
Merge remote-tracking branch 'origin/feature/cheapest_city_internet' …
davidsamuelx Feb 12, 2023
d0f235f
improve code to be readable
Ethaar7 Feb 12, 2023
377bf3b
Merge branch 'feature/city_that_match_manager_expectations' into develop
AmeerAmjed Feb 12, 2023
168feae
add return city
abdellahtachouchout Feb 12, 2023
5d80830
clean up
AmeerAmjed Feb 12, 2023
830e5b2
frist commit
khaledeid1k Feb 12, 2023
ba10673
Merge remote-tracking branch 'origin/develop' into develop
khaledeid1k Feb 12, 2023
21523e5
TopTenCountriesTaxes class with initial test class.
Feb 12, 2023
b098fa0
improve some code
Feb 12, 2023
7111794
Merge branch 'feature/average_fruits_vegetables' into develop
AmeerAmjed Feb 12, 2023
bb9bc3e
Merge branch 'feature/city_has_the_highest_different' into develop
AmeerAmjed Feb 13, 2023
b413df9
change handel fake data to create multi case in test case
AmeerAmjed Feb 13, 2023
4f4d4e6
Merge branch 'develop' into feature/cheapest_city_internet
AmeerAmjed Feb 13, 2023
da5b2d0
refactor fake data
AmeerAmjed Feb 13, 2023
6f62f45
improve code and handel nullability salaries
AmeerAmjed Feb 13, 2023
eac887d
TopTenCountriesTaxes class the null pointer exception solved.
Feb 13, 2023
cdec55c
improve code
khaledeid1k Feb 13, 2023
84daf15
add test without logic
Feb 13, 2023
98fef9d
add logic
Feb 13, 2023
2f6ae6d
create sub functions
Feb 13, 2023
9f345e5
improve code and add comments to function
khaledeid1k Feb 13, 2023
1e97412
Merge remote-tracking branch 'origin/develop' into feature/cheapest_a…
khaledeid1k Feb 13, 2023
67816fb
refactoring fake datasource
khaledeid1k Feb 13, 2023
d65bf8f
Clean up
khaledeid1k Feb 13, 2023
d2398ea
Merge branch 'develop' into feature/cities_average_salary
AmeerAmjed Feb 13, 2023
4be3f93
refactor fake data
AmeerAmjed Feb 13, 2023
e6e29f7
add test case
AmeerAmjed Feb 13, 2023
f233eea
add fake data
AmeerAmjed Feb 13, 2023
345fca7
delete file duplicated file
AmeerAmjed Feb 13, 2023
d3c6774
Merge remote-tracking branch 'origin/feature/best_fashion_shopping_ci…
AmeerAmjed Feb 13, 2023
921a635
implement feature get best cities for fashion shopping
AmeerAmjed Feb 13, 2023
32407a0
Delete FakeDataSource.kt
AmeerAmjed Feb 14, 2023
a680681
Delete FakeDataSource.kt
AmeerAmjed Feb 14, 2023
984a72b
improve fake data and edit test cases
Feb 14, 2023
d7acdd4
Merge remote-tracking branch 'origin/feature/best_city' into feature/…
Feb 14, 2023
dc3f883
improve fake data and edit test cases
Feb 14, 2023
2778dcc
improve fake data and edit test cases
Feb 14, 2023
2527fef
fix issue with delete files from github
AmeerAmjed Feb 14, 2023
208e975
Delete FakeDataSource.kt
AmeerAmjed Feb 14, 2023
a5b9023
Delete FakeDataSource.kt
AmeerAmjed Feb 14, 2023
543fa8e
Merge branch 'feature/top_ten_countries_taxes' into develop
AmeerAmjed Feb 14, 2023
85ede82
fix merge and use fake data
AmeerAmjed Feb 14, 2023
fb40976
Merge branch 'feature/best_fashion_shopping_cities' into develop
AmeerAmjed Feb 14, 2023
b3c9301
fix merge
AmeerAmjed Feb 14, 2023
e1ab1f3
refactor code
davidsamuelx Feb 14, 2023
64702d8
add new test cases to fruit and vegetables
Feb 14, 2023
4cf27ff
Merge pull request #17 from The-Cupcake-team/feature/add_new_case_to_…
AmeerAmjed Feb 14, 2023
6fec065
refactor code
AmeerAmjed Feb 14, 2023
6bd4f4a
Merge branch 'develop' into feature/best_city_to_saving_money
AmeerAmjed Feb 14, 2023
afafb23
improve code
AmeerAmjed Feb 14, 2023
7b88adc
Merge branch 'feature/add_new_test_case_to_fruits_vegetables' into de…
AmeerAmjed Feb 14, 2023
5940012
add helper to show result app
AmeerAmjed Feb 14, 2023
619e6a5
Make functions have single responsibility
khaledeid1k Feb 16, 2023
1fdb013
change away get fake data
AmeerAmjed Feb 16, 2023
83cc2c1
Merge pull request #19 from The-Cupcake-team/feature/refactor_handel_…
AmeerAmjed Feb 16, 2023
fba621a
fix(banana): change argument type
mosayed01 Feb 17, 2023
f12e472
exclude null fruits and vegetables prices
Feb 17, 2023
e8cd71f
refactor fruits-vegetables fake data
Feb 17, 2023
cadeb99
improve test cases of fuits and vegetables
Feb 17, 2023
5b8d7f2
edit names of functions, calculate the ratio and exclude null fruit v…
Feb 17, 2023
03deb05
Merge branch 'develop' into fix/GetTopTenCitiesHasCheapestPriceOfApar…
khaledeid1k Feb 17, 2023
e5d67dd
fix best city for saving money interactor
Feb 17, 2023
6e9fe27
refactor(banana_test): change way of naming functions
mosayed01 Feb 17, 2023
cdf390d
fix(costlier): change logic
Ethaar7 Feb 17, 2023
d65a8cf
fix(costlier): change test
Ethaar7 Feb 17, 2023
10fb665
fix(costlier): refactor fake data
Ethaar7 Feb 17, 2023
4d32831
fix(internet_test): refactor test cases
davidsamuelx Feb 17, 2023
d356405
fix(internet): change logic code
davidsamuelx Feb 17, 2023
49beb52
refactor(internet_test): change test case name
davidsamuelx Feb 18, 2023
4d58b12
refactor(banana_test): change fake data to objects
mosayed01 Feb 18, 2023
e336412
refactor(banana): change empty cases to throw
mosayed01 Feb 18, 2023
23c73a5
fix(internet): change exception
davidsamuelx Feb 18, 2023
e5ce886
test(internet): change exception type
davidsamuelx Feb 18, 2023
a98dea0
test(internet): add enhancement to test cases
davidsamuelx Feb 18, 2023
05af75d
refactor(city highest difference test): change style of naming function
Ethaar7 Feb 18, 2023
c9076d7
fix: change logic
khaledeid1k Feb 18, 2023
0c21ea6
Merge pull request #20 from The-Cupcake-team/fix/cheapest_banana_para…
AmeerAmjed Feb 18, 2023
168b037
feat: add interactor for cheapest tennis price
mosayed01 Feb 19, 2023
1e4ff11
test: add test cases for cheapest tennis price
mosayed01 Feb 19, 2023
2d39642
feat: add implementation for cheapest tennis weekend
mosayed01 Feb 19, 2023
43cdafc
change comments in fruit and vegetables class test
Feb 19, 2023
f41bda5
Merge branch 'develop' into fix/fruits_not_exclude_nulls
Feb 19, 2023
928a169
take how many cities the user need it
Feb 19, 2023
fcef0b8
add test case for fruit and vegetable when data source is empty
Feb 19, 2023
804e69b
Merge pull request #21 from The-Cupcake-team/fix/fruits_not_exclude_n…
AmeerAmjed Feb 19, 2023
c273e92
Merge branch 'fix/refactor_feature‏_city_has_the_highest_difference' …
AmeerAmjed Feb 19, 2023
ef57489
add the tests of the new feature
Feb 20, 2023
78fd058
improve test cases
Feb 20, 2023
6028955
complete the feature
Feb 20, 2023
db9acbd
test(internet): refactor fake data
davidsamuelx Feb 20, 2023
8ac4e97
test(internet): refactor test cases
davidsamuelx Feb 20, 2023
a9c719a
test: add val to expected values
mosayed01 Feb 20, 2023
d4fcbee
test: edite val name
mosayed01 Feb 20, 2023
ca2da37
fix: change message of throw exception
khaledeid1k Feb 20, 2023
844be77
test: change style of naming testing functions
davidsamuelx Feb 20, 2023
78a58df
refactor: change message of exception
davidsamuelx Feb 20, 2023
c5a0133
fix: change class to object
khaledeid1k Feb 20, 2023
82ae43e
Merge pull request #24 from The-Cupcake-team/fix/refactor_feature_cit…
AmeerAmjed Feb 20, 2023
f7b67d2
Merge branch 'fix/top_cities_has_cheapest_price_of_apartments_and_yea…
AmeerAmjed Feb 20, 2023
0926c52
Merge pull request #26 from The-Cupcake-team/feature/tennis_weekend
AmeerAmjed Feb 20, 2023
4386f22
fix: rename execute function to invoke
Feb 20, 2023
296717c
test: change function to invoke
davidsamuelx Feb 20, 2023
71f4621
refactor: change function to invoke in logic
davidsamuelx Feb 20, 2023
cd2ac76
test: refactor test cases
davidsamuelx Feb 20, 2023
1f98d0b
Merge pull request #32 from The-Cupcake-team/fix/refactor_feature_cit…
mosayed01 Feb 20, 2023
c110948
Merge pull request #31 from The-Cupcake-team/fix/fruits_not_exclude_n…
AmeerAmjed Feb 20, 2023
bdf71a6
Merge branch 'develop' into feature/get_best_cities_for_buying_soft_d…
Feb 20, 2023
6cd4533
transform fake data class to singleton
Feb 21, 2023
6fca653
improve test cases
Feb 21, 2023
d8b0b69
improve readability
Feb 21, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 73 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,76 @@ bin/
.vscode/

### Mac OS ###
.DS_Store
.DS_Store

.idea/
.idea/shelf
/confluence/target
/dependencies/repo
/android.tests.dependencies
/dependencies/android.tests.dependencies
/dist
/local
/gh-pages
/ideaSDK
/clionSDK
/android-studio/sdk
/tmp
/intellij
workspace.xml
*.versionsBackup
/idea/testData/debugger/tinyApp/classes*
/jps-plugin/testData/kannotator
/js/js.translator/testData/out/
/js/js.translator/testData/out-min/
/js/js.translator/testData/out-pir/
.gradle/
!**/src/**/build
!**/test/**/build
!**/testData/**/*.iml
.idea/remote-targets.xml
.idea/libraries/Gradle*.xml
.idea/libraries/Maven*.xml
.idea/artifacts/PILL_*.xml
.idea/artifacts/KotlinPlugin.xml
.idea/modules
.idea/runConfigurations/JPS_*.xml
.idea/runConfigurations/PILL_*.xml
.idea/runConfigurations/_FP_*.xml
.idea/runConfigurations/_MT_*.xml
.idea/libraries
.idea/gradle.xml
.idea/inspectionProfiles/profiles_settings.xml
.idea/.name
.idea/artifacts/dist_auto_*
.idea/artifacts/dist.xml
.idea/artifacts/ideaPlugin.xml
.idea/artifacts/kotlinc.xml
.idea/artifacts/kotlin_compiler_jar.xml
.idea/artifacts/kotlin_plugin_jar.xml
.idea/artifacts/kotlin_jps_plugin_jar.xml
.idea/artifacts/kotlin_daemon_client_jar.xml
.idea/artifacts/kotlin_imports_dumper_compiler_plugin_jar.xml
.idea/artifacts/kotlin_main_kts_jar.xml
.idea/artifacts/kotlin_compiler_client_embeddable_jar.xml
.idea/artifacts/kotlin_reflect_jar.xml
.idea/artifacts/kotlin_stdlib_js_ir_*
.idea/artifacts/kotlin_test_js_ir_*
.idea/artifacts/kotlin_stdlib_wasm_*
.idea/artifacts/kotlin_test_wasm_*
.idea/artifacts/kotlinx_atomicfu_runtime_*
.idea/artifacts/kotlinx_cli_jvm_*
.idea/csv-plugin.xml
.idea/libraries-with-intellij-classes.xml
.idea/misc.xml
.idea/protoeditor.xml
node_modules/
.rpt2_cache/
libraries/tools/kotlin-test-js-runner/lib/
local.properties
buildSrcTmp/
distTmp/
outTmp/
/test.output
/kotlin-native/dist
kotlin-ide/
3 changes: 0 additions & 3 deletions .idea/.gitignore

This file was deleted.

32 changes: 0 additions & 32 deletions .idea/codeStyles

This file was deleted.

16 changes: 0 additions & 16 deletions .idea/csv-plugin.xml

This file was deleted.

17 changes: 0 additions & 17 deletions .idea/gradle.xml

This file was deleted.

7 changes: 0 additions & 7 deletions .idea/misc.xml

This file was deleted.

6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ repositories {
}

dependencies {
implementation("org.junit.jupiter:junit-jupiter:5.8.1")
testImplementation(kotlin("test"))
testImplementation("org.junit.jupiter:junit-jupiter:5.8.1")
}

tasks.test {
Expand Down
Binary file added lib/apiguardian-api-1.1.2.jar
Binary file not shown.
Binary file added lib/hamcrest-core-1.3.jar
Binary file not shown.
Binary file added lib/junit-4.13.1.jar
Binary file not shown.
Binary file added lib/junit-jupiter-5.8.1.jar
Binary file not shown.
Binary file added lib/junit-jupiter-api-5.8.1.jar
Binary file not shown.
Binary file added lib/junit-jupiter-engine-5.8.1.jar
Binary file not shown.
Binary file added lib/junit-jupiter-params-5.8.1.jar
Binary file not shown.
Binary file added lib/junit-platform-commons-1.8.1.jar
Binary file not shown.
Binary file added lib/junit-platform-engine-1.8.1.jar
Binary file not shown.
Binary file added lib/opentest4j-1.2.0.jar
Binary file not shown.
17 changes: 3 additions & 14 deletions src/main/kotlin/Main.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
import dataSource.CsvDataSource
import dataSource.utils.CsvParser
import interactor.CostOfLivingDataSource
import interactor.GetCityHasCheapestInternetConnectionInteractor
import interactor.GetHighestSalaryAverageCititesNamesInteractor
import interactor.*
import ui.UserInterface

fun main() {
val csvParser = CsvParser()
val dataSource: CostOfLivingDataSource = CsvDataSource(csvParser)

val getHighestSalaryAverageCities = GetHighestSalaryAverageCititesNamesInteractor(dataSource)
println(getHighestSalaryAverageCities.execute(limit = 10))
printSeparationLine()

val getCityHasCheapestInternetConnectionInteractor = GetCityHasCheapestInternetConnectionInteractor(dataSource)
println(getCityHasCheapestInternetConnectionInteractor.execute())

}
private fun printSeparationLine(){
print("\n_______________________________\n")
UserInterface(dataSource).init()
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package interactor

import model.CityEntity

class GetAverageFruitAndVegetablesInteractor(private val dataSource: CostOfLivingDataSource) {

operator fun invoke(limit: Int): List<String> {
return dataSource
.getAllCitiesData()
.filter(::excludeNullFruitVegPriceAndNullSalaries)
.sortedBy(::calculateTheRatioBetweenFruitVegPriceAndSalary)
.take(limit)
.map(CityEntity::cityName)
}

fun excludeNullFruitVegPriceAndNullSalaries(city: CityEntity) =
checkNullableFruitAndVegetablePrices(city) && city.averageMonthlyNetSalaryAfterTax != null

private fun checkNullableFruitAndVegetablePrices(city: CityEntity) =
with(city.fruitAndVegetablesPrices) {
apples1kg != null && banana1kg != null && oranges1kg != null && tomato1kg != null
&& potato1kg != null && onion1kg != null && lettuceOneHead != null
}

private fun calculateTheRatioBetweenFruitVegPriceAndSalary(city: CityEntity) =
city.fruitAndVegetablesPrices.getAverageFruitsAndVegetablesPrice() / city.averageMonthlyNetSalaryAfterTax!!

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package interactor

import model.CityEntity

class GetBestCitiesForBuyingSoftDrinksInteractor(private val dataSource: CostOfLivingDataSource) {

operator fun invoke(limit : Int): List<CityEntity> {

return dataSource.getAllCitiesData()
.filter(::excludeNullRequireValues)
.takeIf { it.isNotEmpty() && limit > 0 }
?.run {
sortedByDescending(::calculateNumberOfSoftDrinksCanBuy)
.take(limit)
} ?: throw IllegalStateException("List of cities is invalid or limit below zero")
}

private fun excludeNullRequireValues(city: CityEntity): Boolean {

return with(city.realEstatesPrices) {
apartment3BedroomsInCityCentre != null &&
apartment3BedroomsOutsideOfCentre != null &&
apartmentOneBedroomInCityCentre != null &&
apartmentOneBedroomOutsideOfCentre != null } &&
city.averageMonthlyNetSalaryAfterTax != null &&
city.drinksPrices.cokePepsiAThirdOfLiterBottleInRestaurants != null
}

private fun calculateNumberOfSoftDrinksCanBuy(city: CityEntity): Int {

return ((city.averageMonthlyNetSalaryAfterTax!! - LIVING_COST - getMinimumRealEstatesPrice(city)
) / city.drinksPrices.cokePepsiAThirdOfLiterBottleInRestaurants!!).toInt()
}
private fun getMinimumRealEstatesPrice(city: CityEntity): Float {

return with(city.realEstatesPrices) {
minOf(
apartment3BedroomsInCityCentre!!,
apartment3BedroomsOutsideOfCentre!!,
apartmentOneBedroomOutsideOfCentre!!,
apartmentOneBedroomInCityCentre!!
)
}
}

private companion object {

const val LIVING_COST = 500.0f
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package interactor

import model.CityEntity
import model.ClothesPrices



class GetBestCitiesToFashionShoppingInteractor(private val dataSource: CostOfLivingDataSource) {

fun execute(): List<String> {
return dataSource.getAllCitiesData()
.filter(::excludeNullClothesBrandPrices)
.sortedBy {
it.clothesPrices.findAveragePrices()
}
.take(5)
.map { it.cityName }

}

private fun excludeNullClothesBrandPrices(city: CityEntity): Boolean {
return city.clothesPrices.oneSummerDressInAChainStoreZaraHAndM != null &&
city.clothesPrices.onePairOfNikeRunningShoesMidRange != null &&
city.clothesPrices.onePairOfJeansLevis50oneOrSimilar != null
}

private fun ClothesPrices.findAveragePrices(): Float {
return listOfNotNull(
onePairOfJeansLevis50oneOrSimilar,
oneSummerDressInAChainStoreZaraHAndM,
onePairOfNikeRunningShoesMidRange,
).average().toFloat()
}
}
45 changes: 45 additions & 0 deletions src/main/kotlin/interactor/GetBestCityForSavingMoneyInteractor.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package interactor

import model.CityEntity

class GetBestCityForSavingMoneyInteractor(private val dataSource: CostOfLivingDataSource) {

operator fun invoke(): CityEntity{

return dataSource.getAllCitiesData()
.filter(::excludeNullRequireValues)
.sortedByDescending(::calculateSavings)
.first()
}

private fun calculateSavings(city: CityEntity): Float {

return city.averageMonthlyNetSalaryAfterTax!! * DOUBLE_AVERAGE_SALARY -
city.realEstatesPrices.apartment3BedroomsInCityCentre!! -
with(city.foodPrices){
(loafOfFreshWhiteBread500g!! * REQUIRE_BREAD) -
(chickenFillets1kg!! * REQUIRE_CHICKEN) -
(riceWhite1kg!! * REQUIRE_RICE) -
(beefRound1kgOrEquivalentBackLegRedMeat!! * REQUIRE_MEAT) -
city.foodPrices.localCheese1kg!! } - OTHER_NEEDS
}
private fun excludeNullRequireValues(city: CityEntity): Boolean {

return city.realEstatesPrices.apartment3BedroomsInCityCentre != null &&
city.averageMonthlyNetSalaryAfterTax != null &&
city.foodPrices.loafOfFreshWhiteBread500g != null &&
city.foodPrices.localCheese1kg != null &&
city.foodPrices.beefRound1kgOrEquivalentBackLegRedMeat != null &&
city.foodPrices.chickenFillets1kg != null &&
city.foodPrices.riceWhite1kg != null
}
private companion object{

const val REQUIRE_BREAD = 30
const val REQUIRE_MEAT = 4
const val REQUIRE_RICE = 2
const val REQUIRE_CHICKEN = 10
const val OTHER_NEEDS = 250.0F
const val DOUBLE_AVERAGE_SALARY = 2
}
}
Loading