Skip to content

Commit

Permalink
Add the Logic to Handle "Others" Purchase Type (#59)
Browse files Browse the repository at this point in the history
Say we want to purchase something at Canadian Tire, but none of our
credit cards has a home improvement purchase reward. In this situation,
the app should use "Others" purchase reward to determine the reward
amount. This PR adds this logic and a safeguard to ensure there won't be
a null reference exception.
  • Loading branch information
LideLinusZhang authored Jul 23, 2024
1 parent b49a84e commit a3c8933
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ package edu.card.clarity.domain.creditCard

import edu.card.clarity.domain.Purchase
import edu.card.clarity.domain.PurchaseReward
import edu.card.clarity.enums.PurchaseType

data class CashBackCreditCard(
override val info: CreditCardInfo,
override val purchaseRewards: List<PurchaseReward>
) : ICreditCard {
override fun getReturnAmountInCash(purchase: Purchase): Float {
val purchaseReward = purchaseRewards.first {
it.applicablePurchaseType == purchase.type
}
val purchaseReward = purchaseRewards.let {
it.firstOrNull { reward ->
reward.applicablePurchaseType == purchase.type
} ?: it.firstOrNull { reward ->
reward.applicablePurchaseType == PurchaseType.Others
}
} ?: return 0.0f

return purchaseReward.getReturnAmount(purchase.total)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package edu.card.clarity.domain.creditCard
import edu.card.clarity.domain.PointSystem
import edu.card.clarity.domain.Purchase
import edu.card.clarity.domain.PurchaseReward
import edu.card.clarity.enums.PurchaseType

data class PointBackCreditCard(
override val info: CreditCardInfo,
Expand All @@ -14,8 +15,13 @@ data class PointBackCreditCard(
}

fun getReturnAmountInPoint(purchase: Purchase): Int {
val purchaseReturn =
purchaseRewards.first { it.applicablePurchaseType == purchase.type }
val purchaseReturn = purchaseRewards.let {
it.firstOrNull { reward ->
reward.applicablePurchaseType == purchase.type
} ?: it.firstOrNull { reward ->
reward.applicablePurchaseType == PurchaseType.Others
}
} ?: return 0

return purchaseReturn.getReturnAmount(purchase.total).toInt()
}
Expand Down

0 comments on commit a3c8933

Please sign in to comment.