some simplifications and code cleanup
This commit is contained in:
parent
a384b6b1e6
commit
8deb56f9f7
@ -1 +1,3 @@
|
|||||||
# supermarket
|
# 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>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>3.2</version>
|
<version>3.6.1</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<source>1.7</source>
|
<source>1.8</source>
|
||||||
<target>1.7</target>
|
<target>1.8</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-text</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<version>4.11</version>
|
<version>4.12</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
package de.dj_steam;
|
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.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -8,12 +12,12 @@ import java.util.List;
|
|||||||
/**
|
/**
|
||||||
* Created by steam on 24.02.15.
|
* Created by steam on 24.02.15.
|
||||||
*/
|
*/
|
||||||
public class CheckOut {
|
class CheckOut {
|
||||||
|
|
||||||
private String bucket = "";
|
private String bucket = "";
|
||||||
private List<PricingStrategy> strategies;
|
private List<PriceCalculator> strategies;
|
||||||
|
|
||||||
public CheckOut(){
|
CheckOut() {
|
||||||
initPriceStrategies();
|
initPriceStrategies();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -28,28 +32,28 @@ public class CheckOut {
|
|||||||
strategies.add(new DStrategy());
|
strategies.add(new DStrategy());
|
||||||
}
|
}
|
||||||
|
|
||||||
public int scan(char product){
|
int scan(char product) {
|
||||||
putIntoBucket(product);
|
putIntoBucket(product);
|
||||||
return updateTotalPrice();
|
return updateTotalPrice();
|
||||||
}
|
}
|
||||||
|
|
||||||
public int calculatePriceForBucket(String products){
|
int calculatePriceForBucket(String products) {
|
||||||
int total = 0;
|
int total = 0;
|
||||||
this.bucket = products;
|
this.bucket = products;
|
||||||
PriceContext context = new PriceContext();
|
PriceContext context = new PriceContext();
|
||||||
|
|
||||||
for(PricingStrategy strategy : strategies){
|
for (PriceCalculator strategy : strategies) {
|
||||||
context.setStrategy(strategy, bucket);
|
context.setStrategy(strategy, bucket);
|
||||||
total += context.calculatePriceForProduct();
|
total += context.calculatePriceForProduct();
|
||||||
}
|
}
|
||||||
return total;
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void putIntoBucket(char product){
|
private void putIntoBucket(char product) {
|
||||||
bucket += product;
|
bucket += product;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int updateTotalPrice(){
|
private int updateTotalPrice() {
|
||||||
return calculatePriceForBucket(bucket);
|
return calculatePriceForBucket(bucket);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,20 @@
|
|||||||
package de.dj_steam;
|
package de.dj_steam;
|
||||||
|
|
||||||
import de.dj_steam.strategy.PricingStrategy;
|
import de.dj_steam.strategy.PriceCalculator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by steam on 24.02.15.
|
* Created by steam on 24.02.15.
|
||||||
*/
|
*/
|
||||||
public class PriceContext {
|
class PriceContext {
|
||||||
private PricingStrategy strategy = null;
|
private PriceCalculator strategy = null;
|
||||||
private String bucket = "";
|
private String bucket = "";
|
||||||
|
|
||||||
public void setStrategy(final PricingStrategy strategy, String bucket){
|
void setStrategy(final PriceCalculator strategy, final String bucket){
|
||||||
this.strategy = strategy;
|
this.strategy = strategy;
|
||||||
this.bucket = bucket;
|
this.bucket = bucket;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int calculatePriceForProduct() {
|
int calculatePriceForProduct() {
|
||||||
if (strategy != null){
|
if (strategy != null){
|
||||||
return strategy.calculatePrice(bucket);
|
return strategy.calculatePrice(bucket);
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ package de.dj_steam.strategy;
|
|||||||
/**
|
/**
|
||||||
* Created by steam on 24.02.15.
|
* 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 char PRODUCT_ID = 'A';
|
||||||
private static final int PRICE = 50;
|
private static final int PRICE = 50;
|
||||||
@ -12,9 +12,8 @@ public class AStrategy extends AbstractPricingStrategy implements PricingStrateg
|
|||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int calculatePrice(String products) {
|
public int calculatePrice(final String bucket) {
|
||||||
int productsNumber = calculateNumberOfProductsForType(products, PRODUCT_ID);
|
return calculatePriceForGivenProduct(bucket, PRODUCT_ID, PRICE, PRICE_THRESHOLD, DISCOUNT);
|
||||||
return calculatePriceForAllProducts(productsNumber, 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.
|
* 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 char PRODUCT_ID = 'B';
|
||||||
private static final int PRICE = 30;
|
private static final int PRICE = 30;
|
||||||
@ -11,8 +11,7 @@ public class BStrategy extends AbstractPricingStrategy implements PricingStrateg
|
|||||||
private static final int DISCOUNT = 45;
|
private static final int DISCOUNT = 45;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int calculatePrice(String products) {
|
public int calculatePrice(final String bucket) {
|
||||||
int productsNumber = calculateNumberOfProductsForType(products, PRODUCT_ID);
|
return calculatePriceForGivenProduct(bucket, PRODUCT_ID, PRICE, PRICE_THRESHOLD, DISCOUNT);
|
||||||
return calculatePriceForAllProducts(productsNumber, PRICE, PRICE_THRESHOLD, DISCOUNT);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ package de.dj_steam.strategy;
|
|||||||
/**
|
/**
|
||||||
* Created by steam on 24.02.15.
|
* 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 char PRODUCT_ID = 'C';
|
||||||
private static final int PRICE = 20;
|
private static final int PRICE = 20;
|
||||||
@ -12,8 +12,7 @@ public class CStrategy extends AbstractPricingStrategy implements PricingStrateg
|
|||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int calculatePrice(String products) {
|
public int calculatePrice(final String bucket) {
|
||||||
int productsNumber = calculateNumberOfProductsForType(products, PRODUCT_ID);
|
return calculatePriceForGivenProduct(bucket, PRODUCT_ID, PRICE, PRICE_THRESHOLD, DISCOUNT);
|
||||||
return calculatePriceForAllProducts(productsNumber, PRICE, PRICE_THRESHOLD, DISCOUNT);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ package de.dj_steam.strategy;
|
|||||||
/**
|
/**
|
||||||
* Created by steam on 24.02.15.
|
* 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 char PRODUCT_ID = 'D';
|
||||||
private static final int PRICE = 15;
|
private static final int PRICE = 15;
|
||||||
@ -11,8 +11,7 @@ public class DStrategy extends AbstractPricingStrategy implements PricingStrateg
|
|||||||
private static final int DISCOUNT = 0;
|
private static final int DISCOUNT = 0;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int calculatePrice(String products) {
|
public int calculatePrice(final String bucket) {
|
||||||
int productsNumber = calculateNumberOfProductsForType(products, PRODUCT_ID);
|
return calculatePriceForGivenProduct(bucket, PRODUCT_ID, PRICE, PRICE_THRESHOLD, DISCOUNT);
|
||||||
return calculatePriceForAllProducts(productsNumber, 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;
|
package de.dj_steam.strategy;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by steam on 24.02.15.
|
* Created by steam on 24.02.15.
|
||||||
*/
|
*/
|
||||||
public interface PricingStrategy {
|
class PricingStrategy {
|
||||||
public int calculatePrice(String products);
|
|
||||||
|
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