Skip to content
Open
Show file tree
Hide file tree
Changes from 7 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
}
21 changes: 21 additions & 0 deletions src/checkout/ByBrand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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;
}
}
21 changes: 21 additions & 0 deletions src/checkout/ByCategory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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;
}
}
27 changes: 27 additions & 0 deletions src/checkout/Check.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package checkout;

import java.time.LocalDate;
import java.time.chrono.ChronoLocalDate;
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 todayDate = LocalDate.of(2019, 3, 1);
Comment thread
andriikonoh marked this conversation as resolved.
Outdated

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

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

void addProduct(Product product) {
products.add(product);
}
Expand All @@ -33,4 +41,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 LocalDate getTodayDate() {
return todayDate;
}
}
33 changes: 20 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 List<Offer> offers = new ArrayList<>();

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

public Check closeCheck() {
useAllOffer();
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);
}
}
private void useAllOffer(){
if(this.offers.size()!=0) this.offers.forEach(offer ->{
if(isOfferavAilable(offer)) offer.apply(check);
Comment thread
andriikonoh marked this conversation as resolved.
Outdated
});
}

public void useOffer(Offer offer){
if(check != null)
this.offers.add(offer);

}
public boolean isOfferavAilable(Offer ofer){
if (ofer.getExpirationDate().isAfter(check.getTodayDate()))
Comment thread
andriikonoh marked this conversation as resolved.
Outdated
return true;
else return false;
}
}
6 changes: 6 additions & 0 deletions src/checkout/Condition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package checkout;

public interface Condition {
boolean checkCondition(Check check);
}

24 changes: 24 additions & 0 deletions src/checkout/DiscountByBrand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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.

24 changes: 24 additions & 0 deletions src/checkout/FactorRewardByCategory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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;
}
}
17 changes: 17 additions & 0 deletions src/checkout/FlatReward.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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