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

Silpo points system #9

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
9cee2d9
added realisation to FactorByCategory and AnyGoodsOffer
MyroslavSokolov Mar 4, 2019
92bae54
created check for expiratin date in offer (Template Method)
MyroslavSokolov Mar 4, 2019
784006b
created halfPrise offer
MyroslavSokolov Mar 4, 2019
65c80d7
can use offer before making goods
MyroslavSokolov Mar 5, 2019
e015a4d
created interfaces Reward and Condition
MyroslavSokolov Mar 5, 2019
b46e81c
Fixed bug with using offer. Now you can use it anytime you want
MyroslavSokolov Mar 5, 2019
ee1a201
realised FactorByCategoryReward
MyroslavSokolov Mar 5, 2019
e176770
created ByCategory condition
MyroslavSokolov Mar 5, 2019
bb97f90
created ByTotalCostCondition
MyroslavSokolov Mar 5, 2019
0363047
realised DiscountReward
MyroslavSokolov Mar 5, 2019
59e244d
add date expiration\
MyroslavSokolov Mar 5, 2019
d948d38
deleted extra classes
MyroslavSokolov Mar 5, 2019
ee6bd69
fixed bug with using offer during making bargains
MyroslavSokolov Mar 5, 2019
7f1a668
refractore code
MyroslavSokolov Mar 5, 2019
31a8c23
added AnyGoodsReward and fixed tests
MyroslavSokolov Mar 5, 2019
d32ed2b
fixed bugs with DiscountReward tests
MyroslavSokolov Mar 5, 2019
b425a2d
after fork\
MyroslavSokolov Mar 5, 2019
0ed7634
used telescopic constructor in Offer, moved Offer's field dateOfCheck…
MyroslavSokolov Mar 6, 2019
e0a03c8
deleted AnyGoodsReward because it dublicates FlatReward
MyroslavSokolov Mar 6, 2019
88a14d3
can use several offers
MyroslavSokolov Mar 6, 2019
67e838b
cleaned code
MyroslavSokolov Mar 6, 2019
97727f5
cleaned code
MyroslavSokolov Mar 6, 2019
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.

Binary file added lib/hamcrest-core-1.3.jar
Binary file not shown.
Binary file added lib/junit-4.12.jar
Binary file not shown.
Binary file added lib/junit-jupiter-api-5.0.0.jar
Binary file not shown.
Binary file added lib/opentest4j-1.0.0.jar
Binary file not shown.
20 changes: 20 additions & 0 deletions oop-workshop.iml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,25 @@
</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://$MODULE_DIR$/lib/junit-4.12.jar!/" />
<root url="jar://$MODULE_DIR$/lib/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library" scope="TEST">
<library name="JUnit5">
<CLASSES>
<root url="jar://$MODULE_DIR$/lib/junit-jupiter-api-5.0.0.jar!/" />
<root url="jar://$MODULE_DIR$/lib/opentest4j-1.0.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
16 changes: 0 additions & 16 deletions src/checkout/AnyGoodsOffer.java

This file was deleted.

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

public enum Category {
MILK
MILK,
MEET,
FRUIT,
VEGETABLE
}
21 changes: 18 additions & 3 deletions src/checkout/Check.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
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 LocalDate dateOfCheck = LocalDate.now();

public int getTotalCost() {
int totalCost = 0;
for (Product product : this.products) {
totalCost += product.price;
}
return totalCost;
return (this.discount > 0) ? totalCost - (totalCost * discount / 100) : totalCost;
}

void addProduct(Product product) {
Expand All @@ -23,14 +26,26 @@ public int getTotalPoints() {
return getTotalCost() + points;
}

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

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

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

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

LocalDate getDateOfCheck() {
return this.dateOfCheck;
}
}
27 changes: 15 additions & 12 deletions src/checkout/CheckoutService.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package checkout;

import com.sun.javaws.exceptions.OfflineLaunchException;

import java.util.ArrayList;

public class CheckoutService {

private Check check;

private ArrayList<Offer> currentOffers = new ArrayList<>();

public void openCheck() {
check = new Check();
}
Expand All @@ -16,23 +22,20 @@ public void addProduct(Product product) {
}

public Check closeCheck() {
if (currentOffers.size() >= 1) {
this.currentOffers.forEach(item -> {
if (item.isOfferValid(check))
item.applyOffer(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);
}
}
this.currentOffers.add(offer);
}


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

This file was deleted.

33 changes: 30 additions & 3 deletions src/checkout/Offer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
package checkout;

public abstract class Offer {
public abstract void apply(Check check);
}
import checkout.offer_interfaces.Condition;
import checkout.offer_interfaces.Reward;
import java.time.LocalDate;

public class Offer {

private LocalDate expiresDate;
private Reward reward;
private Condition condition;

public Offer(LocalDate expireDate, Reward rewardType) {
this(expireDate, rewardType, null );
}

public Offer(LocalDate expireDate, Reward rewardType, Condition conditionType) {
this.expiresDate = expireDate;
this.reward = rewardType;
myroslavsok marked this conversation as resolved.
Show resolved Hide resolved
this.condition = conditionType;
}

void applyOffer(Check check) {
if (this.expiresDate.isAfter(check.getDateOfCheck()))
reward.applyReward(check);
}

boolean isOfferValid(Check check) {
return (condition == null) || condition.checkCondition(check);
}

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

import checkout.Category;
import checkout.Check;
import checkout.offer_interfaces.Condition;

public class ByCategory implements Condition {

private Category requiredCategory;
private int requiredAmount;

public ByCategory(Category requiredCategory, int requiredAmount) {
this.requiredCategory = requiredCategory;
this.requiredAmount = requiredAmount;
}

public ByCategory(Category requiredCategory) {
this.requiredCategory = requiredCategory;
this.requiredAmount = 0;
}

@Override
public boolean checkCondition(Check check) {
return (this.requiredAmount < check.getCostByCategory(this.requiredCategory));
}
}
20 changes: 20 additions & 0 deletions src/checkout/offer_conditions/ByTotalCost.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package checkout.offer_conditions;

import checkout.Check;
import checkout.offer_interfaces.Condition;

public class ByTotalCost implements Condition {

private int requiredAmount;

public ByTotalCost(int requiredAmount) {
this.requiredAmount = requiredAmount;
}

@Override
public boolean checkCondition(Check check) {
return this.requiredAmount < check.getTotalCost();
}

}

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

import checkout.Check;

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

import checkout.Check;

public interface Reward {
void applyReward(Check check);
}
Loading