diff --git a/README.md b/README.md deleted file mode 100644 index 71582697..00000000 --- a/README.md +++ /dev/null @@ -1,28 +0,0 @@ -# jv-strategy - ---- -Imagine you have a сhain of stores. You need to implement a discount system for people at some special events of the year like New Year. - -In this task, we'll need help with the Strategy pattern. -First of all, you need to create a `DiscountService` interface with the method `getDiscount()` with the return type `double`. -It must be located in the `core.basesyntax.strategy` package. - -You must create its implementations (with the same names) with such discounts: - -| Implementation name |Discount | -| :---: | :---: | -| DefaultDiscountService| 0 | -| NewYearDiscountService| 20 | -| BirthdayDiscountService| 33 | -| BlackFridayDiscountService| 45 | - -In each of these implementations in result of the execution of the method `getDiscount()` must return a discount according to the table above. -Also, these implementations must be located in the `core.basesyntax.strategy.impl` package. - -The last step will be the creation of a class called `DiscountStrategy` located in the `core.basesyntax` package. It must have method `getDiscountServiceBySpecialEvent(String specialEvent)` with return type `DiscountService` where `specialEvent` can have such values: `"Birthday"`, `"Black Friday"`, `"New Year"`. - -Using `specialEvent`, you must write code that defines the `DiscountService` implementation. - -In case the passed `specialEvent` doesn't have a specific implementation you must return `DefaultDiscountService` by default. - -#### [Try to avoid these common mistakes, while solving task](./checklist.md) diff --git a/src/main/java/core/basesyntax/DiscountStrategy.java b/src/main/java/core/basesyntax/DiscountStrategy.java new file mode 100644 index 00000000..bb108622 --- /dev/null +++ b/src/main/java/core/basesyntax/DiscountStrategy.java @@ -0,0 +1,18 @@ +package core.basesyntax; + +import core.basesyntax.strategy.DiscountService; +import core.basesyntax.strategy.impl.BirthdayDiscountService; +import core.basesyntax.strategy.impl.BlackFridayDiscountService; +import core.basesyntax.strategy.impl.DefaultDiscountService; +import core.basesyntax.strategy.impl.NewYearDiscountService; + +public class DiscountStrategy { + public DiscountService getDiscountServiceBySpecialEvent(String specialEvent) { + return switch (specialEvent) { + case "Birthday" -> new BirthdayDiscountService(); + case "Black Friday" -> new BlackFridayDiscountService(); + case "New Year" -> new NewYearDiscountService(); + default -> new DefaultDiscountService(); + }; + } +} diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java new file mode 100644 index 00000000..a2ae54d3 --- /dev/null +++ b/src/main/java/core/basesyntax/Main.java @@ -0,0 +1,7 @@ +package core.basesyntax; + +public class Main { + public static void main(String[] args) { + + } +} diff --git a/src/main/java/core/basesyntax/strategy/DiscountService.java b/src/main/java/core/basesyntax/strategy/DiscountService.java new file mode 100644 index 00000000..492e1b37 --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/DiscountService.java @@ -0,0 +1,5 @@ +package core.basesyntax.strategy; + +public interface DiscountService { + double getDiscount(); +} diff --git a/src/main/java/core/basesyntax/strategy/impl/BirthdayDiscountService.java b/src/main/java/core/basesyntax/strategy/impl/BirthdayDiscountService.java new file mode 100644 index 00000000..bceb7ca6 --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/impl/BirthdayDiscountService.java @@ -0,0 +1,10 @@ +package core.basesyntax.strategy.impl; + +import core.basesyntax.strategy.DiscountService; + +public class BirthdayDiscountService implements DiscountService { + @Override + public double getDiscount() { + return 33.0; + } +} diff --git a/src/main/java/core/basesyntax/strategy/impl/BlackFridayDiscountService.java b/src/main/java/core/basesyntax/strategy/impl/BlackFridayDiscountService.java new file mode 100644 index 00000000..eca369c8 --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/impl/BlackFridayDiscountService.java @@ -0,0 +1,10 @@ +package core.basesyntax.strategy.impl; + +import core.basesyntax.strategy.DiscountService; + +public class BlackFridayDiscountService implements DiscountService { + @Override + public double getDiscount() { + return 45.0; + } +} diff --git a/src/main/java/core/basesyntax/strategy/impl/DefaultDiscountService.java b/src/main/java/core/basesyntax/strategy/impl/DefaultDiscountService.java new file mode 100644 index 00000000..9c3b522d --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/impl/DefaultDiscountService.java @@ -0,0 +1,10 @@ +package core.basesyntax.strategy.impl; + +import core.basesyntax.strategy.DiscountService; + +public class DefaultDiscountService implements DiscountService { + @Override + public double getDiscount() { + return 0; + } +} diff --git a/src/main/java/core/basesyntax/strategy/impl/NewYearDiscountService.java b/src/main/java/core/basesyntax/strategy/impl/NewYearDiscountService.java new file mode 100644 index 00000000..e799547b --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/impl/NewYearDiscountService.java @@ -0,0 +1,10 @@ +package core.basesyntax.strategy.impl; + +import core.basesyntax.strategy.DiscountService; + +public class NewYearDiscountService implements DiscountService { + @Override + public double getDiscount() { + return 20; + } +}