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

InCamp Task #7

Open
wants to merge 9 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
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.

22 changes: 22 additions & 0 deletions oop-workshop.iml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,27 @@
</library>
</orderEntry>
<orderEntry type="library" name="org.hamcrest:hamcrest-core:2.1" level="project" />
<orderEntry type="module-library" scope="TEST">
<library name="JUnit4">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" scope="TEST">
<library name="JUnit5.3">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.4.0/junit-jupiter-api-5.4.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.0.0/apiguardian-api-1.0.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.1.1/opentest4j-1.1.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.4.0/junit-platform-commons-1.4.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
16 changes: 0 additions & 16 deletions src/checkout/AnyGoodsOffer.java

This file was deleted.

3 changes: 2 additions & 1 deletion src/checkout/Category.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package checkout;

public enum Category {
MILK
MILK,
BRED
}
36 changes: 30 additions & 6 deletions src/checkout/Check.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

public class Check {
private List<Product> products = new ArrayList<>();
private List<Offer> offers = new ArrayList<>();
private int points = 0;
private double discount = 0;

public int getTotalCost() {
int totalCost = 0;
public int getTotalPrice() {
int totalPrice = 0;
for (Product product : this.products) {
totalCost += product.price;
totalPrice += product.price;
}
return totalPrice;
}

public double getTotalCost() {
double totalCost = 0;
totalCost += getTotalPrice();
totalCost -= discount;
return totalCost;
}

Expand All @@ -20,17 +30,31 @@ void addProduct(Product product) {
}

public int getTotalPoints() {
return getTotalCost() + points;
return getTotalPrice() + points;
}

void addPoints(int points) {
this.points += points;
}

int getCostByCategory(Category category) {
void useOffers(Check check) {
for (Offer offer : offers) {
offer.apply(check);
}
}

void addOffer(Offer offer) {
offers.add(offer);
}

int getSubCost(Predicate<Product> predicate) {
return products.stream()
.filter(p -> p.category == category)
.filter(predicate)
.mapToInt(p -> p.price)
.reduce(0, (a, b) -> a + b);
}

void addDiscount(double discount) {
this.discount += discount;
}
}
14 changes: 2 additions & 12 deletions src/checkout/CheckoutService.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,13 @@ public void addProduct(Product product) {
}

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

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);
}
}
check.addOffer(offer);
}
}
7 changes: 7 additions & 0 deletions src/checkout/Condition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package checkout;

public interface Condition {
boolean checkCondition(Check check);

int getCostForCondition(Check check);
}
29 changes: 29 additions & 0 deletions src/checkout/ConditionByCategory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package checkout;

import java.util.function.Predicate;

public class ConditionByCategory implements Condition {
Category category;
int totalCost;
Predicate<Product> predicate = p -> p.category == category;

public ConditionByCategory(Category category, int totalCost) {
this.category = category;
this.totalCost = totalCost;
}

public ConditionByCategory(Category category) {
this.category = category;
this.totalCost = 0;
}

@Override
public boolean checkCondition(Check check) {
return check.getSubCost(predicate) > totalCost;
}

@Override
public int getCostForCondition(Check check) {
return check.getSubCost(predicate);
}
}
29 changes: 29 additions & 0 deletions src/checkout/ConditionByTradeMark.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package checkout;

import java.util.function.Predicate;

public class ConditionByTradeMark implements Condition {
TradeMark tradeMark;
int totalCost;
Predicate<Product> productPredicate = p -> p.tradeMark == tradeMark;

public ConditionByTradeMark(TradeMark tradeMark, int totalCost) {
this.tradeMark = tradeMark;
this.totalCost = totalCost;
}

public ConditionByTradeMark(TradeMark tradeMark) {
this.tradeMark = tradeMark;
this.totalCost = 0;
}

@Override
public boolean checkCondition(Check check) {
return check.getSubCost(productPredicate) > totalCost;
}

@Override
public int getCostForCondition(Check check) {
return check.getSubCost(productPredicate);
}
}
17 changes: 17 additions & 0 deletions src/checkout/ConditionOfTotalCost.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package checkout;

public class ConditionOfTotalCost implements Condition {
int totalCost;

public ConditionOfTotalCost(int totalCost) {
this.totalCost = totalCost;
}

public boolean checkCondition(Check check) {
return totalCost <= check.getTotalCost();
}

public int getCostForCondition(Check check) {
return check.getTotalPrice();
}
}
13 changes: 13 additions & 0 deletions src/checkout/DiscountReward.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package checkout;

public class DiscountReward implements Reward {
int discount;

public DiscountReward(int discount) {
this.discount = discount;
}

public void applyReward(Check check, int cost) {
check.addDiscount(cost * discount / 100.0);
}
}
16 changes: 0 additions & 16 deletions src/checkout/FactorByCategoryOffer.java

This file was deleted.

Loading