Skip to content

Commit

Permalink
Rework task for Strategy section
Browse files Browse the repository at this point in the history
  • Loading branch information
onewhl committed Oct 30, 2023
1 parent c241759 commit 945b837
Show file tree
Hide file tree
Showing 50 changed files with 135 additions and 451 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
type: edu
custom_name: Strategy Pattern
files:
- name: src/main/kotlin/jetbrains/refactoring/course/patterns/strategy/PaymentStrategy.kt
visible: true
- name: src/main/kotlin/jetbrains/refactoring/course/patterns/Main.kt
visible: true
- name: src/main/kotlin/jetbrains/refactoring/course/patterns/Order.kt
visible: true
- name: src/main/kotlin/jetbrains/refactoring/course/patterns/processor/PaymentProcessor.kt
visible: true
- name: src/main/kotlin/jetbrains/refactoring/course/patterns/strategy/PayPalPayment.kt
visible: true
- name: src/main/kotlin/jetbrains/refactoring/course/patterns/strategy/BitcoinPayment.kt
visible: true
- name: src/main/kotlin/jetbrains/refactoring/course/patterns/strategy/CreditCardPayment.kt
visible: true
- name: test/Tests.kt
visible: false
- name: test/PaymentStrategyClass.kt
visible: false
- name: test/PaymentProcessorClass.kt
visible: false
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
id: 71899853
63 changes: 63 additions & 0 deletions RefactoringToDesignPatterns/StrategyPatternPractice/Task/task.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
### Task

- Create a base interface `PaymentStrategy` for payment strategy classes with the `processPayment(amount: Double)`
method.
- Implement a separate class for each payment method: `CreditCardPayment`, `PayPalPayment`, and `BitcoinPayment`.
Each of these classes should implement the base `PaymentStrategy` interface and override the `processPayment()`
method.
- Create a `PaymentProcessor` class that encapsulates the payment behavior.
This class should take as a constructor parameter `paymentStrategy: PaymentStrategy`
and should contain the `processOrderPayment` method, which invokes the `processPayment` method from `paymentStrategy`.
- Within the `Main::main` method, for every payment type, instantiate a `PaymentProcessor`. Ensure you pass the
corresponding payment strategy during the object's creation.

By using the **Strategy** design pattern, the payment processing logic is separated from the `Order` class,
making it more flexible and maintainable.
Adding new payment methods or modifying the existing ones is easier and doesn't affect the `Order` class.
The client code also becomes cleaner, as it focuses on creating instances of `PaymentProcessor` with the desired payment
strategies.

### Hints

<div class="hint" title="Where to start?">

The file where you should write the code is already open.
Please, create an interface named `PaymentStrategy` and add `processPayment(amount: Double)` method.
</div>

<div class="hint" title="How should PaymentProcessor class look?">

Here's the PaymentProcessor code:

```kotlin
class PaymentProcessor(private val paymentStrategy: PaymentStrategy) {
fun processOrderPayment(amount: Double) = paymentStrategy.processPayment(amount)
}
```

</div>

<div class="hint" title="How should PaymentStrategy subclasses look?">

For example, for a credit card payment type, the code would be:

```kotlin
class CreditCardPayment : PaymentStrategy {
override fun processPayment(amount: Double) = println("Processing credit card payment: $amount")
}
```

</div>

<div class="hint" title="How to fix main method?">

In the main method, you should instantiate a `PaymentProcessor` object and provide the appropriate payment strategy as
an
argument. For instance, for a credit card payment type, the code would be:

```kotlin
val creditCardPayment = PaymentProcessor(CreditCardPayment())
creditCardPayment.processOrderPayment(100.0)
```

</div>
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,51 @@ class StrategyPatternTest : BaseIjTestClass() {
}
}

@Test
fun paymentProcessorClassTest() {
Assertions.assertDoesNotThrow(
{
val clazz = paymentProcessorClass.checkBaseDefinition()
paymentProcessorClass.checkDeclaredMethods(clazz)
paymentProcessorClass.checkFieldsDefinition(clazz)
},
"Please, create a PaymentProcessor class with a constructor parameter paymentStrategy and processOrderPayment method"
)
}

@Test
fun bitcoinPaymentClassTest() {
Assertions.assertDoesNotThrow(
{
val clazz = bitcoinPaymentClass.checkBaseDefinition()
bitcoinPaymentClass.checkDeclaredMethods(clazz)
},
"Please, create a BitcoinPayment class with processPayment method"
)
}

@Test
fun creditCardPaymentClassTest() {
Assertions.assertDoesNotThrow(
{
val clazz = creditCardPaymentClass.checkBaseDefinition()
creditCardPaymentClass.checkDeclaredMethods(clazz)
},
"Please, create a CreditCardPayment class with processPayment method"
)
}

@Test
fun payPalPaymentClassTest() {
Assertions.assertDoesNotThrow(
{
val clazz = payPalPaymentClass.checkBaseDefinition()
payPalPaymentClass.checkDeclaredMethods(clazz)
},
"Please, create a PayPalPayment class with processPayment method"
)
}

@Test
fun testCreatedInstancesInMainMethod() {
setUp()
Expand Down Expand Up @@ -68,4 +113,4 @@ class StrategyPatternTest : BaseIjTestClass() {
"Please, invoke the $method method of Bitcoin Payment in the main method"
}
}
}
}

This file was deleted.

This file was deleted.

14 changes: 0 additions & 14 deletions RefactoringToDesignPatterns/StrategyPatternPractice/Task1/task.md

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

34 changes: 0 additions & 34 deletions RefactoringToDesignPatterns/StrategyPatternPractice/Task2/task.md

This file was deleted.

This file was deleted.

Loading

0 comments on commit 945b837

Please sign in to comment.