Skip to content
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
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 0 additions & 16 deletions src/checkout/AnyGoodsOffer.java

This file was deleted.

5 changes: 5 additions & 0 deletions src/checkout/Brand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package checkout;

public enum Brand {
VOLOSHKOVE_POLE
}
22 changes: 22 additions & 0 deletions src/checkout/ByBrand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package checkout;

public class ByBrand implements Condition {
Brand brand;

public ByBrand(Brand brand) {
setBrand(brand);
}

@Override
public boolean checkCondition(Check check) {
boolean marker = false;
for (Product p : check.getProducts()) {
if (p.brand == brand) marker = true;
}
return marker;
}

public void setBrand(Brand brand) {
this.brand = brand;
}
}
22 changes: 22 additions & 0 deletions src/checkout/ByCategory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package checkout;

public class ByCategory implements Condition {
Category category;

public ByCategory(Category category) {
setCategory(category);
}

@Override
public boolean checkCondition(Check check) {
boolean marker = false;
for(Product p : check.getProducts()) {
if (p.category == category) marker = true;
}
return marker;
}

public void setCategory(Category category) {
this.category = category;
}
}
26 changes: 26 additions & 0 deletions src/checkout/Check.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package checkout;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

public class Check {
private List<Product> products = new ArrayList<>();
private int points = 0;
private int discount = 0;
private static LocalDate todayDate = LocalDate.now();

public int getTotalCost() {
int totalCost = 0;
Expand All @@ -15,6 +18,10 @@ public int getTotalCost() {
return totalCost;
}

public int getTotalCostWithDiscount() {
return getTotalCost() - discount/10;
}

void addProduct(Product product) {
products.add(product);
}
Expand All @@ -33,4 +40,23 @@ int getCostByCategory(Category category) {
.mapToInt(p -> p.price)
.reduce(0, (a, b) -> a + b);
}

public int getPointsForBrand(Brand brand) {
return products.stream()
.filter(p -> p.brand == brand)
.mapToInt(p -> p.price)
.reduce(0, (a,b)-> a+b);
}

public void addDiscount(int discount) {
this.discount = discount;
}

public List<Product> getProducts() {
return products;
}

public static LocalDate getTodayDate() {
return todayDate;
}
}
25 changes: 13 additions & 12 deletions src/checkout/CheckoutService.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package checkout;

import java.util.ArrayList;
import java.util.List;

public class CheckoutService {

private Check check;
private List<Offer> offers = new ArrayList<>();

public void openCheck() {
offers = new ArrayList<>();
check = new Check();
}

Expand All @@ -16,23 +21,19 @@ public void addProduct(Product product) {
}

public Check closeCheck() {
useAllOffer();
Check closedCheck = check;
check = null;
return closedCheck;
}

private void useAllOffer() {
if(this.offers.size()!=0) this.offers.forEach(offer ->
offer.apply(check));
}

public void useOffer(Offer offer) {
offer.apply(check);
if (offer instanceof FactorByCategoryOffer) {
FactorByCategoryOffer fbOffer = (FactorByCategoryOffer) offer;
int points = check.getCostByCategory(fbOffer.category);
check.addPoints(points * (fbOffer.factor - 1));
} else {
if (offer instanceof AnyGoodsOffer) {
AnyGoodsOffer agOffer = (AnyGoodsOffer) offer;
if (agOffer.totalCost <= check.getTotalCost())
check.addPoints(agOffer.points);
}
}
if(check != null)
this.offers.add(offer);
}
}
5 changes: 5 additions & 0 deletions src/checkout/Condition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package checkout;

public interface Condition {
boolean checkCondition(Check check);
}
25 changes: 25 additions & 0 deletions src/checkout/DiscountByBrand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package checkout;

public class DiscountByBrand implements Reward {
private double discount;
private Brand brand;

public DiscountByBrand(double discount, Brand brand) {
setBrand(brand);
setDiscount(discount);
}

@Override
public void apply(Check check) {
Double points = check.getPointsForBrand(brand) * discount * 10;
check.addDiscount(points.intValue());
}

public void setBrand(Brand brand) {
this.brand = brand;
}

public void setDiscount(double discount) {
this.discount = discount;
}
}
16 changes: 0 additions & 16 deletions src/checkout/FactorByCategoryOffer.java

This file was deleted.

25 changes: 25 additions & 0 deletions src/checkout/FactorRewardByCategory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package checkout;

public class FactorRewardByCategory implements Reward {
private int factor;
Category category;

public FactorRewardByCategory(int factor, Category category) {
setCategory(category);
setFactor(factor);
}

@Override
public void apply(Check check) {
int points = check.getCostByCategory(category);
check.addPoints(points * (factor-1));
}

public void setFactor(int factor) {
this.factor = factor;
}

public void setCategory(Category category) {
this.category = category;
}
}
18 changes: 18 additions & 0 deletions src/checkout/FlatReward.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package checkout;

public class FlatReward implements Reward {
private int reward;

public FlatReward (int reward) {
setReward(reward);
}

@Override
public void apply(Check check) {
check.addPoints(reward);
}

public void setReward(int reward) {
this.reward = reward;
}
}
Loading