Skip to content

Commit

Permalink
Added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rsihare93 committed Aug 27, 2019
1 parent dbe6165 commit fb8d6ba
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 123 deletions.
13 changes: 0 additions & 13 deletions amazingkart/src/main/java/com/amazingkart/App.java

This file was deleted.

59 changes: 50 additions & 9 deletions amazingkart/src/main/java/com/amazingkart/api/API.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,72 @@
package com.amazingkart.api;

import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;

import com.amazingkart.AmazingKartException;
import com.amazingkart.Services.DataLoaderService;
import com.amazingkart.dataStore.ProductStore;
import com.amazingkart.pojo.Product;
import com.amazingkart.promotions.PromotionSet;
import com.amazingkart.promotions.Promotions;
import com.amazingkart.util.JSONConverter;

public class API {

public static void main(String[] args) {
private static final String OUTPUT_JSON_FILE_NAME = "output.json";
private static final String TARGET = "target";

/*
* This is main method, As a part of main method we call following steps 1)
* Validate and parse input to the program 2) It loads Product/currency rates
* data in memory 3) It applies promotion rules on all the product 4) Finally
* saves the products list in json file.
*/
public static void main(String[] args) {
try {
Optional<Promotions> promotion = Promotions.getValue(args[0]);
if (!promotion.isPresent()) {
throw new AmazingKartException("Invalid input parameter : " + args[0]);
}
DataLoaderService.getInstance().loadExchangeRates();
DataLoaderService.getInstance().loadProductDetails();
List<Product> applyPromotion = promotion.get().getPromotionObject().applyPromotion(ProductStore.getInstance().getProducts());
new JSONConverter<Product>().writeToJsonFile("output.json", applyPromotion);
Promotions promotion = validateAndReadInput(args);
loadDataInMemory();
List<Product> updatedJSON = applyPromotion(promotion);
writeToOutputFile(updatedJSON);
} catch (AmazingKartException e) {
e.printStackTrace();
}

}

/* Writes the data back to json file */
private static void writeToOutputFile(List<Product> applyPromotion) throws AmazingKartException {
try {
new JSONConverter<Product>().writeToJsonFile(
Paths.get(TARGET, OUTPUT_JSON_FILE_NAME).toUri().toURL().getFile(), applyPromotion);
} catch (Exception e) {
e.printStackTrace();
}
}

/* Applies the mentioned promotion on all products */
private static List<Product> applyPromotion(Promotions promotion) throws AmazingKartException {
PromotionSet promotionObject = promotion.getPromotionObject();
List<Product> applyPromotion = promotionObject.applyPromotion(ProductStore.getInstance().getProducts());
return applyPromotion;
}

/* Loads product and currency data into memory */
private static void loadDataInMemory() throws AmazingKartException {
DataLoaderService.getInstance().loadExchangeRates();
DataLoaderService.getInstance().loadProductDetails();
}

/* Input Validations */
private static Promotions validateAndReadInput(String[] args) throws AmazingKartException {
if (args.length < 1 || args.length > 1) {
throw new AmazingKartException("Invalid input parameter : " + args[0]);
}
Optional<Promotions> promotion = Promotions.getValue(args[0]);
if (!promotion.isPresent()) {
throw new AmazingKartException("Invalid input parameter : " + args[0]);
}
return promotion.get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import com.amazingkart.pojo.Product;
import com.amazingkart.util.CurrencyConverter;

/* *
* @ProductStore is the class which stores product
* details fetched from web api
* */
public class ProductStore {

private static ProductStore productStore = new ProductStore();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import java.util.HashMap;
import java.util.Map;

/* *
* @RateStore is the class which stores currency rates
* details fetched from web api
* */
public class RateStore {

private static RateStore excahgenRateStore = new RateStore();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,5 @@ public List<Product> fetchJSONResponse(String productDetailsUrl, Type type) thro
return new JSONConverter<Product>().convertJsonToObject(HttpClient.fetchResponse(productDetailsUrl), type);
}

/*public Map fetchJSONResponseIntoMap(String url) throws AmazingKartException {
return (Map) new JSONConverter<T>().convertJsonToObject(HttpClient.fetchResponse(url));
}
*/




}
18 changes: 0 additions & 18 deletions amazingkart/src/main/java/com/amazingkart/pojo/Products.java

This file was deleted.

141 changes: 74 additions & 67 deletions amazingkart/src/main/java/com/amazingkart/promotions/Condition.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,83 +10,90 @@
import com.amazingkart.AmazingKartException;
import com.amazingkart.pojo.Product;

/*Condition class object corresponds to
* the discount condition which can be applicable
* on the product*/
class Condition {
enum COMPARISON_OPERATOR {
enum COMPARISON_OPERATOR {
EQUAL, IN, GREATER_THAN, LESS_THAN;
}

private String variableName;
private List<Object> values;
private Condition.COMPARISON_OPERATOR operator;

Condition(String productVariable, Condition.COMPARISON_OPERATOR operator, Object[] values) {
this.operator =operator;
this.variableName = productVariable;
this.values = Arrays.asList(values);
}

@SuppressWarnings({ "unchecked", "rawtypes" })
public boolean runCondition(Product product) throws AmazingKartException {
PropertyDescriptor pd;
try {
pd = new PropertyDescriptor(getVariableName(), Product.class);

Method getter = pd.getReadMethod();
switch (operator) {
case EQUAL:
if ((values.get(0)).equals(getter.invoke(product))) {
return true;
}
break;
case IN:
if (values.contains(getter.invoke(product))) {
return true;
}
break;
case GREATER_THAN:
if (((Comparable) getter.invoke(product)).compareTo(values.get(0)) > 0) {
return true;
}
break;

case LESS_THAN:
if (((Comparable) getter.invoke(product)).compareTo(values.get(0)) < 0) {
return true;
}
break;

default:
return false;
private String variableName;
private List<Object> values;
private Condition.COMPARISON_OPERATOR operator;

Condition(String productVariable, Condition.COMPARISON_OPERATOR operator, Object[] values) {
this.operator = operator;
this.variableName = productVariable;
this.values = Arrays.asList(values);
}

/*
* runCondition method checks if the discount condition is applicable on the
* product
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public boolean runCondition(Product product) throws AmazingKartException {
PropertyDescriptor pd;
try {
pd = new PropertyDescriptor(getVariableName(), Product.class);

Method getter = pd.getReadMethod();
switch (operator) {
case EQUAL:
if ((values.get(0)).equals(getter.invoke(product))) {
return true;
}
break;
case IN:
if (values.contains(getter.invoke(product))) {
return true;
}
} catch (SecurityException | IllegalArgumentException | InvocationTargetException | IllegalAccessException
| IntrospectionException e) {
throw new AmazingKartException("Exception occured while applying discount", e);
break;
case GREATER_THAN:
if (((Comparable) getter.invoke(product)).compareTo(values.get(0)) > 0) {
return true;
}
break;

case LESS_THAN:
if (((Comparable) getter.invoke(product)).compareTo(values.get(0)) < 0) {
return true;
}
break;

default:
return false;
}
return false;
}

public Condition.COMPARISON_OPERATOR getCondition() {
return operator;
} catch (SecurityException | IllegalArgumentException | InvocationTargetException | IllegalAccessException
| IntrospectionException e) {
throw new AmazingKartException("Exception occured while applying discount", e);
}
return false;
}

public void setCondition(Condition.COMPARISON_OPERATOR operator) {
this.operator = operator;
}
public Condition.COMPARISON_OPERATOR getCondition() {
return operator;
}

public List<Object> getValues() {
return values;
}
public void setCondition(Condition.COMPARISON_OPERATOR operator) {
this.operator = operator;
}

public void setValues(List<Object> values) {
this.values = values;
}
public List<Object> getValues() {
return values;
}

public String getVariableName() {
return variableName;
}
public void setValues(List<Object> values) {
this.values = values;
}

public void setVariableName(String variableName) {
this.variableName = variableName;
}
public String getVariableName() {
return variableName;
}

public void setVariableName(String variableName) {
this.variableName = variableName;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import com.amazingkart.pojo.Discount;
import com.amazingkart.pojo.Product;

/*DiscountRule object corresponds to the discount condition
* and amount/ percentage of discount
* that can be applied on product*/
public class DiscountRule {

enum DISCOUNT_TYPE {
Expand All @@ -21,7 +24,8 @@ enum CONDITION_GROUP_LOGIC_OPERATOR {
private CONDITION_GROUP_LOGIC_OPERATOR operator;
private CONDITION_GROUP_LOGIC_OPERATOR group_LOGIC_OPERATOR;

public DiscountRule(DISCOUNT_TYPE discount_type, Double discount, String discountTag, CONDITION_GROUP_LOGIC_OPERATOR group_LOGIC_OPERATOR, Condition []condition) {
public DiscountRule(DISCOUNT_TYPE discount_type, Double discount, String discountTag,
CONDITION_GROUP_LOGIC_OPERATOR group_LOGIC_OPERATOR, Condition[] condition) {
super();
this.discount_type = discount_type;
this.discount = discount;
Expand All @@ -30,21 +34,24 @@ public DiscountRule(DISCOUNT_TYPE discount_type, Double discount, String discoun
this.group_LOGIC_OPERATOR = group_LOGIC_OPERATOR;
}


private double applyDiscount(Double amount) {
if (DISCOUNT_TYPE.PERCENTAGE.equals(discount_type)) {
return amount * (discount / 100);
} else {
return discount;
}
}


/*
* This method applies the discount rule on product and returns the amount of
* discount that can be applied
*/
public Discount applyDiscountRule(Product product) throws AmazingKartException {
Discount discount = new Discount(0.0, "");
boolean ruleResult = CONDITION_GROUP_LOGIC_OPERATOR.AND.equals(this.operator) ? true : false;

if (conditions.length > 0) {
for (Condition condition: conditions) {
for (Condition condition : conditions) {
boolean runCondition = condition.runCondition(product);
ruleResult = CONDITION_GROUP_LOGIC_OPERATOR.AND.equals(this.operator) ? runCondition && ruleResult
: ruleResult || runCondition;
Expand All @@ -59,8 +66,7 @@ public Discount applyDiscountRule(Product product) throws AmazingKartException {

return discount;
}



public DISCOUNT_TYPE getDiscount_type() {
return discount_type;
}
Expand All @@ -85,7 +91,6 @@ public void setDiscountTag(String discountTag) {
this.discountTag = discountTag;
}


public CONDITION_GROUP_LOGIC_OPERATOR getOperator() {
return operator;
}
Expand All @@ -110,6 +115,4 @@ public void setGroup_LOGIC_OPERATOR(CONDITION_GROUP_LOGIC_OPERATOR group_LOGIC_O
this.group_LOGIC_OPERATOR = group_LOGIC_OPERATOR;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

public class PromotionA extends PromotionSet {

/*List of promotion rules for set A*/
DiscountRule[] discountRules = {
new DiscountRule(DISCOUNT_TYPE.PERCENTAGE, 7.0, "get 7% off", CONDITION_GROUP_LOGIC_OPERATOR.OR,
new Condition[] { new Condition("origin", COMPARISON_OPERATOR.EQUAL, new String[] { "Africa" }) }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

public class PromotionB extends PromotionSet {

/*List of promotion rules for set B*/
DiscountRule[] rules = {
new DiscountRule(DISCOUNT_TYPE.PERCENTAGE, 12.0, "get 12% off", CONDITION_GROUP_LOGIC_OPERATOR.OR,
new Condition[] { new Condition("inventory", Condition.COMPARISON_OPERATOR.GREATER_THAN,
Expand Down

0 comments on commit fb8d6ba

Please sign in to comment.