-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
27 additions
and
28 deletions.
There are no files selected for viewing
17 changes: 11 additions & 6 deletions
17
...trategyPatternPractice/Task/src/main/kotlin/jetbrains/refactoring/course/patterns/Main.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
package jetbrains.refactoring.course.patterns | ||
|
||
import jetbrains.refactoring.course.patterns.processor.PaymentProcessor | ||
import jetbrains.refactoring.course.patterns.strategy.BitcoinPayment | ||
import jetbrains.refactoring.course.patterns.strategy.CreditCardPayment | ||
import jetbrains.refactoring.course.patterns.strategy.PayPalPayment | ||
|
||
fun main() { | ||
val order1 = Order(100.0, "CreditCard") | ||
val order2 = Order(50.0, "PayPal") | ||
val order3 = Order(200.0, "Bitcoin") | ||
val creditCardPaymentProcessor = PaymentProcessor(CreditCardPayment()) | ||
val paypalPaymentProcessor = PaymentProcessor(PayPalPayment()) | ||
val bitcoinPaymentProcessor = PaymentProcessor(BitcoinPayment()) | ||
|
||
order1.processPayment() | ||
order2.processPayment() | ||
order3.processPayment() | ||
creditCardPaymentProcessor.processOrderPayment(100.0) | ||
paypalPaymentProcessor.processOrderPayment(50.0) | ||
bitcoinPaymentProcessor.processOrderPayment(200.0) | ||
} |
20 changes: 2 additions & 18 deletions
20
...rategyPatternPractice/Task/src/main/kotlin/jetbrains/refactoring/course/patterns/Order.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,4 @@ | ||
package jetbrains.refactoring.course.patterns | ||
|
||
class Order(val totalAmount: Double, val paymentMethod: String) { | ||
fun processPayment() { | ||
when (paymentMethod) { | ||
"CreditCard" -> { | ||
println("Processing credit card payment: $totalAmount") | ||
} | ||
"PayPal" -> { | ||
println("Processing PayPal payment: $totalAmount") | ||
} | ||
"Bitcoin" -> { | ||
println("Processing Bitcoin payment: $totalAmount") | ||
} | ||
else -> { | ||
println("Invalid payment method") | ||
} | ||
} | ||
} | ||
} | ||
// The Order class is no longer necessary | ||
class Order(val totalAmount: Double) |
6 changes: 5 additions & 1 deletion
6
.../Task/src/main/kotlin/jetbrains/refactoring/course/patterns/processor/PaymentProcessor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
package jetbrains.refactoring.course.patterns.processor | ||
|
||
// TODO | ||
import jetbrains.refactoring.course.patterns.strategy.PaymentStrategy | ||
|
||
class PaymentProcessor(private val paymentStrategy: PaymentStrategy) { | ||
fun processOrderPayment(amount: Double) = paymentStrategy.processPayment(amount) | ||
} |
4 changes: 3 additions & 1 deletion
4
...ice/Task/src/main/kotlin/jetbrains/refactoring/course/patterns/strategy/BitcoinPayment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
package jetbrains.refactoring.course.patterns.strategy | ||
|
||
// TODO | ||
class BitcoinPayment: PaymentStrategy { | ||
override fun processPayment(amount: Double) = println("Processing Bitcoin payment: $amount") | ||
} |
4 changes: 3 additions & 1 deletion
4
.../Task/src/main/kotlin/jetbrains/refactoring/course/patterns/strategy/CreditCardPayment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
package jetbrains.refactoring.course.patterns.strategy | ||
|
||
// TODO | ||
class CreditCardPayment: PaymentStrategy { | ||
override fun processPayment(amount: Double) = println("Processing credit card payment: $amount") | ||
} |
4 changes: 3 additions & 1 deletion
4
...tice/Task/src/main/kotlin/jetbrains/refactoring/course/patterns/strategy/PayPalPayment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
package jetbrains.refactoring.course.patterns.strategy | ||
|
||
// TODO | ||
class PayPalPayment: PaymentStrategy { | ||
override fun processPayment(amount: Double) = println("Processing PayPal payment: $amount") | ||
} |