From 8fb44824d035df0baf93d0d9b5c68ffa30355007 Mon Sep 17 00:00:00 2001 From: hkh08 Date: Sun, 19 Jan 2020 12:05:30 +0000 Subject: [PATCH 1/8] initial project structure and one test added --- .idea/$CACHE_FILE$ | 6 + .idea/.gitignore | 2 + .idea/codeStyles/Project.xml | 8 ++ .idea/codeStyles/codeStyleConfig.xml | 5 + .idea/compiler.xml | 16 +++ .idea/java-test.iml | 17 +++ .idea/libraries/Maven__junit_junit_4_11.xml | 13 ++ .../Maven__org_hamcrest_hamcrest_core_1_3.xml | 13 ++ .idea/misc.xml | 14 ++ .idea/modules.xml | 8 ++ .idea/sbt.xml | 6 + .idea/uiDesigner.xml | 124 ++++++++++++++++++ .idea/vcs.xml | 6 + pom.xml | 40 ++++++ src/main/java/com/grocery/henry/Basket.java | 44 +++++++ src/main/resources/META-INF/MANIFEST.MF | 0 .../java/com/grocery/henry/BasketTest.java | 27 ++++ 17 files changed, 349 insertions(+) create mode 100644 .idea/$CACHE_FILE$ create mode 100644 .idea/.gitignore create mode 100644 .idea/codeStyles/Project.xml create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 .idea/compiler.xml create mode 100644 .idea/java-test.iml create mode 100644 .idea/libraries/Maven__junit_junit_4_11.xml create mode 100644 .idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/sbt.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/vcs.xml create mode 100644 pom.xml create mode 100644 src/main/java/com/grocery/henry/Basket.java create mode 100644 src/main/resources/META-INF/MANIFEST.MF create mode 100644 src/test/java/com/grocery/henry/BasketTest.java diff --git a/.idea/$CACHE_FILE$ b/.idea/$CACHE_FILE$ new file mode 100644 index 00000000..6cb8985e --- /dev/null +++ b/.idea/$CACHE_FILE$ @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..5c98b428 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 00000000..3fe733fe --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,8 @@ + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 00000000..a55e7a17 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 00000000..88394a27 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/java-test.iml b/.idea/java-test.iml new file mode 100644 index 00000000..166ba6eb --- /dev/null +++ b/.idea/java-test.iml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__junit_junit_4_11.xml b/.idea/libraries/Maven__junit_junit_4_11.xml new file mode 100644 index 00000000..f33320d8 --- /dev/null +++ b/.idea/libraries/Maven__junit_junit_4_11.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml new file mode 100644 index 00000000..f58bbc11 --- /dev/null +++ b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..d56c7d78 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..daec1427 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/sbt.xml b/.idea/sbt.xml new file mode 100644 index 00000000..20187435 --- /dev/null +++ b/.idea/sbt.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 00000000..e96534fb --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..35eb1ddf --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 00000000..5959bed0 --- /dev/null +++ b/pom.xml @@ -0,0 +1,40 @@ + + + 4.0.0 + + henry-grocery + java-test + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-jar-plugin + + + src/main/resources/META-INF/MANIFEST.MF + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 7 + 7 + + + + + + + junit + junit + 4.11 + test + + + + \ No newline at end of file diff --git a/src/main/java/com/grocery/henry/Basket.java b/src/main/java/com/grocery/henry/Basket.java new file mode 100644 index 00000000..dd5aea81 --- /dev/null +++ b/src/main/java/com/grocery/henry/Basket.java @@ -0,0 +1,44 @@ +package com.grocery.henry; + +import java.util.HashMap; +import java.util.Map; + +public class Basket { + private Map products = new HashMap(); + private double soup = 0.65; + private double bread = 0.80; + private double milk = 1.30; + private double apple = 0.10; + + public void add(String productName, int count) { + products.put(productName, count); + } + + + public double calculate() { + double totalPrice = 0d; + for (String productName : products.keySet()) { + switch (productName) { + case "soup": + System.out.println("soup"); + totalPrice = totalPrice + (soup * products.get(productName)); + break; + case "bread": + System.out.println("bread"); + totalPrice = totalPrice + (bread * products.get(productName)); + break; + case "milk": + System.out.println("milk"); + totalPrice = totalPrice + (milk * products.get(productName)); + break; + case "apple": + System.out.println("apple"); + totalPrice = totalPrice + (apple * products.get(productName)); + break; + default: + System.out.println("no match"); + } + } + return totalPrice; + } +} diff --git a/src/main/resources/META-INF/MANIFEST.MF b/src/main/resources/META-INF/MANIFEST.MF new file mode 100644 index 00000000..e69de29b diff --git a/src/test/java/com/grocery/henry/BasketTest.java b/src/test/java/com/grocery/henry/BasketTest.java new file mode 100644 index 00000000..149d10e3 --- /dev/null +++ b/src/test/java/com/grocery/henry/BasketTest.java @@ -0,0 +1,27 @@ +package com.grocery.henry; + +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; + +public class BasketTest { + + private Basket basket; + + @Before + public void setUp() { + basket = new Basket(); + } + + @Test + public void shouldApplyDiscountWhenThreeTinsAndTwoLoavesAdded() { + basket.add("soup", 3); + basket.add("bread", 2); + + double result = basket.calculate(); + + assertThat(result, is(3.15)); + } +} From 6393d4bde519a7a5105aa3fa1b6c1274b52ab3e5 Mon Sep 17 00:00:00 2001 From: hkh08 Date: Sun, 19 Jan 2020 16:23:24 +0000 Subject: [PATCH 2/8] first test passed with functionality added --- .idea/compiler.xml | 2 +- .idea/java-test.iml | 2 +- pom.xml | 4 +- src/main/java/com/grocery/henry/Basket.java | 36 +++++++++++------- .../java/com/grocery/henry/BasketTest.java | 6 ++- target/classes/META-INF/MANIFEST.MF | 0 .../classes/META-INF/java-test.kotlin_module | Bin 0 -> 16 bytes target/classes/com/grocery/henry/Basket.class | Bin 0 -> 2628 bytes target/java-test-1.0-SNAPSHOT.jar | Bin 0 -> 1562 bytes target/maven-archiver/pom.properties | 5 +++ .../compile/default-compile/inputFiles.lst | 0 .../default-testCompile/inputFiles.lst | 0 .../META-INF/java-test.kotlin_module | Bin 0 -> 16 bytes .../com/grocery/henry/BasketTest.class | Bin 0 -> 1145 bytes 14 files changed, 35 insertions(+), 20 deletions(-) create mode 100644 target/classes/META-INF/MANIFEST.MF create mode 100644 target/classes/META-INF/java-test.kotlin_module create mode 100644 target/classes/com/grocery/henry/Basket.class create mode 100644 target/java-test-1.0-SNAPSHOT.jar create mode 100644 target/maven-archiver/pom.properties create mode 100644 target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst create mode 100644 target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst create mode 100644 target/test-classes/META-INF/java-test.kotlin_module create mode 100644 target/test-classes/com/grocery/henry/BasketTest.class diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 88394a27..361abb8b 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -10,7 +10,7 @@ - + \ No newline at end of file diff --git a/.idea/java-test.iml b/.idea/java-test.iml index 166ba6eb..2e98b503 100644 --- a/.idea/java-test.iml +++ b/.idea/java-test.iml @@ -1,6 +1,6 @@ - + diff --git a/pom.xml b/pom.xml index 5959bed0..5c2e7578 100644 --- a/pom.xml +++ b/pom.xml @@ -22,8 +22,8 @@ org.apache.maven.plugins maven-compiler-plugin - 7 - 7 + 8 + 8 diff --git a/src/main/java/com/grocery/henry/Basket.java b/src/main/java/com/grocery/henry/Basket.java index dd5aea81..b5078933 100644 --- a/src/main/java/com/grocery/henry/Basket.java +++ b/src/main/java/com/grocery/henry/Basket.java @@ -1,39 +1,36 @@ package com.grocery.henry; +import java.math.BigDecimal; import java.util.HashMap; import java.util.Map; public class Basket { private Map products = new HashMap(); - private double soup = 0.65; - private double bread = 0.80; - private double milk = 1.30; - private double apple = 0.10; + private BigDecimal soup = BigDecimal.valueOf(0.65); + private BigDecimal bread = BigDecimal.valueOf(0.80); + private BigDecimal milk = BigDecimal.valueOf(1.30); + private BigDecimal apple = BigDecimal.valueOf(0.10); public void add(String productName, int count) { products.put(productName, count); } - public double calculate() { - double totalPrice = 0d; + public BigDecimal calculate() { + BigDecimal totalPrice = BigDecimal.valueOf(0.0); for (String productName : products.keySet()) { switch (productName) { case "soup": - System.out.println("soup"); - totalPrice = totalPrice + (soup * products.get(productName)); + totalPrice = totalPrice.add(soup.multiply(BigDecimal.valueOf(products.get(productName)))); break; case "bread": - System.out.println("bread"); - totalPrice = totalPrice + (bread * products.get(productName)); + totalPrice = totalPrice.add(getBreadDiscount()); break; case "milk": - System.out.println("milk"); - totalPrice = totalPrice + (milk * products.get(productName)); + totalPrice = totalPrice.add(milk.multiply(BigDecimal.valueOf(products.get(productName)))); break; case "apple": - System.out.println("apple"); - totalPrice = totalPrice + (apple * products.get(productName)); + totalPrice = totalPrice.add(apple.multiply(BigDecimal.valueOf(products.get(productName)))); break; default: System.out.println("no match"); @@ -41,4 +38,15 @@ public double calculate() { } return totalPrice; } + + private BigDecimal getBreadDiscount() { + BigDecimal breadPriceTotal; + if (products.containsKey("soup") && products.get("soup") >= 2) { + int numberOfDiscounts = products.get("soup") / 2; + breadPriceTotal = (bread.divide(BigDecimal.valueOf(2))).multiply(BigDecimal.valueOf(numberOfDiscounts)); + + return breadPriceTotal.add(bread.multiply(BigDecimal.valueOf((products.get("bread") - numberOfDiscounts)))); + } + return bread.multiply(BigDecimal.valueOf(products.get("bread"))); + } } diff --git a/src/test/java/com/grocery/henry/BasketTest.java b/src/test/java/com/grocery/henry/BasketTest.java index 149d10e3..80dd0173 100644 --- a/src/test/java/com/grocery/henry/BasketTest.java +++ b/src/test/java/com/grocery/henry/BasketTest.java @@ -3,6 +3,8 @@ import org.junit.Before; import org.junit.Test; +import java.math.BigDecimal; + import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; @@ -20,8 +22,8 @@ public void shouldApplyDiscountWhenThreeTinsAndTwoLoavesAdded() { basket.add("soup", 3); basket.add("bread", 2); - double result = basket.calculate(); + BigDecimal result = basket.calculate(); - assertThat(result, is(3.15)); + assertThat(result, is(BigDecimal.valueOf(3.15))); } } diff --git a/target/classes/META-INF/MANIFEST.MF b/target/classes/META-INF/MANIFEST.MF new file mode 100644 index 00000000..e69de29b diff --git a/target/classes/META-INF/java-test.kotlin_module b/target/classes/META-INF/java-test.kotlin_module new file mode 100644 index 0000000000000000000000000000000000000000..2983af70661ad375cc499ebc4da5a68ca46c532e GIT binary patch literal 16 RcmZQzU|?ooU|@t|egFVe02KfL literal 0 HcmV?d00001 diff --git a/target/classes/com/grocery/henry/Basket.class b/target/classes/com/grocery/henry/Basket.class new file mode 100644 index 0000000000000000000000000000000000000000..8f910cbded9b09f81795b4377604374dc32aad2a GIT binary patch literal 2628 zcmaJ@ZBSHI7=8}>ahKgI3kaJaA|fGzu$F=ZCWfS<3#b?>Wp&xT?q1owyYAj4=)26m z%rg7LfBkZ%{#cU;HPiIFslQD#{pgQV)Bl>Bnw*|WzXKc4q}&+^@^ zuf74W9kX6^;IM)tUXeTdXd5ziB7xdzkEt0D5JnqkQMNx(#c3RY-I%eq0`!w7R*{kJUFDK zdId^Hji{+v*|aXuwe)mnPK;}2G&pLd4KvzXv@>X0dQ?yM?xGTBCYenM1aij-&58y4 zjp%?LHWFH#=dOvgu0@DT81YGpS}GN%J@-z-G^||$j+V`1Jl&IwaMvF)Ono?;n9$Ql zwF!x<3?;)_d`wFlQqFHWt(d`xYD3{UDtKnqUv;@Q;siG6%Y+SPlUemI9rC_~Q($eCG(ngrLRil;` zo+Q<_*9xW-JjIL)lt*=|U$Qb_Wb!sDOgre2Q;S7u-oTqG&f~2`>o$k^0#R{6LZQW`Ca19$i!(q%sf@u9xkguR#1VI zK}%0-Rx&N1Olk3~K5|N+re$EobOu@m^Jm3TvA9+hbdqEiTPjNjx{He~=T&jn^-0Eh815 z5$MRPUY5_^6~8Sm(3y;-C+K8SrlmUPkdX|smrRSdfUYG-(P}%1OSef}N`m6%ve5H) zhkbQ~lcuE^X6B$yKiv^y%82OLz-zgK_pggT9PBv=+$V2i+|QS7XRq0L_RQw9XZ9aw zC$Vf_jy;s7;NS}AZoLlC`ZY>UT!-TtoUK)G%|p2c4_Br3%Ey&zugbXc+pCJtZOy$L zYvJaoP`?L$l%fV+Y=n<~s@TaBHD?D8VizUB=G>k4;Rt?|wd+&rxWrdJl8beG zck>PLO}5czkMz5J0qaZXW5XOaoSR4E95&9ODRc!V+nB$5bL+zrUUpY_hlEtz72^8> z&6S(x&~oM0FKt)pg#$9nJt*VI;y^VvA%J=QBk>b+{WI3$2I}w|>V<;!!ix=}6^)`D zO`;RcqMK-01u}D+M46p0#wmL8Idl=VkM$|ApQt9R`WICS1{EB*g?$PR@=y5vezm^Q z*VuRy%HPa!iM-o$ImWNdkmT)x463qr0h>!O3w=?#&6?{I9Qyxe{6X&ds?ch4x}`49 zZAjAOv*C3`%-2N<++V#TB7a_>EkMG6Eo_K({tR|th%K99vo2DygAF-k7w!--!!{8N UCQpu}#0BvWKM@$AP=Z7M0m_wQ(*OVf literal 0 HcmV?d00001 diff --git a/target/java-test-1.0-SNAPSHOT.jar b/target/java-test-1.0-SNAPSHOT.jar new file mode 100644 index 0000000000000000000000000000000000000000..6fc373655660d8ebec1c9315deee3a79d6fbb3de GIT binary patch literal 1562 zcmWIWW@h1H0D-HK#sOdkl;8x?zOEsTx}JV+`TuQ}lbAEG>!4=~NPm6TC&YjTl)^*}wf3hTOikS8R zm5QBnyo`&zO6F><6*1am%%cMo=nfM04hlZt)W_O+QgS8NzRuK@m0ZWFmQ}e4@M`Xg z)4Zu*t@DNF5%1Z?MSEfxV6MaT1V6Itaudr^^Wa{=R4j|BI3qQ$s8Tn*C_g#1s1lD> zbwXOR63Y^GOHzwV;D&H8FaU${cWZ%0J|hD|Ix_=<9`S}0`EQOY60-5{J0tHTesBJsq7Di+4mW z|Df7)M&$f^J8jANqQ583^;`boOAf1lph?sGmqpoXArc1bOrEuTd)n1I^{pQ3!CUiw zzOVW(_;`&GJJV@yU)dvNlB-;$biAPzVeq!=*U9m^u#=*Xp zrD@eZTNwF%bTlsWQoi9VyVlgsY+v)+tZg^E7eNVxrD_oA(y# zC93vLRZ+7#ws47;$)o_*%Rlqj#8$H`+k91NW`23%tQi-6t@^$C;o-X+{|c>|uA7y6 zNV7NX-C}dOWWVnpURXMVplobK7DQQtxP?TSgT2zvmTFiTLBUiHn55tAO z`;;EkJXO*Ym9e_XwXO9tdysFmwM*y62mki<^@nfKS(9ROP}|wR^QdT{_zMq)^9>Qn zq2Y|T>Kcru98|rQ8@J?O@Q06emB(Hu7s$tc`)XzyFLw6dg%h)6SDww6dfOqd`svv0 zo%R9Vj7%cTxHCM^8$cky@YWGTBV~V>HtYoeNHGX3X++5gNW}p@i$R$b0t6WT|33<3 z!YzhnS(x?MGc7{%Crr)gSs1rbAh$yR$Q`=CT#4omRHKlyG|W)!nHpy7lExz94TWZL oM8IKBbqKR{fkg#i|0ANPvr2qf` literal 0 HcmV?d00001 diff --git a/target/maven-archiver/pom.properties b/target/maven-archiver/pom.properties new file mode 100644 index 00000000..037d3904 --- /dev/null +++ b/target/maven-archiver/pom.properties @@ -0,0 +1,5 @@ +#Generated by Maven +#Sun Jan 19 11:14:40 GMT 2020 +version=1.0-SNAPSHOT +groupId=henry-grocery +artifactId=java-test diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst new file mode 100644 index 00000000..e69de29b diff --git a/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst b/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst new file mode 100644 index 00000000..e69de29b diff --git a/target/test-classes/META-INF/java-test.kotlin_module b/target/test-classes/META-INF/java-test.kotlin_module new file mode 100644 index 0000000000000000000000000000000000000000..2983af70661ad375cc499ebc4da5a68ca46c532e GIT binary patch literal 16 RcmZQzU|?ooU|@t|egFVe02KfL literal 0 HcmV?d00001 diff --git a/target/test-classes/com/grocery/henry/BasketTest.class b/target/test-classes/com/grocery/henry/BasketTest.class new file mode 100644 index 0000000000000000000000000000000000000000..2a2d8ad93d8ef7c2b14afeb3dbe96c726796ce97 GIT binary patch literal 1145 zcmZ`(ZBNrs6n?t5ZlyY$b0SO>eA|Xn5qx7v7!nhbDT&M(Ki;*M^D})?KYjsN!McVb=2hI$kivWcmvJ|bdy#WLj|b6S z%WV{vs4|1%F1Ot(#MVDzPDa(QUj+@*ChK5NpgHQ|b2 zT|v55?<&Z=_AIWT*cL8-7Y=${c397$NU80a%;_>IB0mXc0$cbBX2%kAxE~Nn*5|>8 zp@JLlLpKlu-W9$el$Psy0SkoZ5(p8DyVBlg2h13-z&18Te~X)9z#KxIJV%vO zsO@^8H2E75X)TVr6CEu^5IRb@s$gnd8+AUf6d zDqiS#iB%O%9cy@{pgImPVx}_V@*U>(jh)^;Hz|~67AdUMM#|!&-9QSr-`tL-SD+D@ zp~C_aF;%ON(_g-*FACHH=7fA_kLFa{8lN1E$z}tS+8h`r&B@rN@ui{

e*jQO(VI z-0ahYw=6JiE@=wW{{bcOmhW>(LNyi}9h;G}Z~^SasP!)9=&cm#4N*zaSjb9~O#`I0 zOrAlzLVLDx4CN~sDO{s97Y8R$AzL3^z;#TKN9~~GRGhpTgVBw~PozE_BmEtjGmPk2 zf+#15?hHpyaHbQMnHaNris=$&l-Fo{L#C9?9U&K6YBsTskZ&)PQ8>ZG3G@?OI%BLL qOPLjD#8IX?tC*obgKErP8KEZZRcgyk%#xaab=->UxJ_#cbAJJXB?vYE literal 0 HcmV?d00001 From 192ce917ecc384d8eb9731f4d259208ba7cc050b Mon Sep 17 00:00:00 2001 From: hkh08 Date: Sun, 19 Jan 2020 16:36:42 +0000 Subject: [PATCH 3/8] refacting: enum with price added --- src/main/java/com/grocery/henry/Basket.java | 43 ++++++++++--------- .../com/grocery/henry/domain/Product.java | 20 +++++++++ .../java/com/grocery/henry/BasketTest.java | 5 ++- 3 files changed, 46 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/grocery/henry/domain/Product.java diff --git a/src/main/java/com/grocery/henry/Basket.java b/src/main/java/com/grocery/henry/Basket.java index b5078933..8bc9498d 100644 --- a/src/main/java/com/grocery/henry/Basket.java +++ b/src/main/java/com/grocery/henry/Basket.java @@ -1,36 +1,39 @@ package com.grocery.henry; +import com.grocery.henry.domain.Product; + import java.math.BigDecimal; import java.util.HashMap; import java.util.Map; +import static com.grocery.henry.domain.Product.APPLE; +import static com.grocery.henry.domain.Product.BREAD; +import static com.grocery.henry.domain.Product.MILK; +import static com.grocery.henry.domain.Product.SOUP; + public class Basket { - private Map products = new HashMap(); - private BigDecimal soup = BigDecimal.valueOf(0.65); - private BigDecimal bread = BigDecimal.valueOf(0.80); - private BigDecimal milk = BigDecimal.valueOf(1.30); - private BigDecimal apple = BigDecimal.valueOf(0.10); + private Map products = new HashMap(); - public void add(String productName, int count) { + public void add(Product productName, int count) { products.put(productName, count); } public BigDecimal calculate() { BigDecimal totalPrice = BigDecimal.valueOf(0.0); - for (String productName : products.keySet()) { - switch (productName) { - case "soup": - totalPrice = totalPrice.add(soup.multiply(BigDecimal.valueOf(products.get(productName)))); + for (Product product : products.keySet()) { + switch (product) { + case SOUP: + totalPrice = totalPrice.add(SOUP.getPrice().multiply(BigDecimal.valueOf(products.get(product)))); break; - case "bread": + case BREAD: totalPrice = totalPrice.add(getBreadDiscount()); break; - case "milk": - totalPrice = totalPrice.add(milk.multiply(BigDecimal.valueOf(products.get(productName)))); + case MILK: + totalPrice = totalPrice.add(MILK.getPrice().multiply(BigDecimal.valueOf(products.get(product)))); break; - case "apple": - totalPrice = totalPrice.add(apple.multiply(BigDecimal.valueOf(products.get(productName)))); + case APPLE: + totalPrice = totalPrice.add(APPLE.getPrice().multiply(BigDecimal.valueOf(products.get(product)))); break; default: System.out.println("no match"); @@ -41,12 +44,12 @@ public BigDecimal calculate() { private BigDecimal getBreadDiscount() { BigDecimal breadPriceTotal; - if (products.containsKey("soup") && products.get("soup") >= 2) { - int numberOfDiscounts = products.get("soup") / 2; - breadPriceTotal = (bread.divide(BigDecimal.valueOf(2))).multiply(BigDecimal.valueOf(numberOfDiscounts)); + if (products.containsKey(SOUP) && products.get(SOUP) >= 2) { + int numberOfDiscounts = products.get(SOUP) / 2; + breadPriceTotal = (BREAD.getPrice().divide(BigDecimal.valueOf(2))).multiply(BigDecimal.valueOf(numberOfDiscounts)); - return breadPriceTotal.add(bread.multiply(BigDecimal.valueOf((products.get("bread") - numberOfDiscounts)))); + return breadPriceTotal.add(BREAD.getPrice().multiply(BigDecimal.valueOf((products.get(BREAD) - numberOfDiscounts)))); } - return bread.multiply(BigDecimal.valueOf(products.get("bread"))); + return BREAD.getPrice().multiply(BigDecimal.valueOf(products.get(BREAD))); } } diff --git a/src/main/java/com/grocery/henry/domain/Product.java b/src/main/java/com/grocery/henry/domain/Product.java new file mode 100644 index 00000000..1a722c83 --- /dev/null +++ b/src/main/java/com/grocery/henry/domain/Product.java @@ -0,0 +1,20 @@ +package com.grocery.henry.domain; + +import java.math.BigDecimal; + +public enum Product { + SOUP(BigDecimal.valueOf(0.65)), + BREAD(BigDecimal.valueOf(0.80)), + MILK(BigDecimal.valueOf(1.30)), + APPLE(BigDecimal.valueOf(0.10)); + + private BigDecimal price; + + Product(BigDecimal price) { + this.price = price; + } + + public BigDecimal getPrice() { + return price; + } +} diff --git a/src/test/java/com/grocery/henry/BasketTest.java b/src/test/java/com/grocery/henry/BasketTest.java index 80dd0173..e48833ad 100644 --- a/src/test/java/com/grocery/henry/BasketTest.java +++ b/src/test/java/com/grocery/henry/BasketTest.java @@ -1,5 +1,6 @@ package com.grocery.henry; +import com.grocery.henry.domain.Product; import org.junit.Before; import org.junit.Test; @@ -19,8 +20,8 @@ public void setUp() { @Test public void shouldApplyDiscountWhenThreeTinsAndTwoLoavesAdded() { - basket.add("soup", 3); - basket.add("bread", 2); + basket.add(Product.SOUP, 3); + basket.add(Product.BREAD, 2); BigDecimal result = basket.calculate(); From c8bd339e980192797da11374dde3a37928c182e5 Mon Sep 17 00:00:00 2001 From: hkh08 Date: Sun, 19 Jan 2020 17:24:01 +0000 Subject: [PATCH 4/8] apple offer added --- src/main/java/com/grocery/henry/Basket.java | 45 ++++++++++++++----- .../java/com/grocery/henry/BasketTest.java | 36 ++++++++++++++- 2 files changed, 67 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/grocery/henry/Basket.java b/src/main/java/com/grocery/henry/Basket.java index 8bc9498d..04973850 100644 --- a/src/main/java/com/grocery/henry/Basket.java +++ b/src/main/java/com/grocery/henry/Basket.java @@ -3,6 +3,8 @@ import com.grocery.henry.domain.Product; import java.math.BigDecimal; +import java.time.LocalDate; +import java.time.temporal.TemporalAdjusters; import java.util.HashMap; import java.util.Map; @@ -10,6 +12,7 @@ import static com.grocery.henry.domain.Product.BREAD; import static com.grocery.henry.domain.Product.MILK; import static com.grocery.henry.domain.Product.SOUP; +import static java.math.BigDecimal.valueOf; public class Basket { private Map products = new HashMap(); @@ -19,21 +22,21 @@ public void add(Product productName, int count) { } - public BigDecimal calculate() { - BigDecimal totalPrice = BigDecimal.valueOf(0.0); + public BigDecimal calculate(LocalDate orderDate) { + BigDecimal totalPrice = valueOf(0.0); for (Product product : products.keySet()) { switch (product) { case SOUP: - totalPrice = totalPrice.add(SOUP.getPrice().multiply(BigDecimal.valueOf(products.get(product)))); + totalPrice = totalPrice.add(SOUP.getPrice().multiply(valueOf(products.get(product)))); break; case BREAD: - totalPrice = totalPrice.add(getBreadDiscount()); + totalPrice = totalPrice.add(breadPriceIncludingDiscount(orderDate)); break; case MILK: - totalPrice = totalPrice.add(MILK.getPrice().multiply(BigDecimal.valueOf(products.get(product)))); + totalPrice = totalPrice.add(MILK.getPrice().multiply(valueOf(products.get(product)))); break; case APPLE: - totalPrice = totalPrice.add(APPLE.getPrice().multiply(BigDecimal.valueOf(products.get(product)))); + totalPrice = totalPrice.add(applePriceIncludingDiscount(orderDate)); break; default: System.out.println("no match"); @@ -42,14 +45,32 @@ public BigDecimal calculate() { return totalPrice; } - private BigDecimal getBreadDiscount() { + private BigDecimal breadPriceIncludingDiscount(LocalDate orderDate) { + LocalDate startDate = LocalDate.now().minusDays(1); + LocalDate endDate = startDate.plusDays(7); BigDecimal breadPriceTotal; - if (products.containsKey(SOUP) && products.get(SOUP) >= 2) { - int numberOfDiscounts = products.get(SOUP) / 2; - breadPriceTotal = (BREAD.getPrice().divide(BigDecimal.valueOf(2))).multiply(BigDecimal.valueOf(numberOfDiscounts)); + if (orderDate.isAfter(startDate) && orderDate.isBefore(endDate)) { + if (products.containsKey(SOUP) && products.get(SOUP) >= 2) { + int numberOfDiscounts = products.get(SOUP) / 2; + breadPriceTotal = (BREAD.getPrice().divide(valueOf(2))).multiply(valueOf(numberOfDiscounts)); + return breadPriceTotal.add(BREAD.getPrice().multiply(valueOf((products.get(BREAD) - numberOfDiscounts)))); + } + } + + return BREAD.getPrice().multiply(valueOf(products.get(BREAD))); + } - return breadPriceTotal.add(BREAD.getPrice().multiply(BigDecimal.valueOf((products.get(BREAD) - numberOfDiscounts)))); + + + private BigDecimal applePriceIncludingDiscount(LocalDate orderDate) { + LocalDate startDate = LocalDate.now().plusDays(3); + LocalDate endDate = startDate.plusMonths(1).with(TemporalAdjusters.lastDayOfMonth()); + + BigDecimal totalApplePrice = APPLE.getPrice().multiply(valueOf(products.get(APPLE))); + if (orderDate.isAfter(startDate) && orderDate.isBefore(endDate)) { + return totalApplePrice.subtract(totalApplePrice.multiply(valueOf(10)).divide(valueOf(100))); } - return BREAD.getPrice().multiply(BigDecimal.valueOf(products.get(BREAD))); + return totalApplePrice; } + } diff --git a/src/test/java/com/grocery/henry/BasketTest.java b/src/test/java/com/grocery/henry/BasketTest.java index e48833ad..4153a6a9 100644 --- a/src/test/java/com/grocery/henry/BasketTest.java +++ b/src/test/java/com/grocery/henry/BasketTest.java @@ -6,6 +6,7 @@ import java.math.BigDecimal; +import static java.time.LocalDate.now; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; @@ -19,12 +20,43 @@ public void setUp() { } @Test - public void shouldApplyDiscountWhenThreeTinsAndTwoLoavesAdded() { + public void shouldApplyDiscountWhenThreeSoupsTwoBreadsBoughtToday() { basket.add(Product.SOUP, 3); basket.add(Product.BREAD, 2); - BigDecimal result = basket.calculate(); + BigDecimal result = basket.calculate(now()); assertThat(result, is(BigDecimal.valueOf(3.15))); } + + @Test + public void shouldNotApplyDiscountWhenSixApplesOneMilkBoughtToday() { + basket.add(Product.APPLE, 6); + basket.add(Product.MILK, 1); + + BigDecimal result = basket.calculate(now()); + + assertThat(result, is(BigDecimal.valueOf(1.90))); + } + + @Test + public void shouldApplyDiscountWhenSixApplesOneMilkBoughtInFiveDaysTime() { + basket.add(Product.APPLE, 6); + basket.add(Product.MILK, 1); + + BigDecimal result = basket.calculate(now().plusDays(5)); + + assertThat(result, is(BigDecimal.valueOf(1.84))); + } + + @Test + public void shouldApplyDiscountWhenThreeApplesTwoSoupsOneBreadBoughtInFiveDaysTime() { + basket.add(Product.APPLE, 3); + basket.add(Product.SOUP, 2); + basket.add(Product.BREAD, 1); + + BigDecimal result = basket.calculate(now().plusDays(5)); + + assertThat(result, is(BigDecimal.valueOf(1.97))); + } } From a0600c209a798b5aa5697487396d6be594516cf9 Mon Sep 17 00:00:00 2001 From: hkh08 Date: Sun, 19 Jan 2020 17:38:39 +0000 Subject: [PATCH 5/8] Discount utility added to verify valid dates --- .idea/workspace.xml | 284 ++++++++++++++++++ src/main/java/com/grocery/henry/Basket.java | 16 +- .../com/grocery/henry/util/DiscountUtil.java | 18 ++ .../grocery/henry/util/DiscountUtilTest.java | 31 ++ 4 files changed, 337 insertions(+), 12 deletions(-) create mode 100644 .idea/workspace.xml create mode 100644 src/main/java/com/grocery/henry/util/DiscountUtil.java create mode 100644 src/test/java/com/grocery/henry/util/DiscountUtilTest.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 00000000..5dbcb3c0 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,284 @@ + + + + + + + + + + + + + + + +