Skip to content

Commit 8cbf06b

Browse files
committed
update local shadow
1 parent 8b89e42 commit 8cbf06b

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

core-kotlin-modules/core-kotlin-10/src/test/kotlin/com/baeldung/variableshadowing/VariableShadowingUnitTest.kt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,24 @@ class VariableShadowingUnitTest{
116116
val price = 100 // local variable
117117
val discountRate = 0.1
118118

119-
fun applyDiscount(price: Int): Double { // Nested function with parameter named 'price'
120-
val updatedDiscountRate = 0.2 // Using a new variable name to avoid shadowing
121-
return price * (1 - updatedDiscountRate) // 'price' here refers to the parameter, not the outer variable
119+
fun applyDiscount(price: Int): Double {
120+
val discountRate = 0.2
121+
return price * (1 - discountRate)
122+
}
123+
124+
fun applyDiscountFood(price: Int): Double {
125+
val discountRate = 0.4
126+
return price * (1 - discountRate)
127+
}
128+
129+
fun applyDiscountDrink(price: Int): Double {
130+
val discountRate = 0.5
131+
return price * (1 - discountRate)
122132
}
123133

124134
assertEquals(80.0, applyDiscount(price))
135+
assertEquals(50.0, applyDiscountDrink(price))
136+
assertEquals(60.0, applyDiscountFood(price))
125137
}
126138

127139
calculateTotalPrice(20)

0 commit comments

Comments
 (0)