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 5 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
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
}
39 changes: 33 additions & 6 deletions src/checkout/Check.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,59 @@

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

public int getTotalCost() {
int totalCost = 0;
private double discount = 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;
}

void addProduct(Product product) {
products.add(product);
}

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

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

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

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

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

int getCostByOutlet(Outlet outlet) {
Copy link
Owner

Choose a reason for hiding this comment

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

Implement generic getSubCost method and pass Predicate<Product> as parameter.

return products.stream()
.filter(p -> p.outlet == outlet)
.mapToInt(p -> p.price)
.reduce(0, (a,b) -> a + b);
}

void addDiscount(double discount) {
this.discount += discount;
}
}
17 changes: 5 additions & 12 deletions src/checkout/CheckoutService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package checkout;

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

public class CheckoutService {

private Check check;
Expand All @@ -16,23 +19,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);
}
22 changes: 22 additions & 0 deletions src/checkout/ConditionByCategory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package checkout;

public class ConditionByCategory implements Condition {
Category category;
public ConditionByCategory(Category category) {
this.category = category;
}

@Override
public boolean checkCondition(Check check) {
if(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.

configure required amount

return true;
}else{
return false;
}
}

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

public class ConditionByOutlet implements Condition {
Outlet outlet;
public ConditionByOutlet(Outlet outlet) {
this.outlet = outlet;
}

@Override
public boolean checkCondition(Check check) {
if(check.getCostByOutlet(outlet) != 0){
Copy link
Owner

Choose a reason for hiding this comment

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

configure required amount

return true;
}else{
return false;
}
}

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

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

public boolean checkCondition(Check check) {
if (check.getTotalCost() < totalCost) {
return false;
}else{
return true;
}
}
public int getCostForCondition(Check check) {
return check.getTotalPrice();
}
}
12 changes: 12 additions & 0 deletions src/checkout/DiscountReward.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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.

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

public class FactorReward implements Reward {
int factor;
public FactorReward(int factor) {
this.factor = factor;
}

public void applyReward(Check check, int cost){
check.addPoints(cost*(factor-1));
}
}
13 changes: 13 additions & 0 deletions src/checkout/FlatReward.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package checkout;

public class FlatReward implements Reward{
int flat;
public FlatReward(int flat) {
this.flat = flat;
}

public void applyReward(Check check, int cost){
check.addPoints(flat);
}
}

21 changes: 19 additions & 2 deletions src/checkout/Offer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
package checkout;

public abstract class Offer {
public abstract void apply(Check check);
import java.time.LocalDate;

public class Offer {
private LocalDate expiredDate;
private Reward rewardType;
private Condition conditionType;


public Offer(LocalDate expiredDate, Reward rewardType, Condition conditionType) {
this.expiredDate = expiredDate;
this.rewardType = rewardType;
Copy link
Owner

Choose a reason for hiding this comment

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

Remove type form field names

this.conditionType = conditionType;
}

public void apply(Check check) {
if(expiredDate.isAfter(LocalDate.now()) && conditionType.checkCondition(check)){
rewardType.applyReward(check, conditionType.getCostForCondition(check));
}
}
}
7 changes: 7 additions & 0 deletions src/checkout/Outlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package checkout;

public enum Outlet {
PEPSI,
ROSHEN,
LAYS
}
6 changes: 4 additions & 2 deletions src/checkout/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ public class Product {
final int price;
final String name;
Category category;
Outlet outlet;

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

public Product(int price, String name) {
this(price, name, null);
this(price, name, null, null);
}
}
5 changes: 5 additions & 0 deletions src/checkout/Reward.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package checkout;

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