Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add interface #1318

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/main/java/core/basesyntax/DiscountStrategy.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
8 changes: 0 additions & 8 deletions src/main/java/core/basesyntax/HelloWorld.java

This file was deleted.

13 changes: 13 additions & 0 deletions src/main/java/core/basesyntax/Main.java
Original file line number Diff line number Diff line change
@@ -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);*/
}
}
6 changes: 6 additions & 0 deletions src/main/java/core/basesyntax/strategy/DiscountService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package core.basesyntax.strategy;

public interface DiscountService {
double getDiscount();

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
2 changes: 1 addition & 1 deletion src/test/java/core/basesyntax/StrategyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading