diff --git a/.classpath b/.classpath index 9ba41a249..b3a434205 100644 --- a/.classpath +++ b/.classpath @@ -31,6 +31,19 @@ + + + + + + + + + + + + + diff --git a/.project b/.project index 700a32c79..5b16d02d6 100644 --- a/.project +++ b/.project @@ -20,4 +20,15 @@ org.eclipse.jdt.core.javanature org.eclipse.m2e.core.maven2Nature + + + 1759399064089 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 000000000..f9fe34593 --- /dev/null +++ b/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,4 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding//src/test/java=UTF-8 +encoding/=UTF-8 diff --git a/prices.csv b/prices.csv new file mode 100644 index 000000000..e00a384f9 --- /dev/null +++ b/prices.csv @@ -0,0 +1,3 @@ +A, 50 +B, 40 +C, 12 \ No newline at end of file diff --git a/src/main/java/com/mycompany/app/App.java b/src/main/java/com/mycompany/app/App.java index bc1164c88..dace59cd2 100644 --- a/src/main/java/com/mycompany/app/App.java +++ b/src/main/java/com/mycompany/app/App.java @@ -7,6 +7,10 @@ public class App { public static void main(String[] args) { - System.out.println("Hello Remote World!"); + // CodeKata: http://codekata.com/kata/kata09-back-to-the-checkout/ + ShoppingCartService service = new ShoppingCartService(); + System.out.println(service.getTotalCostOfShoppingCart("AABB").toString()); + System.out.println(service.getTotalCostOfShoppingCart("AAAABB").toString()); + System.out.println(service.getTotalCostOfShoppingCart("AAAABCCCCB").toString()); } } diff --git a/src/main/java/com/mycompany/app/InventoryPosition.java b/src/main/java/com/mycompany/app/InventoryPosition.java new file mode 100644 index 000000000..1a4ad2513 --- /dev/null +++ b/src/main/java/com/mycompany/app/InventoryPosition.java @@ -0,0 +1,19 @@ +package com.mycompany.app; + +public class InventoryPosition { + private char identifier; + private Integer price; + + public InventoryPosition(char identifier, Integer price){ + this.identifier = identifier; + this.price = price; + } + + public char getIdentifier(){ + return identifier; + } + + public Integer getPrice(){ + return price; + } +} diff --git a/src/main/java/com/mycompany/app/InventoryService.java b/src/main/java/com/mycompany/app/InventoryService.java new file mode 100644 index 000000000..8de6b2717 --- /dev/null +++ b/src/main/java/com/mycompany/app/InventoryService.java @@ -0,0 +1,43 @@ + +package com.mycompany.app; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class InventoryService { + + public List getAllProductsInInventory(){ + List csvLines = readCsvFile("prices.csv"); + return transformCsvLinesToInventoryPosition(csvLines); + } + + private List transformCsvLinesToInventoryPosition(List csvLines){ + List currentInventory = new ArrayList<>(); + for(String current : csvLines){ + String[] splittedString = current.split(","); + currentInventory.add(new InventoryPosition(removeWhitespacesFromString(splittedString[0]).charAt(0), Integer.parseInt(removeWhitespacesFromString(splittedString[1])))); + } + return currentInventory; + } + + private String removeWhitespacesFromString(String input){ + return input.replaceAll("\\s+",""); + } + + private List readCsvFile(String csvFilePath){ + List csvLineList = new ArrayList<>(); + try (BufferedReader reader = new BufferedReader(new FileReader(csvFilePath))) { + String line = ""; + while ((line = reader.readLine()) != null) { + csvLineList.add(line); + } + + } catch (IOException e) { + e.printStackTrace(); + } + return csvLineList; + } +} diff --git a/src/main/java/com/mycompany/app/ShoppingCartService.java b/src/main/java/com/mycompany/app/ShoppingCartService.java new file mode 100644 index 000000000..3e8ef46e2 --- /dev/null +++ b/src/main/java/com/mycompany/app/ShoppingCartService.java @@ -0,0 +1,25 @@ + +package com.mycompany.app; + +import java.util.List; + +public class ShoppingCartService { + + public Integer getTotalCostOfShoppingCart(String shoppingCartContent){ + Integer result = 0; + InventoryService inventoryService = new InventoryService(); + List inventoryPosition = inventoryService.getAllProductsInInventory(); + for(InventoryPosition current: inventoryPosition){ + result += getCostForInventoryPositionInShoppingCart(current, shoppingCartContent); + } + return result; + } + + private Integer getCostForInventoryPositionInShoppingCart(InventoryPosition currentInventoryPosition, String shoppingCartContent){ + return getAmountOfItemInShoppingCart(shoppingCartContent,currentInventoryPosition.getIdentifier()) * currentInventoryPosition.getPrice(); + } + + private Integer getAmountOfItemInShoppingCart(String shoppingCartContent, char productIdentifier){ + return shoppingCartContent.split(productIdentifier+"", -1).length-1; + } +}