Skip to content

Commit

Permalink
fix: add a reference solution
Browse files Browse the repository at this point in the history
  • Loading branch information
GirZ0n committed Nov 6, 2023
1 parent 4ba7dc7 commit 982e5ab
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 28 deletions.
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)
}
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)
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)
}
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")
}
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")
}
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")
}

0 comments on commit 982e5ab

Please sign in to comment.