forked from concretesolutions/qa-recruiting-brazil
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Code Kata concretesolutions#9 solution - Java Project upload
Java implementation of the solution for the problem concretesolutions#9 Back to Checkout from Code Kata
- Loading branch information
1 parent
0e06324
commit fed86ae
Showing
9 changed files
with
565 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>br.com.concrete</groupId> | ||
<artifactId>concrete</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>concreteChallenge</name> | ||
|
||
<dependencies> | ||
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api --> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
<version>5.7.0</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --> | ||
<dependency> | ||
<groupId>org.seleniumhq.selenium</groupId> | ||
<artifactId>selenium-java</artifactId> | ||
<version>3.141.59</version> | ||
</dependency> | ||
|
||
|
||
</dependencies> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,244 @@ | ||
package control; | ||
import java.util.ArrayList; | ||
|
||
import entity.Item; | ||
import entity.PriceRules; | ||
|
||
public class Checkout { | ||
|
||
private ArrayList<Item> itemList; | ||
private PriceRules rules; | ||
private int interatorItemA = 0; | ||
private int interatorItemB = 0; | ||
private int interatorItemC = 0; | ||
private int interatorItemD = 0; | ||
|
||
public Checkout (PriceRules priceRules) { | ||
this.itemList = new ArrayList<Item>(); | ||
rules = priceRules; | ||
} | ||
|
||
|
||
public ArrayList<Item> getItemList() { | ||
return itemList; | ||
} | ||
|
||
public void scan (String itemId) { | ||
|
||
Item item; | ||
|
||
if(rules.getPricingRulesTable().containsKey(itemId)) { | ||
|
||
int price = rules.getUnitPrice(itemId); | ||
item = new Item(itemId, price); | ||
this.itemList.add(item); | ||
itemIterator(itemId); | ||
|
||
}else { | ||
System.out.println("Unknown item. Couldn't finish operation."); | ||
} | ||
|
||
} | ||
private void itemIterator(String itemId) { | ||
|
||
if(itemId.equals("A")) { | ||
this.interatorItemA++; | ||
} | ||
else if (itemId.equals("B")) { | ||
this.interatorItemB++; | ||
} | ||
else if (itemId.equals("C")){ | ||
this.interatorItemC++; | ||
} | ||
else if(itemId.equals("D")){ | ||
this.interatorItemD++; | ||
} | ||
} | ||
|
||
public int priceList(String itemsIDSequence) { | ||
|
||
newItemList(); | ||
resetInterators(); | ||
|
||
char[]itemsList = itemsIDSequence.toCharArray(); | ||
for(char id : itemsList) { | ||
String item = Character.toString(id); | ||
scan(item); | ||
} | ||
return getTotal(); | ||
} | ||
|
||
private int getTotalForPrint () { | ||
int totalPrice = 0; | ||
|
||
for (Item i : this.itemList ) { | ||
totalPrice = totalPrice+ i.getPrice(); | ||
} | ||
if(checkForPromo(itemList)) { | ||
|
||
if(interatorItemA>= rules.getPromoQuantity("A")){ | ||
totalPrice -= giveAndPrintDiscount("A", interatorItemA); | ||
} | ||
if(interatorItemB>= rules.getPromoQuantity("B")){ | ||
totalPrice -= giveAndPrintDiscount("B", interatorItemB); | ||
} | ||
if(interatorItemC>= rules.getPromoQuantity("C")){ | ||
totalPrice -= giveAndPrintDiscount("C", interatorItemC); | ||
} | ||
if(interatorItemD>= rules.getPromoQuantity("D")){ | ||
totalPrice -= giveAndPrintDiscount("D", interatorItemD); | ||
} | ||
} | ||
return totalPrice; | ||
} | ||
|
||
public int getTotal() { | ||
int totalPrice = 0; | ||
|
||
for (Item i : this.itemList ) { | ||
totalPrice = totalPrice+ i.getPrice(); | ||
} | ||
if(checkForPromo(itemList)) { | ||
|
||
if(interatorItemA>= rules.getPromoQuantity("A")){ | ||
totalPrice -= giveDiscount("A", interatorItemA); | ||
} | ||
if(interatorItemB>= rules.getPromoQuantity("B")){ | ||
totalPrice -= giveDiscount("B", interatorItemB); | ||
} | ||
if(interatorItemC>= rules.getPromoQuantity("C")){ | ||
totalPrice -= giveDiscount("C", interatorItemC); | ||
} | ||
if(interatorItemD>= rules.getPromoQuantity("D")){ | ||
totalPrice -= giveDiscount("D", interatorItemD); | ||
} | ||
} | ||
return totalPrice; | ||
} | ||
|
||
private void newItemList() { | ||
this.itemList.clear(); | ||
} | ||
|
||
private void resetInterators() { | ||
this.interatorItemA = 0; | ||
this.interatorItemB = 0; | ||
this.interatorItemC = 0; | ||
this.interatorItemD = 0; | ||
} | ||
|
||
private boolean checkForPromo(ArrayList<Item> itemList) { | ||
boolean hasPromo = false; | ||
|
||
for(Item i: itemList) { | ||
String itemID = i.getId(); | ||
|
||
if(rules.getPromoQuantity(itemID) > 1) { | ||
hasPromo = true; | ||
break; | ||
} | ||
} | ||
return hasPromo; | ||
} | ||
|
||
private int giveAndPrintDiscount(String itemID, int itemInterator) { | ||
|
||
int discount; | ||
|
||
int minQuantity = rules.getPromoQuantity(itemID); | ||
int specialPrice = rules.getSpecialPrice(itemID); | ||
int unitPrice = rules.getUnitPrice(itemID); | ||
|
||
if(itemInterator >=minQuantity && minQuantity>1) { | ||
|
||
unitPrice = rules.getUnitPrice(itemID); | ||
int fullPrice = minQuantity*unitPrice; | ||
int discountMultiplier = itemInterator/minQuantity; | ||
discount = (fullPrice - specialPrice)*discountMultiplier; | ||
System.out.println("You recieved " + discount +" off on item "+ itemID); | ||
}else { | ||
discount = 0; | ||
} | ||
|
||
return discount; | ||
} | ||
|
||
private int giveDiscount(String itemID, int itemInterator) { | ||
|
||
int discount; | ||
|
||
int minQuantity = rules.getPromoQuantity(itemID); | ||
int specialPrice = rules.getSpecialPrice(itemID); | ||
int unitPrice = rules.getUnitPrice(itemID); | ||
|
||
if(itemInterator >=minQuantity && minQuantity>1) { | ||
|
||
unitPrice = rules.getUnitPrice(itemID); | ||
int fullPrice = minQuantity*unitPrice; | ||
int discountMultiplier = itemInterator/minQuantity; | ||
discount = (fullPrice - specialPrice)*discountMultiplier; | ||
}else { | ||
discount = 0; | ||
} | ||
|
||
return discount; | ||
} | ||
|
||
public void printItemList () { | ||
if(!itemList.isEmpty()) { | ||
System.out.println(" "); | ||
for (Item i : this.itemList){ | ||
System.out.println(i.getId()+" | "+ i.getPrice()); | ||
} | ||
}else { | ||
System.out.println("There's no item on this list."); | ||
} | ||
} | ||
|
||
public void printTotal() { | ||
System.out.println("Total: "+getTotalForPrint()); | ||
} | ||
|
||
|
||
public int getInteratorItemA() { | ||
return interatorItemA; | ||
} | ||
|
||
|
||
public void setInteratorItemA(int interatorItemA) { | ||
this.interatorItemA = interatorItemA; | ||
} | ||
|
||
|
||
public int getInteratorItemB() { | ||
return interatorItemB; | ||
} | ||
|
||
|
||
public void setInteratorItemB(int interatorItemB) { | ||
this.interatorItemB = interatorItemB; | ||
} | ||
|
||
|
||
public int getInteratorItemC() { | ||
return interatorItemC; | ||
} | ||
|
||
|
||
public void setInteratorItemC(int interatorItemC) { | ||
this.interatorItemC = interatorItemC; | ||
} | ||
|
||
|
||
public int getInteratorItemD() { | ||
return interatorItemD; | ||
} | ||
|
||
|
||
public void setInteratorItemD(int interatorItemD) { | ||
this.interatorItemD = interatorItemD; | ||
} | ||
|
||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package entity; | ||
|
||
public class Item { | ||
|
||
private String id; | ||
private int unitPrice; | ||
|
||
public Item (String id, int price) { | ||
this.id = id; | ||
this.unitPrice = price; | ||
} | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public int getPrice() { | ||
return unitPrice; | ||
} | ||
|
||
public void setPrice(int price) { | ||
this.unitPrice = price; | ||
} | ||
|
||
|
||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package entity; | ||
|
||
import java.util.HashMap; | ||
|
||
public class PriceRules { | ||
|
||
private HashMap <String, int[]> pricingRulesTable; | ||
public PriceRules() { | ||
|
||
pricingRulesTable = new HashMap<String, int[]>(); | ||
|
||
} | ||
|
||
|
||
public HashMap<String, int[]> getPricingRulesTable() { | ||
return pricingRulesTable; | ||
} | ||
|
||
|
||
public void setPricingRulesTable(HashMap<String, int[]> pricingRulesTable) { | ||
this.pricingRulesTable = pricingRulesTable; | ||
} | ||
|
||
|
||
public void newPriceRule(String itemId, int unitPrice, int quantity, int specialPrice) { | ||
|
||
int[] values = {unitPrice, quantity, specialPrice}; | ||
this.pricingRulesTable.put(itemId, values); | ||
|
||
} | ||
|
||
public void printTable() { | ||
|
||
System.out.println("Item Unit $ Quantity Special $ "); | ||
for (String id :pricingRulesTable.keySet()) { | ||
System.out.print(id +" "); | ||
|
||
int values[] = pricingRulesTable.get(id); | ||
for(int v : values) { | ||
System.out.print(v+" "); | ||
} | ||
System.out.println(" "); | ||
} | ||
} | ||
|
||
public int getUnitPrice(String key) { | ||
int[] values = pricingRulesTable.get(key); | ||
return values[0]; | ||
} | ||
|
||
public int getPromoQuantity(String key) { | ||
int[] values = pricingRulesTable.get(key); | ||
return values[1]; | ||
} | ||
|
||
public int getSpecialPrice(String key) { | ||
int[] values = pricingRulesTable.get(key); | ||
return values[2]; | ||
} | ||
} |
Oops, something went wrong.