From 65c7406c70396a924e8acbf3c47ead3e7eb17953 Mon Sep 17 00:00:00 2001 From: Marcela Oliveira Date: Sun, 1 Mar 2020 22:43:09 -0300 Subject: [PATCH 1/5] add project without all rules validation --- .gitattributes | 6 + .gitignore | 43 ++++ build.gradle | 40 ++++ gradle/wrapper/gradle-wrapper.properties | 6 + gradlew | 183 ++++++++++++++++++ gradlew.bat | 100 ++++++++++ settings.gradle | 10 + src/main/java/qa/recruiting/brazil/App.java | 14 ++ .../qa/recruiting/brazil/model/Checkout.java | 46 +++++ .../qa/recruiting/brazil/model/Goods.java | 13 ++ .../java/qa/recruiting/brazil/model/Rule.java | 13 ++ .../qa/recruiting/brazil/CheckoutTest.java | 74 +++++++ src/test/resources/rules.json | 34 ++++ 13 files changed, 582 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 build.gradle create mode 100644 gradle/wrapper/gradle-wrapper.properties create mode 100755 gradlew create mode 100644 gradlew.bat create mode 100644 settings.gradle create mode 100644 src/main/java/qa/recruiting/brazil/App.java create mode 100644 src/main/java/qa/recruiting/brazil/model/Checkout.java create mode 100644 src/main/java/qa/recruiting/brazil/model/Goods.java create mode 100644 src/main/java/qa/recruiting/brazil/model/Rule.java create mode 100644 src/test/java/qa/recruiting/brazil/CheckoutTest.java create mode 100644 src/test/resources/rules.json diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..00a51af --- /dev/null +++ b/.gitattributes @@ -0,0 +1,6 @@ +# +# https://help.github.com/articles/dealing-with-line-endings/ +# +# These are explicitly windows files and should use crlf +*.bat text eol=crlf + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d19f295 --- /dev/null +++ b/.gitignore @@ -0,0 +1,43 @@ +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +# generated files +bin/ +gen/ + +# Eclipse project files +.classpath +.project +.settings + +# Gradle +.gradle/ +build/ +bin/ + +# Idea +.idea/ +out/ +*.iml +*.ipr +*.iws \ No newline at end of file diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..b4d18f7 --- /dev/null +++ b/build.gradle @@ -0,0 +1,40 @@ +/* + * This file was generated by the Gradle 'init' task. + * + * This generated file contains a sample Java project to get you started. + * For more details take a look at the Java Quickstart chapter in the Gradle + * User Manual available at https://docs.gradle.org/6.1/userguide/tutorial_java_projects.html + */ + +plugins { + // Apply the java plugin to add support for Java + id 'java' + + // Apply the application plugin to add support for building a CLI application. + id 'application' + id "idea" + id "eclipse" +} + +sourceCompatibility = 1.8 +targetCompatibility = 1.8 + +repositories { + mavenCentral() +} + +dependencies { + // This dependency is used by the application. + implementation 'com.google.guava:guava:28.1-jre' + compileOnly 'org.projectlombok:lombok:1.18.12' + annotationProcessor 'org.projectlombok:lombok:1.18.12' + + // Use JUnit test framework + testImplementation 'junit:junit:4.12' + testImplementation 'com.fasterxml.jackson.core:jackson-databind:2.9.10.1' +} + +application { + // Define the main class for the application. + mainClassName = 'qa.recruiting.brazil.App' +} diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..766213f --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Sun Mar 01 16:54:55 BRT 2020 +distributionUrl=https\://services.gradle.org/distributions/gradle-6.1-all.zip +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStorePath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..2fe81a7 --- /dev/null +++ b/gradlew @@ -0,0 +1,183 @@ +#!/usr/bin/env sh + +# +# Copyright 2015 the original author or authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin or MSYS, switch paths to Windows format before running java +if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=`expr $i + 1` + done + case $i in + 0) set -- ;; + 1) set -- "$args0" ;; + 2) set -- "$args0" "$args1" ;; + 3) set -- "$args0" "$args1" "$args2" ;; + 4) set -- "$args0" "$args1" "$args2" "$args3" ;; + 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=`save "$@"` + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +exec "$JAVACMD" "$@" diff --git a/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..9618d8d --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,100 @@ +@rem +@rem Copyright 2015 the original author or authors. +@rem +@rem Licensed under the Apache License, Version 2.0 (the "License"); +@rem you may not use this file except in compliance with the License. +@rem You may obtain a copy of the License at +@rem +@rem https://www.apache.org/licenses/LICENSE-2.0 +@rem +@rem Unless required by applicable law or agreed to in writing, software +@rem distributed under the License is distributed on an "AS IS" BASIS, +@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +@rem See the License for the specific language governing permissions and +@rem limitations under the License. +@rem + +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..010c698 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,10 @@ +/* + * This file was generated by the Gradle 'init' task. + * + * The settings file is used to specify which projects to include in your build. + * + * Detailed information about configuring a multi-project build in Gradle can be found + * in the user manual at https://docs.gradle.org/6.1/userguide/multi_project_builds.html + */ + +rootProject.name = 'qa-recruiting-brazil' diff --git a/src/main/java/qa/recruiting/brazil/App.java b/src/main/java/qa/recruiting/brazil/App.java new file mode 100644 index 0000000..485eb52 --- /dev/null +++ b/src/main/java/qa/recruiting/brazil/App.java @@ -0,0 +1,14 @@ +/* + * This Java source file was generated by the Gradle 'init' task. + */ +package qa.recruiting.brazil; + +public class App { + public String getGreeting() { + return "Hello world."; + } + + public static void main(String[] args) { + System.out.println(new App().getGreeting()); + } +} diff --git a/src/main/java/qa/recruiting/brazil/model/Checkout.java b/src/main/java/qa/recruiting/brazil/model/Checkout.java new file mode 100644 index 0000000..8163cc2 --- /dev/null +++ b/src/main/java/qa/recruiting/brazil/model/Checkout.java @@ -0,0 +1,46 @@ +package qa.recruiting.brazil.model; + +import lombok.Getter; +import lombok.Setter; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +@Getter +@Setter +public class Checkout { + + private List rules; + List items = new ArrayList<>(); + + + public Checkout(List rules) { + this.rules = rules; + } + + public int getTotal() { +// Predicate hasGoods = rule -> itemsQuantity.containsKey(rule.getGoods().getSku()); +// rules.stream() +// .filter(rule -> hasGoods.and(quantityIsSame) ? total = total + rule.getPrice() : true) +// .map(rule -> hasGoods ? total = total + (rule.getGoods().getUnitPrice() * itemsQuantity.get(rule.getGoods().getSku()).intValue()) : true); + int total = 0; + Map itemsQuantity = items.stream() + .collect(Collectors.groupingBy(item -> item, Collectors.counting())); + if (itemsQuantity.size() > 0) { + for (Rule rule : rules) { + if (itemsQuantity.containsKey(rule.getGoods().getSku()) && rule.getGoodsQtd() == itemsQuantity.get(rule.getGoods().getSku()).intValue()) { + total = total + rule.getPrice(); + } else if (itemsQuantity.containsKey(rule.getGoods().getSku())) { + total = total + (rule.getGoods().getUnitPrice() * itemsQuantity.get(rule.getGoods().getSku()).intValue()) ; + } + } + } + return total; + } + + public void scan(String goods) { + items.add(goods); + } +} diff --git a/src/main/java/qa/recruiting/brazil/model/Goods.java b/src/main/java/qa/recruiting/brazil/model/Goods.java new file mode 100644 index 0000000..ab741eb --- /dev/null +++ b/src/main/java/qa/recruiting/brazil/model/Goods.java @@ -0,0 +1,13 @@ +package qa.recruiting.brazil.model; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class Goods { + + private String sku; + private int unitPrice; +} + diff --git a/src/main/java/qa/recruiting/brazil/model/Rule.java b/src/main/java/qa/recruiting/brazil/model/Rule.java new file mode 100644 index 0000000..a8affdf --- /dev/null +++ b/src/main/java/qa/recruiting/brazil/model/Rule.java @@ -0,0 +1,13 @@ +package qa.recruiting.brazil.model; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class Rule { + + private Goods goods; + private int goodsQtd; + private int price; +} diff --git a/src/test/java/qa/recruiting/brazil/CheckoutTest.java b/src/test/java/qa/recruiting/brazil/CheckoutTest.java new file mode 100644 index 0000000..46d9662 --- /dev/null +++ b/src/test/java/qa/recruiting/brazil/CheckoutTest.java @@ -0,0 +1,74 @@ +/* + * This Java source file was generated by the Gradle 'init' task. + */ +package qa.recruiting.brazil; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Test; +import qa.recruiting.brazil.model.Checkout; +import qa.recruiting.brazil.model.Rule; + +import java.io.IOException; +import java.io.InputStream; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +public class CheckoutTest { + + @Test + public void priceTest() throws IOException { + assertEquals(0, getPrice("")); + assertEquals( 50, getPrice("A")); + assertEquals( 80, getPrice("AB")); + assertEquals(115, getPrice("CDBA")); + + assertEquals(100, getPrice("AA")); + assertEquals(130, getPrice("AAA")); + assertEquals(180, getPrice("AAAA")); + assertEquals(230, getPrice("AAAAA")); + assertEquals(260, getPrice("AAAAAA")); + + assertEquals(160, getPrice("AAAB")); + assertEquals(175, getPrice("AAABB")); + assertEquals(190, getPrice("AAABBD")); + assertEquals(190, getPrice("DABABA")); + } + + @Test + public void incrementalPriceTest() throws IOException { + List rules = getRules(); + Checkout checkout = new Checkout(rules); + assertEquals( 0, checkout.getTotal()); + checkout.scan("A"); + assertEquals( 50, checkout.getTotal()); + checkout.scan("B"); + assertEquals( 80, checkout.getTotal()); + checkout.scan("A"); + assertEquals(130, checkout.getTotal()); + checkout.scan("A"); + assertEquals(160, checkout.getTotal()); + checkout.scan("B"); + assertEquals(175, checkout.getTotal()); + } + + private int getPrice(String items) throws IOException { + List rules = getRules(); + Checkout checkout = new Checkout(rules); + items.chars() + .mapToObj(character -> Character.toString(character)) + .forEach(checkout::scan); + + return checkout.getTotal(); + } + + private List getRules() throws IOException { + ObjectMapper mapper = new ObjectMapper(); + String rulesPath = "/rules.json"; + InputStream inputStream = CheckoutTest.class.getResourceAsStream(rulesPath); + List rules = mapper.readValue(inputStream, new TypeReference>(){}); + return rules; + } + +} diff --git a/src/test/resources/rules.json b/src/test/resources/rules.json new file mode 100644 index 0000000..0d8036c --- /dev/null +++ b/src/test/resources/rules.json @@ -0,0 +1,34 @@ +[ + { + "goods": { + "sku": "A", + "unitPrice": 50 + }, + "goodsQtd": 3, + "price": 130 + }, + { + "goods": { + "sku": "B", + "unitPrice": 30 + }, + "goodsQtd": 2, + "price": 45 + }, + { + "goods": { + "sku": "C", + "unitPrice": 20 + }, + "goodsQtd": 0, + "price": 0 + }, + { + "goods": { + "sku": "D", + "unitPrice": 15 + }, + "goodsQtd": 0, + "price": 0 + } +] \ No newline at end of file From 34446ebb19052cc5378f35396217820630c3510a Mon Sep 17 00:00:00 2001 From: Marcela Oliveira Date: Mon, 2 Mar 2020 17:41:46 -0300 Subject: [PATCH 2/5] improve price calculation --- .../qa/recruiting/brazil/model/Checkout.java | 39 +++++++++++++++---- .../qa/recruiting/brazil/CheckoutTest.java | 7 ++-- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/main/java/qa/recruiting/brazil/model/Checkout.java b/src/main/java/qa/recruiting/brazil/model/Checkout.java index 8163cc2..2357cfb 100644 --- a/src/main/java/qa/recruiting/brazil/model/Checkout.java +++ b/src/main/java/qa/recruiting/brazil/model/Checkout.java @@ -21,25 +21,48 @@ public Checkout(List rules) { } public int getTotal() { -// Predicate hasGoods = rule -> itemsQuantity.containsKey(rule.getGoods().getSku()); -// rules.stream() -// .filter(rule -> hasGoods.and(quantityIsSame) ? total = total + rule.getPrice() : true) -// .map(rule -> hasGoods ? total = total + (rule.getGoods().getUnitPrice() * itemsQuantity.get(rule.getGoods().getSku()).intValue()) : true); int total = 0; Map itemsQuantity = items.stream() .collect(Collectors.groupingBy(item -> item, Collectors.counting())); + if (itemsQuantity.size() > 0) { for (Rule rule : rules) { - if (itemsQuantity.containsKey(rule.getGoods().getSku()) && rule.getGoodsQtd() == itemsQuantity.get(rule.getGoods().getSku()).intValue()) { - total = total + rule.getPrice(); - } else if (itemsQuantity.containsKey(rule.getGoods().getSku())) { - total = total + (rule.getGoods().getUnitPrice() * itemsQuantity.get(rule.getGoods().getSku()).intValue()) ; + if (hasGoods(itemsQuantity, rule)) { + total = setTotalPrice(itemsQuantity, rule, total); } } } return total; } + private int setTotalPrice(Map itemsQuantity, Rule rule, int total) { + return hasSameQtd(itemsQuantity, rule) ? setTotalDiscountPrice(total, rule) : setTotalRegularPrice(itemsQuantity, rule, total); + } + + private int setTotalRegularPrice(Map itemsQuantity, Rule rule, int total) { + return total += getRuleUnitPrice(rule) * getItemQtd(itemsQuantity, rule); + } + + private int setTotalDiscountPrice(int total, Rule rule) { + return total += rule.getPrice(); + } + + private int getItemQtd(Map itemsQuantity, Rule rule) { + return itemsQuantity.get(rule.getGoods().getSku()).intValue(); + } + + private int getRuleUnitPrice(Rule rule) { + return rule.getGoods().getUnitPrice(); + } + + private boolean hasSameQtd(Map itemsQuantity, Rule rule) { + return rule.getGoodsQtd() == getItemQtd(itemsQuantity, rule); + } + + private boolean hasGoods(Map itemsQuantity, Rule rule) { + return itemsQuantity.containsKey(rule.getGoods().getSku()); + } + public void scan(String goods) { items.add(goods); } diff --git a/src/test/java/qa/recruiting/brazil/CheckoutTest.java b/src/test/java/qa/recruiting/brazil/CheckoutTest.java index 46d9662..5a92c73 100644 --- a/src/test/java/qa/recruiting/brazil/CheckoutTest.java +++ b/src/test/java/qa/recruiting/brazil/CheckoutTest.java @@ -57,7 +57,7 @@ private int getPrice(String items) throws IOException { List rules = getRules(); Checkout checkout = new Checkout(rules); items.chars() - .mapToObj(character -> Character.toString(character)) + .mapToObj(Character::toString) .forEach(checkout::scan); return checkout.getTotal(); @@ -67,8 +67,7 @@ private List getRules() throws IOException { ObjectMapper mapper = new ObjectMapper(); String rulesPath = "/rules.json"; InputStream inputStream = CheckoutTest.class.getResourceAsStream(rulesPath); - List rules = mapper.readValue(inputStream, new TypeReference>(){}); - return rules; - } + return mapper.readValue(inputStream, new TypeReference>(){}); + } } From 1514bca5af7c274c822c3e57ef2fda064ba0efb7 Mon Sep 17 00:00:00 2001 From: Marcela Oliveira Date: Mon, 2 Mar 2020 18:30:39 -0300 Subject: [PATCH 3/5] add feature test cases --- ...st-case-ios-data-and-storage-usage.feature | 23 ++++++++++++++++ docs/test-case-ios-search-chat.feature | 24 +++++++++++++++++ docs/test-case-ios-send-message.feature | 26 +++++++++++++++++++ src/main/java/qa/recruiting/brazil/App.java | 14 ---------- .../qa/recruiting/brazil/model/Checkout.java | 1 + 5 files changed, 74 insertions(+), 14 deletions(-) create mode 100644 docs/test-case-ios-data-and-storage-usage.feature create mode 100644 docs/test-case-ios-search-chat.feature create mode 100644 docs/test-case-ios-send-message.feature delete mode 100644 src/main/java/qa/recruiting/brazil/App.java diff --git a/docs/test-case-ios-data-and-storage-usage.feature b/docs/test-case-ios-data-and-storage-usage.feature new file mode 100644 index 0000000..0ef1e97 --- /dev/null +++ b/docs/test-case-ios-data-and-storage-usage.feature @@ -0,0 +1,23 @@ +Feature: DATA AND STORAGE USAGE + As a user + In order to manage how much data and store I used + I want to configure my data and storage usage + + Scenario: User setting all media auto-download to wi-fi only successfully + + Given User is on Data and Store Usage Settings + When all "media options" is setting to "Wi-Fi" + Then all "media options" is displayed as Wi-Fi + + Scenario: User reset media auto-download successfully + + Given User is on Data and Store Usage Settings + When "Reset Auto-Download Settings" is pressed + Then "Photos" is displayed as "Wi-Fi and Cellular" + And "Audio", "Videos" and "Documents" are displayed as "Wi-Fi" + + Scenario: User is able to view statistics of Network Usage successfully + + Given User is on Data and Store Usage Settings + When "Network usage" is pressed + Then statistics of "Messages", "Chat Media", "Status Media", "Whatsapp Calls" and "Total Bytes" are displayed \ No newline at end of file diff --git a/docs/test-case-ios-search-chat.feature b/docs/test-case-ios-search-chat.feature new file mode 100644 index 0000000..541618d --- /dev/null +++ b/docs/test-case-ios-search-chat.feature @@ -0,0 +1,24 @@ +Feature: SEARCH CHATS + As a user + In order to find previous conversations + I want to search chats + + Scenario: User search for a chat and is able to find it + + Given User is on chat list + When "a term search" is entered + Then a "list" of chats is displayed + + Scenario: User search for a chat and is unable to find it + + Given User is on chat list + When "a term search" is entered + Then a the message "No Results" is displayed + + Scenario: User search for a chat and cancel + + Given User is on chat list + When "a term search" is entered + And a "chat list found" is displayed + And "Cancel" is pressed + Then the "original chat list" is displayed \ No newline at end of file diff --git a/docs/test-case-ios-send-message.feature b/docs/test-case-ios-send-message.feature new file mode 100644 index 0000000..7b22b91 --- /dev/null +++ b/docs/test-case-ios-send-message.feature @@ -0,0 +1,26 @@ +Feature: SEND MESSAGE + As a user + In order to communicate with other users + I want to send a message + + Scenario: User send a message successfully + + Given User is on a chat + When a "message" is filled + And "send button" is pressed + Then a "message" is sent + And one "tick" is displayed + + Scenario: User send a message and a message is delivered successfully + + Given User is on a chat + When a "message" is filled + And "send button" is pressed + Then a "message" is sent + And two "tick" are displayed + + Scenario: User not fill message field and is unable to send message + + Given User is on a chat + When a "message" is not filled + Then "send button" is not displayed \ No newline at end of file diff --git a/src/main/java/qa/recruiting/brazil/App.java b/src/main/java/qa/recruiting/brazil/App.java deleted file mode 100644 index 485eb52..0000000 --- a/src/main/java/qa/recruiting/brazil/App.java +++ /dev/null @@ -1,14 +0,0 @@ -/* - * This Java source file was generated by the Gradle 'init' task. - */ -package qa.recruiting.brazil; - -public class App { - public String getGreeting() { - return "Hello world."; - } - - public static void main(String[] args) { - System.out.println(new App().getGreeting()); - } -} diff --git a/src/main/java/qa/recruiting/brazil/model/Checkout.java b/src/main/java/qa/recruiting/brazil/model/Checkout.java index 2357cfb..2d1b478 100644 --- a/src/main/java/qa/recruiting/brazil/model/Checkout.java +++ b/src/main/java/qa/recruiting/brazil/model/Checkout.java @@ -44,6 +44,7 @@ private int setTotalRegularPrice(Map itemsQuantity, Rule rule, int } private int setTotalDiscountPrice(int total, Rule rule) { + return total += rule.getPrice(); } From 9e22dd0374a509ca6aec85d1479c02a04e527aac Mon Sep 17 00:00:00 2001 From: Marcela Oliveira Date: Mon, 2 Mar 2020 21:24:13 -0300 Subject: [PATCH 4/5] fix conditionals --- .../qa/recruiting/brazil/model/Checkout.java | 67 +++++++++++-------- src/test/resources/rules.json | 8 +-- 2 files changed, 42 insertions(+), 33 deletions(-) diff --git a/src/main/java/qa/recruiting/brazil/model/Checkout.java b/src/main/java/qa/recruiting/brazil/model/Checkout.java index 2d1b478..de61450 100644 --- a/src/main/java/qa/recruiting/brazil/model/Checkout.java +++ b/src/main/java/qa/recruiting/brazil/model/Checkout.java @@ -6,6 +6,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; @Getter @@ -15,56 +16,64 @@ public class Checkout { private List rules; List items = new ArrayList<>(); - public Checkout(List rules) { this.rules = rules; } + public void scan(String goods) { + items.add(goods); + } + public int getTotal() { - int total = 0; + AtomicInteger total = new AtomicInteger(); Map itemsQuantity = items.stream() .collect(Collectors.groupingBy(item -> item, Collectors.counting())); if (itemsQuantity.size() > 0) { - for (Rule rule : rules) { - if (hasGoods(itemsQuantity, rule)) { - total = setTotalPrice(itemsQuantity, rule, total); - } - } + itemsQuantity.forEach((item, quantity) -> + { + Rule currentRule = rules.stream() + .filter(rule -> isTheSameRule(item, rule)) + .findFirst() + .get(); + total.set(setTotalPrice(quantity.intValue(), currentRule, total.get())); + }); } - return total; - } - - private int setTotalPrice(Map itemsQuantity, Rule rule, int total) { - return hasSameQtd(itemsQuantity, rule) ? setTotalDiscountPrice(total, rule) : setTotalRegularPrice(itemsQuantity, rule, total); + return total.get(); } - private int setTotalRegularPrice(Map itemsQuantity, Rule rule, int total) { - return total += getRuleUnitPrice(rule) * getItemQtd(itemsQuantity, rule); + private boolean isTheSameRule(String item, Rule rule) { + return rule.getGoods().getSku().equals(item); } - private int setTotalDiscountPrice(int total, Rule rule) { - - return total += rule.getPrice(); + private int setTotalPrice(int quantity, Rule rule, int total) { + if (quantity == 1) { + total = setTotalRegularPrice(quantity, rule, total); + } else { + total = setTotalDiscountPrice(quantity, rule, total); + } + return total; } - private int getItemQtd(Map itemsQuantity, Rule rule) { - return itemsQuantity.get(rule.getGoods().getSku()).intValue(); + private int setTotalRegularPrice(int quantity, Rule rule, int total) { + return total + getRuleUnitPrice(rule) * quantity; } - private int getRuleUnitPrice(Rule rule) { - return rule.getGoods().getUnitPrice(); - } + private int setTotalDiscountPrice(int quantity, Rule rule, int total) { + int multiplicandFactor = 0; + int mode = rule.getGoodsQtd() % quantity; + int rest = quantity % rule.getGoodsQtd(); - private boolean hasSameQtd(Map itemsQuantity, Rule rule) { - return rule.getGoodsQtd() == getItemQtd(itemsQuantity, rule); - } + if(mode == 0) { + multiplicandFactor = rule.getGoodsQtd() / quantity; + } else if (mode == rule.getGoodsQtd()) { + multiplicandFactor = quantity / rule.getGoodsQtd(); + } - private boolean hasGoods(Map itemsQuantity, Rule rule) { - return itemsQuantity.containsKey(rule.getGoods().getSku()); + return total + (rule.getPrice() * multiplicandFactor) + (getRuleUnitPrice(rule) * rest); } - public void scan(String goods) { - items.add(goods); + private int getRuleUnitPrice(Rule rule) { + return rule.getGoods().getUnitPrice(); } } diff --git a/src/test/resources/rules.json b/src/test/resources/rules.json index 0d8036c..069e80c 100644 --- a/src/test/resources/rules.json +++ b/src/test/resources/rules.json @@ -20,15 +20,15 @@ "sku": "C", "unitPrice": 20 }, - "goodsQtd": 0, - "price": 0 + "goodsQtd": 1, + "price": 20 }, { "goods": { "sku": "D", "unitPrice": 15 }, - "goodsQtd": 0, - "price": 0 + "goodsQtd": 1, + "price": 15 } ] \ No newline at end of file From 3d85b169366eca61d9e42e8886bb4dc092899c84 Mon Sep 17 00:00:00 2001 From: Marcela Oliveira Date: Mon, 2 Mar 2020 21:26:39 -0300 Subject: [PATCH 5/5] remove application class from build.gradle --- build.gradle | 5 ----- 1 file changed, 5 deletions(-) diff --git a/build.gradle b/build.gradle index b4d18f7..8a552c2 100644 --- a/build.gradle +++ b/build.gradle @@ -33,8 +33,3 @@ dependencies { testImplementation 'junit:junit:4.12' testImplementation 'com.fasterxml.jackson.core:jackson-databind:2.9.10.1' } - -application { - // Define the main class for the application. - mainClassName = 'qa.recruiting.brazil.App' -}