From 4af954791204f4c7578d94f4d6f2bc6c6e9a2c40 Mon Sep 17 00:00:00 2001 From: Maxim Golyak Date: Tue, 7 Jan 2025 22:03:52 +0200 Subject: [PATCH 1/4] add interface --- src/main/java/core/basesyntax/strategy/DiscountService.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 src/main/java/core/basesyntax/strategy/DiscountService.java 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..4eab3ec9 --- /dev/null +++ b/src/main/java/core/basesyntax/strategy/DiscountService.java @@ -0,0 +1,5 @@ +package core.basesyntax.strategy; + +public interface DiscountService { + double getDiscount(double prise); +} From 9ebd0aec5f7ad39650f166955bf419ffdde19cd5 Mon Sep 17 00:00:00 2001 From: Maxim Golyak Date: Wed, 8 Jan 2025 22:23:54 +0200 Subject: [PATCH 2/4] added Service --- .../core/basesyntax/DiscountStrategy.java | 47 +++++++++++++++++++ .../basesyntax/strategy/DiscountService.java | 2 +- .../impl/BirthdayDiscountService.java | 10 ++++ .../impl/BlackFridayDiscountService.java | 10 ++++ .../strategy/impl/DefaultDiscountService.java | 10 ++++ .../strategy/impl/NewYearDiscountService.java | 10 ++++ 6 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 src/main/java/core/basesyntax/DiscountStrategy.java create mode 100644 src/main/java/core/basesyntax/strategy/impl/BirthdayDiscountService.java create mode 100644 src/main/java/core/basesyntax/strategy/impl/BlackFridayDiscountService.java create mode 100644 src/main/java/core/basesyntax/strategy/impl/DefaultDiscountService.java create mode 100644 src/main/java/core/basesyntax/strategy/impl/NewYearDiscountService.java diff --git a/src/main/java/core/basesyntax/DiscountStrategy.java b/src/main/java/core/basesyntax/DiscountStrategy.java new file mode 100644 index 00000000..eeb70e99 --- /dev/null +++ b/src/main/java/core/basesyntax/DiscountStrategy.java @@ -0,0 +1,47 @@ +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 { + private int birthday = 33; + private int blackFriday = 45; + private int newYear = 20; + + + + private int noDiscount = 0; + + public int getBirthday() { + return birthday; + } + + public int getBlackFriday() { + return blackFriday; + } + + public int getNewYear() { + return newYear; + } + public int getNoDiscount() { + return noDiscount; + } + + public DiscountService getDiscountServiceBySpecialEvent(String SpecialEvent) { + switch (SpecialEvent) { + case "NewYear" : + return new NewYearDiscountService(); + case "Birthday" : + return new BirthdayDiscountService(); + case "BlackFriday" : + return new BlackFridayDiscountService(); + case "Defaul" : + default: + return new DefaultDiscountService(); + + } + } +} diff --git a/src/main/java/core/basesyntax/strategy/DiscountService.java b/src/main/java/core/basesyntax/strategy/DiscountService.java index 4eab3ec9..ef79620d 100644 --- a/src/main/java/core/basesyntax/strategy/DiscountService.java +++ b/src/main/java/core/basesyntax/strategy/DiscountService.java @@ -1,5 +1,5 @@ package core.basesyntax.strategy; public interface DiscountService { - double getDiscount(double prise); + double getDiscount(double prise, double discount); } 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..6f81bda7 --- /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(double prise, double discount) { + return (double) ((prise / 100) * discount); + } +} 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..6cfd4686 --- /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(double prise, double discount) { + return (double) ((prise / 100) * discount); + } +} 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..b4e023da --- /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(double prise, double discount) { + return (double) ((prise / 100) * discount); + } +} 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..9f692de0 --- /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(double prise, double discount) { + return (double) ((prise / 100) * discount); + } +} From 97ce727c8b4816f4c4cad315402cc2a211af9edc Mon Sep 17 00:00:00 2001 From: Maxim Golyak Date: Thu, 9 Jan 2025 23:26:36 +0200 Subject: [PATCH 3/4] added Se --- .../core/basesyntax/DiscountStrategy.java | 38 +++++-------------- .../basesyntax/strategy/DiscountService.java | 4 +- .../impl/BirthdayDiscountService.java | 4 +- .../impl/BlackFridayDiscountService.java | 4 +- .../strategy/impl/DefaultDiscountService.java | 4 +- .../strategy/impl/NewYearDiscountService.java | 4 +- 6 files changed, 19 insertions(+), 39 deletions(-) diff --git a/src/main/java/core/basesyntax/DiscountStrategy.java b/src/main/java/core/basesyntax/DiscountStrategy.java index eeb70e99..50ec6b83 100644 --- a/src/main/java/core/basesyntax/DiscountStrategy.java +++ b/src/main/java/core/basesyntax/DiscountStrategy.java @@ -7,41 +7,21 @@ import core.basesyntax.strategy.impl.NewYearDiscountService; public class DiscountStrategy { - private int birthday = 33; - private int blackFriday = 45; - private int newYear = 20; + public DiscountService getDiscountServiceBySpecialEvent(String specialEvent) { + if (specialEvent == null) { + return new DefaultDiscountService(); + } - - private int noDiscount = 0; - - public int getBirthday() { - return birthday; - } - - public int getBlackFriday() { - return blackFriday; - } - - public int getNewYear() { - return newYear; - } - public int getNoDiscount() { - return noDiscount; - } - - public DiscountService getDiscountServiceBySpecialEvent(String SpecialEvent) { - switch (SpecialEvent) { - case "NewYear" : - return new NewYearDiscountService(); - case "Birthday" : + switch (specialEvent.toLowerCase()) { + case "birthday": return new BirthdayDiscountService(); - case "BlackFriday" : + case "black friday": return new BlackFridayDiscountService(); - case "Defaul" : + case "new year": + return new NewYearDiscountService(); default: return new DefaultDiscountService(); - } } } diff --git a/src/main/java/core/basesyntax/strategy/DiscountService.java b/src/main/java/core/basesyntax/strategy/DiscountService.java index ef79620d..ee92f4d2 100644 --- a/src/main/java/core/basesyntax/strategy/DiscountService.java +++ b/src/main/java/core/basesyntax/strategy/DiscountService.java @@ -1,5 +1,5 @@ package core.basesyntax.strategy; public interface DiscountService { - double getDiscount(double prise, double discount); -} + double getDiscount(); +} \ No newline at end of file diff --git a/src/main/java/core/basesyntax/strategy/impl/BirthdayDiscountService.java b/src/main/java/core/basesyntax/strategy/impl/BirthdayDiscountService.java index 6f81bda7..63f78c45 100644 --- a/src/main/java/core/basesyntax/strategy/impl/BirthdayDiscountService.java +++ b/src/main/java/core/basesyntax/strategy/impl/BirthdayDiscountService.java @@ -4,7 +4,7 @@ public class BirthdayDiscountService implements DiscountService { @Override - public double getDiscount(double prise, double discount) { - return (double) ((prise / 100) * discount); + 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 index 6cfd4686..aff8bb60 100644 --- a/src/main/java/core/basesyntax/strategy/impl/BlackFridayDiscountService.java +++ b/src/main/java/core/basesyntax/strategy/impl/BlackFridayDiscountService.java @@ -4,7 +4,7 @@ public class BlackFridayDiscountService implements DiscountService { @Override - public double getDiscount(double prise, double discount) { - return (double) ((prise / 100) * discount); + 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 index b4e023da..9c3b522d 100644 --- a/src/main/java/core/basesyntax/strategy/impl/DefaultDiscountService.java +++ b/src/main/java/core/basesyntax/strategy/impl/DefaultDiscountService.java @@ -4,7 +4,7 @@ public class DefaultDiscountService implements DiscountService { @Override - public double getDiscount(double prise, double discount) { - return (double) ((prise / 100) * discount); + 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 index 9f692de0..e799547b 100644 --- a/src/main/java/core/basesyntax/strategy/impl/NewYearDiscountService.java +++ b/src/main/java/core/basesyntax/strategy/impl/NewYearDiscountService.java @@ -4,7 +4,7 @@ public class NewYearDiscountService implements DiscountService { @Override - public double getDiscount(double prise, double discount) { - return (double) ((prise / 100) * discount); + public double getDiscount() { + return 20; } } From f6ee0bad4b7b86eeddfe13e974692890bb535522 Mon Sep 17 00:00:00 2001 From: Maxim Golyak Date: Fri, 10 Jan 2025 15:16:30 +0200 Subject: [PATCH 4/4] added getDiscount --- src/main/java/core/basesyntax/HelloWorld.java | 8 -------- src/main/java/core/basesyntax/Main.java | 13 +++++++++++++ .../core/basesyntax/strategy/DiscountService.java | 3 ++- src/test/java/core/basesyntax/StrategyTest.java | 2 +- 4 files changed, 16 insertions(+), 10 deletions(-) delete mode 100644 src/main/java/core/basesyntax/HelloWorld.java create mode 100644 src/main/java/core/basesyntax/Main.java 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 index ee92f4d2..a4987b02 100644 --- a/src/main/java/core/basesyntax/strategy/DiscountService.java +++ b/src/main/java/core/basesyntax/strategy/DiscountService.java @@ -2,4 +2,5 @@ public interface DiscountService { double getDiscount(); -} \ No newline at end of file + +} 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);