some simplifications and code cleanup
This commit is contained in:
parent
a384b6b1e6
commit
8deb56f9f7
@ -1 +1,3 @@
|
||||
# supermarket
|
||||
|
||||
this is one of my solutions for ["Back to the checkout code cata"](http://codekata.com/kata/kata09-back-to-the-checkout/)
|
||||
|
13
pom.xml
13
pom.xml
@ -13,19 +13,24 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.2</version>
|
||||
<version>3.6.1</version>
|
||||
<configuration>
|
||||
<source>1.7</source>
|
||||
<target>1.7</target>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-text</artifactId>
|
||||
<version>1.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.11</version>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
@ -1,6 +1,10 @@
|
||||
package de.dj_steam;
|
||||
|
||||
import de.dj_steam.strategy.*;
|
||||
import de.dj_steam.strategy.AStrategy;
|
||||
import de.dj_steam.strategy.BStrategy;
|
||||
import de.dj_steam.strategy.CStrategy;
|
||||
import de.dj_steam.strategy.DStrategy;
|
||||
import de.dj_steam.strategy.PriceCalculator;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@ -8,12 +12,12 @@ import java.util.List;
|
||||
/**
|
||||
* Created by steam on 24.02.15.
|
||||
*/
|
||||
public class CheckOut {
|
||||
class CheckOut {
|
||||
|
||||
private String bucket = "";
|
||||
private List<PricingStrategy> strategies;
|
||||
private List<PriceCalculator> strategies;
|
||||
|
||||
public CheckOut(){
|
||||
CheckOut() {
|
||||
initPriceStrategies();
|
||||
}
|
||||
|
||||
@ -28,17 +32,17 @@ public class CheckOut {
|
||||
strategies.add(new DStrategy());
|
||||
}
|
||||
|
||||
public int scan(char product){
|
||||
int scan(char product) {
|
||||
putIntoBucket(product);
|
||||
return updateTotalPrice();
|
||||
}
|
||||
|
||||
public int calculatePriceForBucket(String products){
|
||||
int calculatePriceForBucket(String products) {
|
||||
int total = 0;
|
||||
this.bucket = products;
|
||||
PriceContext context = new PriceContext();
|
||||
|
||||
for(PricingStrategy strategy : strategies){
|
||||
for (PriceCalculator strategy : strategies) {
|
||||
context.setStrategy(strategy, bucket);
|
||||
total += context.calculatePriceForProduct();
|
||||
}
|
||||
|
@ -1,20 +1,20 @@
|
||||
package de.dj_steam;
|
||||
|
||||
import de.dj_steam.strategy.PricingStrategy;
|
||||
import de.dj_steam.strategy.PriceCalculator;
|
||||
|
||||
/**
|
||||
* Created by steam on 24.02.15.
|
||||
*/
|
||||
public class PriceContext {
|
||||
private PricingStrategy strategy = null;
|
||||
class PriceContext {
|
||||
private PriceCalculator strategy = null;
|
||||
private String bucket = "";
|
||||
|
||||
public void setStrategy(final PricingStrategy strategy, String bucket){
|
||||
void setStrategy(final PriceCalculator strategy, final String bucket){
|
||||
this.strategy = strategy;
|
||||
this.bucket = bucket;
|
||||
}
|
||||
|
||||
public int calculatePriceForProduct() {
|
||||
int calculatePriceForProduct() {
|
||||
if (strategy != null){
|
||||
return strategy.calculatePrice(bucket);
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ package de.dj_steam.strategy;
|
||||
/**
|
||||
* Created by steam on 24.02.15.
|
||||
*/
|
||||
public class AStrategy extends AbstractPricingStrategy implements PricingStrategy {
|
||||
public class AStrategy extends PricingStrategy implements PriceCalculator {
|
||||
|
||||
private static final char PRODUCT_ID = 'A';
|
||||
private static final int PRICE = 50;
|
||||
@ -12,9 +12,8 @@ public class AStrategy extends AbstractPricingStrategy implements PricingStrateg
|
||||
|
||||
|
||||
@Override
|
||||
public int calculatePrice(String products) {
|
||||
int productsNumber = calculateNumberOfProductsForType(products, PRODUCT_ID);
|
||||
return calculatePriceForAllProducts(productsNumber, PRICE, PRICE_THRESHOLD, DISCOUNT);
|
||||
public int calculatePrice(final String bucket) {
|
||||
return calculatePriceForGivenProduct(bucket, PRODUCT_ID, PRICE, PRICE_THRESHOLD, DISCOUNT);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,27 +0,0 @@
|
||||
package de.dj_steam.strategy;
|
||||
|
||||
/**
|
||||
* Created by steam on 24.02.15.
|
||||
*/
|
||||
public class AbstractPricingStrategy {
|
||||
|
||||
protected int calculateNumberOfProductsForType(String products, char product) {
|
||||
int numberOfProducts = 0;
|
||||
for (int i = 0; i < products.length(); i++) {
|
||||
if (products.charAt(i) == product) {
|
||||
numberOfProducts++;
|
||||
}
|
||||
}
|
||||
return numberOfProducts;
|
||||
}
|
||||
|
||||
protected int calculatePriceForAllProducts(int numberOfProducts, int price, int priceThreshold, int discount) {
|
||||
if (priceThreshold > 0) {
|
||||
int remainder = numberOfProducts % priceThreshold;
|
||||
int triples = numberOfProducts / priceThreshold;
|
||||
return (triples * discount) + (remainder * price);
|
||||
} else {
|
||||
return numberOfProducts * price;
|
||||
}
|
||||
}
|
||||
}
|
@ -3,7 +3,7 @@ package de.dj_steam.strategy;
|
||||
/**
|
||||
* Created by steam on 24.02.15.
|
||||
*/
|
||||
public class BStrategy extends AbstractPricingStrategy implements PricingStrategy {
|
||||
public class BStrategy extends PricingStrategy implements PriceCalculator{
|
||||
|
||||
private static final char PRODUCT_ID = 'B';
|
||||
private static final int PRICE = 30;
|
||||
@ -11,8 +11,7 @@ public class BStrategy extends AbstractPricingStrategy implements PricingStrateg
|
||||
private static final int DISCOUNT = 45;
|
||||
|
||||
@Override
|
||||
public int calculatePrice(String products) {
|
||||
int productsNumber = calculateNumberOfProductsForType(products, PRODUCT_ID);
|
||||
return calculatePriceForAllProducts(productsNumber, PRICE, PRICE_THRESHOLD, DISCOUNT);
|
||||
public int calculatePrice(final String bucket) {
|
||||
return calculatePriceForGivenProduct(bucket, PRODUCT_ID, PRICE, PRICE_THRESHOLD, DISCOUNT);
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ package de.dj_steam.strategy;
|
||||
/**
|
||||
* Created by steam on 24.02.15.
|
||||
*/
|
||||
public class CStrategy extends AbstractPricingStrategy implements PricingStrategy {
|
||||
public class CStrategy extends PricingStrategy implements PriceCalculator {
|
||||
|
||||
private static final char PRODUCT_ID = 'C';
|
||||
private static final int PRICE = 20;
|
||||
@ -12,8 +12,7 @@ public class CStrategy extends AbstractPricingStrategy implements PricingStrateg
|
||||
|
||||
|
||||
@Override
|
||||
public int calculatePrice(String products) {
|
||||
int productsNumber = calculateNumberOfProductsForType(products, PRODUCT_ID);
|
||||
return calculatePriceForAllProducts(productsNumber, PRICE, PRICE_THRESHOLD, DISCOUNT);
|
||||
public int calculatePrice(final String bucket) {
|
||||
return calculatePriceForGivenProduct(bucket, PRODUCT_ID, PRICE, PRICE_THRESHOLD, DISCOUNT);
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ package de.dj_steam.strategy;
|
||||
/**
|
||||
* Created by steam on 24.02.15.
|
||||
*/
|
||||
public class DStrategy extends AbstractPricingStrategy implements PricingStrategy {
|
||||
public class DStrategy extends PricingStrategy implements PriceCalculator {
|
||||
|
||||
private static final char PRODUCT_ID = 'D';
|
||||
private static final int PRICE = 15;
|
||||
@ -11,8 +11,7 @@ public class DStrategy extends AbstractPricingStrategy implements PricingStrateg
|
||||
private static final int DISCOUNT = 0;
|
||||
|
||||
@Override
|
||||
public int calculatePrice(String products) {
|
||||
int productsNumber = calculateNumberOfProductsForType(products, PRODUCT_ID);
|
||||
return calculatePriceForAllProducts(productsNumber, PRICE, PRICE_THRESHOLD, DISCOUNT);
|
||||
public int calculatePrice(final String bucket) {
|
||||
return calculatePriceForGivenProduct(bucket, PRODUCT_ID, PRICE, PRICE_THRESHOLD, DISCOUNT);
|
||||
}
|
||||
}
|
||||
|
8
src/main/java/de/dj_steam/strategy/PriceCalculator.java
Executable file
8
src/main/java/de/dj_steam/strategy/PriceCalculator.java
Executable file
@ -0,0 +1,8 @@
|
||||
package de.dj_steam.strategy;
|
||||
|
||||
/**
|
||||
* Created by steam on 24.02.15.
|
||||
*/
|
||||
public interface PriceCalculator {
|
||||
int calculatePrice(String bucket);
|
||||
}
|
@ -1,8 +1,28 @@
|
||||
package de.dj_steam.strategy;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
/**
|
||||
* Created by steam on 24.02.15.
|
||||
*/
|
||||
public interface PricingStrategy {
|
||||
public int calculatePrice(String products);
|
||||
class PricingStrategy {
|
||||
|
||||
int calculatePriceForGivenProduct( final String bucket, final char productId, final int productPrice, final int priceThreshold, final int discount){
|
||||
int productsNumber = calculateNumberOfProductsForType(bucket, productId);
|
||||
return calculatePriceForAllProducts(productsNumber, productPrice, priceThreshold, discount);
|
||||
}
|
||||
|
||||
private int calculateNumberOfProductsForType(final String products, final char product) {
|
||||
return StringUtils.countMatches(products, product);
|
||||
}
|
||||
|
||||
private int calculatePriceForAllProducts(final int numberOfProducts, final int price, final int priceThreshold, final int discount) {
|
||||
if (priceThreshold > 0) {
|
||||
int remainder = numberOfProducts % priceThreshold;
|
||||
int triples = numberOfProducts / priceThreshold;
|
||||
return (triples * discount) + (remainder * price);
|
||||
} else {
|
||||
return numberOfProducts * price;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user