diff --git a/src/main/java/core/basesyntax/DiscountStrategy.java b/src/main/java/core/basesyntax/DiscountStrategy.java new file mode 100644 index 00000000..50ec6b83 --- /dev/null +++ b/src/main/java/core/basesyntax/DiscountStrategy.java @@ -0,0 +1,27 @@ +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) { + if (specialEvent == null) { + return new DefaultDiscountService(); + } + + switch (specialEvent.toLowerCase()) { + case "birthday": + return new BirthdayDiscountService(); + case "black friday": + return new BlackFridayDiscountService(); + case "new year": + return new NewYearDiscountService(); + default: + return new DefaultDiscountService(); + } + } +} diff --git a/src/main/java/core/basesyntax/HelloWorld.java b/src/main/java/core/basesyntax/HelloWorld.java deleted file mode 100644 index 97db782b..00000000 --- a/src/main/java/core/basesyntax/HelloWorld.java +++ /dev/null @@ -1,8 +0,0 @@ -package core.basesyntax; - -/** - * Feel free to remove this class and create your own. - */ -public class HelloWorld { - -} diff --git a/src/main/java/core/basesyntax/Main.java b/src/main/java/core/basesyntax/Main.java new file mode 100644 index 00000000..4639fd5f --- /dev/null +++ b/src/main/java/core/basesyntax/Main.java @@ -0,0 +1,13 @@ +package core.basesyntax; + +public class Main { + public static void main(String[] args) { + /*System.out.println("+++"); + + DiscountStrategy discountStrategy = new DiscountStrategy(); + DiscountService discountService = discountStrategy + .getDiscountServiceBySpecialEvent("birthday"); + double discount = discountService.getDiscount(); + System.out.println(discount);*/ + } +} 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..a4987b02 --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/DiscountService.java @@ -0,0 +1,6 @@ +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..63f78c45 --- /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; + } +} 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..aff8bb60 --- /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; + } +} 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; + } +} diff --git a/src/test/java/core/basesyntax/StrategyTest.java b/src/test/java/core/basesyntax/StrategyTest.java index f66af794..10e4a242 100644 --- a/src/test/java/core/basesyntax/StrategyTest.java +++ b/src/test/java/core/basesyntax/StrategyTest.java @@ -26,7 +26,7 @@ public void getDiscountBySpecialEvent_IsOk() { DiscountStrategy discountStrategy = new DiscountStrategy(); double expectedDiscount = specialEventsDiscounts.get(specialEvent); double actualDiscount = discountStrategy.getDiscountServiceBySpecialEvent(specialEvent) - .getDiscount(); + .getDiscount(); Assert.assertEquals("Expected discount of " + specialEvent + " " + expectedDiscount + ",but your discount " + actualDiscount, expectedDiscount, actualDiscount, DELTA);