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
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.

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

public class ByCategory implements Condition {
private Category category;

public ByCategory(Category category) {
this.category = category;
}

@Override
public boolean inCondition(Check check) {
return check.getCostByCategory(category) > 0;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extract amount into field

}

@Override
public int getPointsByCondition(Check check) {
return check.getCostByCategory(category);
}
}
19 changes: 19 additions & 0 deletions src/checkout/ByTrademark.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package checkout;

public class ByTrademark implements Condition {
private Trademark trademark;

public ByTrademark(Trademark trademark) {
this.trademark = trademark;
}

@Override
public boolean inCondition(Check check) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chose better method name

return check.getCostByTrademark(trademark) > 0;
}

@Override
public int getPointsByCondition(Check check) {
return check.getCostByTrademark(trademark);
}
}
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,
MEAT
}
16 changes: 16 additions & 0 deletions src/checkout/Check.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
public class Check {
private List<Product> products = new ArrayList<>();
private int points = 0;
private int discount = 0;

public int getTotalCost() {
int totalCost = 0;
Expand Down Expand Up @@ -33,4 +34,19 @@ int getCostByCategory(Category category) {
.mapToInt(p -> p.price)
.reduce(0, (a, b) -> a + b);
}

int getCostByTrademark(Trademark trademark) {
return products.stream()
.filter(p -> p.trademark == trademark)
.mapToInt(p -> p.price)
.reduce(0, (a, b) -> a + b);
}

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

public int getDiscount() {
return discount;
}
}
22 changes: 9 additions & 13 deletions src/checkout/CheckoutService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package checkout;

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

public class CheckoutService {

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

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

public Check closeCheck() {
for (Offer of : offers) {
of.apply(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);
}
}
offers.add(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 inCondition(Check check);

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

public class Discount implements Reward {
private int discont;

public Discount(int discont) {
this.discont = discont;
}

@Override
public void useReward(Check check, Condition condition) {
int points = condition.getPointsByCondition(check);
check.addDiscount((int) Math.round(points * (discont / 100d)));
}
}
15 changes: 15 additions & 0 deletions src/checkout/Factor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package checkout;

public class Factor implements Reward {
private int factor;

public Factor(int factor) {
this.factor = factor;
}

@Override
public void useReward(Check check, Condition condition) {
int points = condition.getPointsByCondition(check);
check.addPoints(points * (factor - 1));
}
}
16 changes: 0 additions & 16 deletions src/checkout/FactorByCategoryOffer.java

This file was deleted.

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

public class Flat implements Reward {
private int points;

public Flat(int points) {
this.points = points;
}

@Override
public void useReward(Check check, Condition condition) {
check.addPoints(points);
}
}
5 changes: 5 additions & 0 deletions src/checkout/Offer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package checkout;

import java.util.Date;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use LocalDate from java.time


public abstract class Offer {
public abstract void apply(Check check);
public boolean isValid(Date expirationDate) {
return expirationDate.after(new Date());
};
}
11 changes: 10 additions & 1 deletion src/checkout/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@ public class Product {
final int price;
final String name;
Category category;
Trademark trademark;

public Product(int price, String name, Category category) {
this.price = price;
this.name = name;
this.category = category;
}

public Product(int price, String name, Category category, Trademark trademark) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constructor duplication

this.price = price;
this.name = name;
this.category = category;
this.trademark = trademark;
}


public Product(int price, String name) {
this(price, name, null);
this(price, name, null, null);
}
}
Loading