diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..ad8a47ed440511696e71d483fd71c146c459c572 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +#Gradle +.gradle/ +build/ + +# Eclipse +.classpath +.project +.settings/ +bin/ + +# IntelliJ IDEA +.idea/ +*.iml +*.iws +*.ipr diff --git a/barcode-tasking-sample/build.gradle b/barcode-tasking-sample/build.gradle new file mode 100644 index 0000000000000000000000000000000000000000..3cae2d720fb5d1f4383313e27f1395b2b3a30385 --- /dev/null +++ b/barcode-tasking-sample/build.gradle @@ -0,0 +1,19 @@ +plugins { + id 'java' +} + +group 'org.example' +version '1.0-SNAPSHOT' + +repositories { + mavenCentral() +} + +dependencies { + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1' + testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1' +} + +test { + useJUnitPlatform() +} \ No newline at end of file diff --git a/barcode-tasking-sample/gradle/wrapper/gradle-wrapper.jar b/barcode-tasking-sample/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000000000000000000000000000000000000..41d9927a4d4fb3f96a785543079b8df6723c946b Binary files /dev/null and b/barcode-tasking-sample/gradle/wrapper/gradle-wrapper.jar differ diff --git a/barcode-tasking-sample/gradle/wrapper/gradle-wrapper.properties b/barcode-tasking-sample/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000000000000000000000000000000000000..41dfb87909a877d96c3af4adccce4c7a301b55a2 --- /dev/null +++ b/barcode-tasking-sample/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/barcode-tasking-sample/gradlew b/barcode-tasking-sample/gradlew new file mode 100755 index 0000000000000000000000000000000000000000..1b6c787337ffb79f0e3cf8b1e9f00f680a959de1 --- /dev/null +++ b/barcode-tasking-sample/gradlew @@ -0,0 +1,234 @@ +#!/bin/sh + +# +# Copyright © 2015-2021 the original 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 POSIX generated by Gradle. +# +# Important for running: +# +# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is +# noncompliant, but you have some other compliant shell such as ksh or +# bash, then to run this script, type that shell name before the whole +# command line, like: +# +# ksh Gradle +# +# Busybox and similar reduced shells will NOT work, because this script +# requires all of these POSIX shell features: +# * functions; +# * expansions «$var», «${var}», «${var:-default}», «${var+SET}», +# «${var#prefix}», «${var%suffix}», and «$( cmd )»; +# * compound commands having a testable exit status, especially «case»; +# * various built-in commands including «command», «set», and «ulimit». +# +# Important for patching: +# +# (2) This script targets any POSIX shell, so it avoids extensions provided +# by Bash, Ksh, etc; in particular arrays are avoided. +# +# The "traditional" practice of packing multiple parameters into a +# space-separated string is a well documented source of bugs and security +# problems, so this is (mostly) avoided, by progressively accumulating +# options in "$@", and eventually passing that to Java. +# +# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, +# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; +# see the in-line comments for details. +# +# There are tweaks for specific operating systems such as AIX, CygWin, +# Darwin, MinGW, and NonStop. +# +# (3) This script is generated from the Groovy template +# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# within the Gradle project. +# +# You can find Gradle at https://github.com/gradle/gradle/. +# +############################################################################## + +# Attempt to set APP_HOME + +# Resolve links: $0 may be a link +app_path=$0 + +# Need this for daisy-chained symlinks. +while + APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path + [ -h "$app_path" ] +do + ls=$( ls -ld "$app_path" ) + link=${ls#*' -> '} + case $link in #( + /*) app_path=$link ;; #( + *) app_path=$APP_HOME$link ;; + esac +done + +APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit + +APP_NAME="Gradle" +APP_BASE_NAME=${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 "$*" +} >&2 + +die () { + echo + echo "$*" + echo + exit 1 +} >&2 + +# 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 ;; #( + MSYS* | 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" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + ulimit -n "$MAX_FD" || + warn "Could not set maximum file descriptor limit to $MAX_FD" + esac +fi + +# Collect all arguments for the java command, stacking in reverse order: +# * args from the command line +# * the main class name +# * -classpath +# * -D...appname settings +# * --module-path (only if needed) +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. + +# For Cygwin or MSYS, switch paths to Windows format before running java +if "$cygwin" || "$msys" ; then + APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) + CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) + + JAVACMD=$( cygpath --unix "$JAVACMD" ) + + # Now convert the arguments - kludge to limit ourselves to /bin/sh + for arg do + if + case $arg in #( + -*) false ;; # don't mess with options #( + /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath + [ -e "$t" ] ;; #( + *) false ;; + esac + then + arg=$( cygpath --path --ignore --mixed "$arg" ) + fi + # Roll the args list around exactly as many times as the number of + # args, so each arg winds up back in the position where it started, but + # possibly modified. + # + # NB: a `for` loop captures its iteration list before it begins, so + # changing the positional parameters here affects neither the number of + # iterations, nor the values presented in `arg`. + shift # remove old arg + set -- "$@" "$arg" # push replacement arg + done +fi + +# Collect all arguments for the java command; +# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of +# shell script including quotes and variable substitutions, so put them in +# double quotes to make sure that they get re-expanded; and +# * put everything else in single quotes, so that it's not re-expanded. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Use "xargs" to parse quoted args. +# +# With -n1 it outputs one arg per line, with the quotes and backslashes removed. +# +# In Bash we could simply go: +# +# readarray ARGS < <( xargs -n1 <<<"$var" ) && +# set -- "${ARGS[@]}" "$@" +# +# but POSIX shell has neither arrays nor command substitution, so instead we +# post-process each arg (as a line of input to sed) to backslash-escape any +# character that might be a shell metacharacter, then use eval to reverse +# that process (while maintaining the separation between arguments), and wrap +# the whole thing up as a single "set" statement. +# +# This will of course break if any of these variables contains a newline or +# an unmatched quote. +# + +eval "set -- $( + printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | + xargs -n1 | + sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | + tr '\n' ' ' + )" '"$@"' + +exec "$JAVACMD" "$@" diff --git a/barcode-tasking-sample/gradlew.bat b/barcode-tasking-sample/gradlew.bat new file mode 100644 index 0000000000000000000000000000000000000000..ac1b06f93825db68fb0c0b5150917f340eaa5d02 --- /dev/null +++ b/barcode-tasking-sample/gradlew.bat @@ -0,0 +1,89 @@ +@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 Resolve any "." and ".." in APP_HOME to make it shorter. +for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi + +@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 execute + +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 execute + +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 + +: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 %* + +: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/barcode-tasking-sample/settings.gradle b/barcode-tasking-sample/settings.gradle new file mode 100644 index 0000000000000000000000000000000000000000..c164dd4e57250e44f6ef6ab2d069a916ce678d10 --- /dev/null +++ b/barcode-tasking-sample/settings.gradle @@ -0,0 +1,2 @@ +rootProject.name = 'barcode-tasking-sample' + diff --git a/barcode-tasking-sample/src/main/java/org/example/Barcode.java b/barcode-tasking-sample/src/main/java/org/example/Barcode.java new file mode 100644 index 0000000000000000000000000000000000000000..911ee70791903bbd65a4e2d88e9c6aeba55791a5 --- /dev/null +++ b/barcode-tasking-sample/src/main/java/org/example/Barcode.java @@ -0,0 +1,26 @@ +package org.example; + +import java.util.Objects; + +public class Barcode { + public final String barcode; + public final int amount; + public Barcode(String barcode, int amount) { + this.barcode = barcode; + this.amount = amount; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null || getClass() != obj.getClass()) return false; + + Barcode b = (Barcode) obj; + return Objects.equals(barcode, b.barcode) && amount == b.amount; + } + + @Override + public int hashCode() { + return Objects.hash(barcode, amount); + } +} diff --git a/barcode-tasking-sample/src/main/java/org/example/BarcodesParser.java b/barcode-tasking-sample/src/main/java/org/example/BarcodesParser.java new file mode 100644 index 0000000000000000000000000000000000000000..b92ac18edcfee7d8c362fd8d2facacd151ab7d7d --- /dev/null +++ b/barcode-tasking-sample/src/main/java/org/example/BarcodesParser.java @@ -0,0 +1,34 @@ +package org.example; + +import java.util.ArrayList; +import java.util.List; + +public class BarcodesParser { + public static List GroupBarcodes(List barcodesAndAmount) { + List groupedBarcodes = new ArrayList<>(); + for (Barcode barcode : barcodesAndAmount) { + if (groupedBarcodes.contains(barcode)) { + continue; + } + groupedBarcodes.add(barcode); + } + + return groupedBarcodes; + } + + public static List ParseBarcodes(List selectedBarcodes) { + List barcodesAndAmount = new ArrayList<>(); + for (String barcodeAndAmount : selectedBarcodes) { + String[] res = barcodeAndAmount.split("-", 2); + if (res.length == 0) { + continue; + } + + String barcode = res[0]; + int amount = res.length == 2 ? Integer.parseInt(res[1]) : 1; + barcodesAndAmount.add(new Barcode(barcode, amount)); + } + + return barcodesAndAmount; + } +} diff --git a/barcode-tasking-sample/src/main/java/org/example/BarcodesValidation.java b/barcode-tasking-sample/src/main/java/org/example/BarcodesValidation.java new file mode 100644 index 0000000000000000000000000000000000000000..bf1aaa828f6798b4a1a4e296d71fb2eff25a36c2 --- /dev/null +++ b/barcode-tasking-sample/src/main/java/org/example/BarcodesValidation.java @@ -0,0 +1,89 @@ +package org.example; + +import java.util.List; +import java.util.Objects; + +public class BarcodesValidation { + public static String CreateErrorReceipt(ValidateErrorResult validationResult) { + if (validationResult.errorType == ValidateErrorType.INVALID_BARCODE) { + return "ERROR\n-------\nThe barcode cannot be recognized"; + } else { + return String.format("ERROR\n-------\nThe product cannot be found: %s", validationResult.barcode); + } + } + + public static ValidateErrorResult ValidateBarcodes(List selectedBarcodes, List products) { + if (selectedBarcodes == null) { + return new ValidateErrorResult("", ValidateErrorType.INVALID_BARCODE); + } + + ValidateErrorResult validationResult = new ValidateErrorResult("", ValidateErrorType.VALID); + for (String barcodeAndAmount : selectedBarcodes) { + if (!isValidBarcodeAndAmount(barcodeAndAmount)) { + validationResult.errorType = ValidateErrorType.INVALID_BARCODE; + validationResult.barcode = barcodeAndAmount; + break; + } + + String barcode = barcodeAndAmount.split("-", 2)[0]; + if (!ContainsInProducts(barcode, products)) { + validationResult.errorType = ValidateErrorType.PRODUCT_NOT_FOUND; + validationResult.barcode = barcode; + break; + } + } + + return validationResult; + } + + private static boolean ContainsInProducts(String barcode, List products) { + for (Product product : products) { + if (Objects.equals(product.barcode, barcode)) { + return true; + } + } + + return false; + } + + private static boolean isValidBarcodeAndAmount(String barcodeAndAmount) { + String[] res = barcodeAndAmount.split("-"); + if (res.length == 0 || res.length > 2) { + return false; + } + + if (!isOnlyContainsUpperCaseOrNumber(res[0])) { + return false; + } + + if (res.length == 2) { + return isValidProductAmount(res[1]); + } + + return true; + } + + private static boolean isOnlyContainsUpperCaseOrNumber(String barcode) { + for (int i = 0; i < barcode.length(); i++) { + if (Character.isUpperCase(barcode.charAt(i)) || Character.isDigit(barcode.charAt(i))) { + continue; + } + return false; + } + + return true; + } + + private static boolean isValidProductAmount(String amountStr) { + try { + int amount = Integer.parseInt(amountStr); + if (amount <= 0 || amount > 99) { + return false; + } + } catch (NumberFormatException e) { + return false; + } + + return true; + } +} diff --git a/barcode-tasking-sample/src/main/java/org/example/Main.java b/barcode-tasking-sample/src/main/java/org/example/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..407f157bcb0852a527a500994d9e16a54f520f3f --- /dev/null +++ b/barcode-tasking-sample/src/main/java/org/example/Main.java @@ -0,0 +1,7 @@ +package org.example; + +public class Main { + public static void main(String[] args) { + System.out.println("Hello world!"); + } +} \ No newline at end of file diff --git a/barcode-tasking-sample/src/main/java/org/example/Product.java b/barcode-tasking-sample/src/main/java/org/example/Product.java new file mode 100644 index 0000000000000000000000000000000000000000..6756c2107e419d801a229c4027cc5ff29bf7279a --- /dev/null +++ b/barcode-tasking-sample/src/main/java/org/example/Product.java @@ -0,0 +1,12 @@ +package org.example; + +public class Product { + public final String name; + public final double price; + public final String barcode; + public Product(String name, double price, String barcode) { + this.name = name; + this.price = price; + this.barcode = barcode; + } +} diff --git a/barcode-tasking-sample/src/main/java/org/example/Receipt.java b/barcode-tasking-sample/src/main/java/org/example/Receipt.java new file mode 100644 index 0000000000000000000000000000000000000000..9548643986a5ed71036ee1110a65e18baf4a05f1 --- /dev/null +++ b/barcode-tasking-sample/src/main/java/org/example/Receipt.java @@ -0,0 +1,22 @@ +package org.example; + +import java.util.ArrayList; +import java.util.List; + +public class Receipt { + private final List receiptProductList = new ArrayList<>(); + private double totalPrice; + + public void addProduct(ReceiptProduct product) { + receiptProductList.add(product); + totalPrice += product.totalPrice; + } + + public List getReceiptProductList() { + return receiptProductList; + } + + public double getTotalPrice() { + return totalPrice; + } +} \ No newline at end of file diff --git a/barcode-tasking-sample/src/main/java/org/example/ReceiptCreator.java b/barcode-tasking-sample/src/main/java/org/example/ReceiptCreator.java new file mode 100644 index 0000000000000000000000000000000000000000..6051d9ad3721aa83922ef31be5a09d3833a18e4b --- /dev/null +++ b/barcode-tasking-sample/src/main/java/org/example/ReceiptCreator.java @@ -0,0 +1,31 @@ +package org.example; + +import java.util.List; + +public class ReceiptCreator { + public static Receipt CreateReceipt(List groupedBarcodes, List products) { + Receipt receipt = new Receipt(); + for (Barcode barcode : groupedBarcodes) { + Product product = GetProductWithBarcode(products, barcode.barcode); + if (product == null) { + continue; + } + + double totalPrice = product.price * barcode.amount; + ReceiptProduct receiptProduct = new ReceiptProduct(product.name, barcode.amount, product.price, totalPrice); + receipt.addProduct(receiptProduct); + } + + return receipt; + } + + private static Product GetProductWithBarcode(List products, String barcode) { + for (Product product : products) { + if (product.barcode.equals(barcode)) { + return product; + } + } + + return null; + } +} diff --git a/barcode-tasking-sample/src/main/java/org/example/ReceiptFormatter.java b/barcode-tasking-sample/src/main/java/org/example/ReceiptFormatter.java new file mode 100644 index 0000000000000000000000000000000000000000..7a238e27802517a73c7534b6bb0d5326d49c43d4 --- /dev/null +++ b/barcode-tasking-sample/src/main/java/org/example/ReceiptFormatter.java @@ -0,0 +1,15 @@ +package org.example; + +public class ReceiptFormatter { + public static String FormatReceipt(Receipt receipt) { + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append("Receipt\n-------\n"); + for (ReceiptProduct product : receipt.getReceiptProductList()) { + stringBuilder.append(String.format("Name: %s, Amount: %d, Price: %.2f, Total: %.2f\n", + product.name, product.amount, product.price, product.totalPrice)); + } + stringBuilder.append(String.format("-------\nTotal: %.2f", receipt.getTotalPrice())); + + return stringBuilder.toString(); + } +} diff --git a/barcode-tasking-sample/src/main/java/org/example/ReceiptPrinter.java b/barcode-tasking-sample/src/main/java/org/example/ReceiptPrinter.java new file mode 100644 index 0000000000000000000000000000000000000000..4b2f33f01b048853005c4309ad85f3df32a2bcc6 --- /dev/null +++ b/barcode-tasking-sample/src/main/java/org/example/ReceiptPrinter.java @@ -0,0 +1,21 @@ +package org.example; + +import java.util.List; + +public class ReceiptPrinter { + public static String CreateValidReceipt(List selectedBarcodes, List products) { + List barcodesAndAmount = BarcodesParser.ParseBarcodes(selectedBarcodes); + List groupedBarcodes = BarcodesParser.GroupBarcodes(barcodesAndAmount); + Receipt receipt = ReceiptCreator.CreateReceipt(groupedBarcodes, products); + return ReceiptFormatter.FormatReceipt(receipt); + } + + public static String PrintReceipt(List selectedBarcodes, List products) { + ValidateErrorResult validationResult = BarcodesValidation.ValidateBarcodes(selectedBarcodes, products); + if (validationResult.errorType != ValidateErrorType.VALID) { + return BarcodesValidation.CreateErrorReceipt(validationResult); + } + + return CreateValidReceipt(selectedBarcodes, products); + } +} diff --git a/barcode-tasking-sample/src/main/java/org/example/ReceiptProduct.java b/barcode-tasking-sample/src/main/java/org/example/ReceiptProduct.java new file mode 100644 index 0000000000000000000000000000000000000000..9438f955f3b90bc99b1d969a5596e40adeea1dfe --- /dev/null +++ b/barcode-tasking-sample/src/main/java/org/example/ReceiptProduct.java @@ -0,0 +1,34 @@ +package org.example; + +import java.util.Objects; + +public class ReceiptProduct { + public final String name; + public final int amount; + public final double price; + public final double totalPrice; + + public ReceiptProduct(String name, int amount, double price, double totalPrice) { + this.name = name; + this.amount = amount; + this.price = price; + this.totalPrice = totalPrice; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null || getClass() != obj.getClass()) return false; + + ReceiptProduct product = (ReceiptProduct) obj; + return Objects.equals(name, product.name) && + amount == product.amount && + 0 == Double.compare(price, product.price) && + 0 == Double.compare(totalPrice, product.totalPrice); + } + + @Override + public int hashCode() { + return Objects.hash(name, amount, price, totalPrice); + } +} diff --git a/barcode-tasking-sample/src/main/java/org/example/ValidateErrorResult.java b/barcode-tasking-sample/src/main/java/org/example/ValidateErrorResult.java new file mode 100644 index 0000000000000000000000000000000000000000..a41d0de1817a76483d685413b962ad636cfdbf48 --- /dev/null +++ b/barcode-tasking-sample/src/main/java/org/example/ValidateErrorResult.java @@ -0,0 +1,10 @@ +package org.example; + +public class ValidateErrorResult { + public String barcode; + public ValidateErrorType errorType; + public ValidateErrorResult(String barcode, ValidateErrorType errorType) { + this.barcode = barcode; + this.errorType = errorType; + } +} diff --git a/barcode-tasking-sample/src/main/java/org/example/ValidateErrorType.java b/barcode-tasking-sample/src/main/java/org/example/ValidateErrorType.java new file mode 100644 index 0000000000000000000000000000000000000000..3920acb84ff737edab36ceee2d4979a938148ee6 --- /dev/null +++ b/barcode-tasking-sample/src/main/java/org/example/ValidateErrorType.java @@ -0,0 +1,7 @@ +package org.example; + +public enum ValidateErrorType { + VALID, + INVALID_BARCODE, + PRODUCT_NOT_FOUND +} diff --git a/barcode-tasking-sample/src/test/java/BarcodesParserTest.java b/barcode-tasking-sample/src/test/java/BarcodesParserTest.java new file mode 100644 index 0000000000000000000000000000000000000000..04bed015a2268370b67029d176c8a7854be47b81 --- /dev/null +++ b/barcode-tasking-sample/src/test/java/BarcodesParserTest.java @@ -0,0 +1,63 @@ +import org.example.Barcode; +import org.example.BarcodesParser; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +public class BarcodesParserTest { + @Test + void should_get_empty_grouped_barcodes_if_no_barcode() { + // Given + List barcodesAndAmount = new ArrayList<>(); + + // When + List groupedBarcodes = BarcodesParser.GroupBarcodes(barcodesAndAmount); + + // Then + Assertions.assertTrue(groupedBarcodes.isEmpty()); + } + + @Test + void should_get_grouped_barcodes_if_contains_same_barcodes() { + // Given + List barcodesAndAmount = new ArrayList<>(); + barcodesAndAmount.add(new Barcode("ITEM000001", 1)); + barcodesAndAmount.add(new Barcode("ITEM000001", 1)); + + List succeedBarcodes = List.of(new Barcode("ITEM000001", 1)); + + // When + List groupedBarcodes = BarcodesParser.GroupBarcodes(barcodesAndAmount); + + // Then + Assertions.assertEquals(succeedBarcodes, groupedBarcodes); + } + + @Test + void should_get_empty_barcodes_and_amount_if_no_selected_barcodes() { + // Given + List selectedBarcodes = new ArrayList<>(); + + // When + List barcodesAndAmount = BarcodesParser.ParseBarcodes(selectedBarcodes); + + // Then + Assertions.assertTrue(barcodesAndAmount.isEmpty()); + } + + @Test + void should_get_barcodes_and_amount_if_contains_one_barcodes() { + // Given + List selectedBarcodes = List.of("ITEM000001"); + + List succeedBarcodesAndAmount = List.of(new Barcode("ITEM000001", 1)); + + // When + List barcodesAndAmount = BarcodesParser.ParseBarcodes(selectedBarcodes); + + // Then + Assertions.assertEquals(succeedBarcodesAndAmount, barcodesAndAmount); + } +} diff --git a/barcode-tasking-sample/src/test/java/BarcodesValidationTest.java b/barcode-tasking-sample/src/test/java/BarcodesValidationTest.java new file mode 100644 index 0000000000000000000000000000000000000000..cef4cf67586ac918738c2bb4efe0d1da3690c936 --- /dev/null +++ b/barcode-tasking-sample/src/test/java/BarcodesValidationTest.java @@ -0,0 +1,76 @@ +import org.example.BarcodesValidation; +import org.example.Product; +import org.example.ValidateErrorResult; +import org.example.ValidateErrorType; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.List; + +public class BarcodesValidationTest { + @Test + void should_get_error_receipt_content_if_error_type_is_invalid_barcode() { + // Given + ValidateErrorResult validationResult = new ValidateErrorResult("ASDF1234$$$$", ValidateErrorType.INVALID_BARCODE); + + // When + String receiptContent = BarcodesValidation.CreateErrorReceipt(validationResult); + + // Then + Assertions.assertEquals(receiptContent, "ERROR\n-------\nThe barcode cannot be recognized"); + } + + @Test + void should_get_error_receipt_content_if_error_type_is_product_not_found() { + // Given + ValidateErrorResult validationResult = new ValidateErrorResult("ITEM000002", ValidateErrorType.PRODUCT_NOT_FOUND); + + // When + String receiptContent = BarcodesValidation.CreateErrorReceipt(validationResult); + + // Then + Assertions.assertEquals(receiptContent, "ERROR\n-------\nThe product cannot be found: ITEM000002"); + } + + @Test + void should_get_error_validation_result_if_barcode_has_invalid_character() { + // Given + List selectedBarcodes = List.of("ASDF1234$$$$"); + List products = List.of(new Product("Cola", 3.00, "ITEM000001")); + + // When + ValidateErrorResult validationResult = BarcodesValidation.ValidateBarcodes(selectedBarcodes, products); + + // Then + Assertions.assertEquals(validationResult.barcode, "ASDF1234$$$$"); + Assertions.assertEquals(validationResult.errorType, ValidateErrorType.INVALID_BARCODE); + } + + @Test + void should_get_error_validation_result_if_barcode_amount_over_99() { + // Given + List selectedBarcodes = List.of("ITEM000001-100"); + List products = List.of(new Product("Cola", 3.00, "ITEM000001")); + + // When + ValidateErrorResult validationResult = BarcodesValidation.ValidateBarcodes(selectedBarcodes, products); + + // Then + Assertions.assertEquals(validationResult.barcode, "ITEM000001-100"); + Assertions.assertEquals(validationResult.errorType, ValidateErrorType.INVALID_BARCODE); + } + + @Test + void should_get_error_validation_result_if_product_not_found() { + // Given + List selectedBarcodes = List.of("ITEM000002"); + List products = List.of(new Product("Cola", 3.00, "ITEM000001")); + + // When + ValidateErrorResult validationResult = BarcodesValidation.ValidateBarcodes(selectedBarcodes, products); + + // Then + Assertions.assertEquals(validationResult.barcode, "ITEM000002"); + Assertions.assertEquals(validationResult.errorType, ValidateErrorType.PRODUCT_NOT_FOUND); + } +} diff --git a/barcode-tasking-sample/src/test/java/ReceiptCreatorTest.java b/barcode-tasking-sample/src/test/java/ReceiptCreatorTest.java new file mode 100644 index 0000000000000000000000000000000000000000..f8a88aa1f7fb7936946f3307e94cd4d6bca55c5e --- /dev/null +++ b/barcode-tasking-sample/src/test/java/ReceiptCreatorTest.java @@ -0,0 +1,57 @@ +import org.example.*; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +public class ReceiptCreatorTest { + @Test + void should_get_empty_receipt_if_empty_grouped_barcodes() { + // Given + List groupedBarcodes = new ArrayList<>(); + List products = new ArrayList<>(); + products.add(new Product("Cola", 3.00, "ITEM000001")); + + // When + Receipt receipt = ReceiptCreator.CreateReceipt(groupedBarcodes, products); + + // Then + Assertions.assertTrue(receipt.getReceiptProductList().isEmpty()); + Assertions.assertEquals(receipt.getTotalPrice(), 0.00); + } + + @Test + void should_get_empty_receipt_if_empty_products() { + // Given + List groupedBarcodes = new ArrayList<>(); + groupedBarcodes.add(new Barcode("ITEM000001", 1)); + List products = new ArrayList<>(); + + // When + Receipt receipt = ReceiptCreator.CreateReceipt(groupedBarcodes, products); + + // Then + Assertions.assertTrue(receipt.getReceiptProductList().isEmpty()); + Assertions.assertEquals(receipt.getTotalPrice(), 0.00); + } + + @Test + void should_get_receipt_if_one_grouped_barcodes() { + // Given + List groupedBarcodes = new ArrayList<>(); + groupedBarcodes.add(new Barcode("ITEM000001", 1)); + List products = new ArrayList<>(); + products.add(new Product("Cola", 3.00, "ITEM000001")); + + Receipt succeedReceipt = new Receipt(); + succeedReceipt.addProduct(new ReceiptProduct("Cola", 1, 3.00, 3.00)); + + // When + Receipt receipt = ReceiptCreator.CreateReceipt(groupedBarcodes, products); + + // Then + Assertions.assertEquals(receipt.getReceiptProductList(), succeedReceipt.getReceiptProductList()); + Assertions.assertEquals(0, Double.compare(receipt.getTotalPrice(), 3.00)); + } +} diff --git a/barcode-tasking-sample/src/test/java/ReceiptFormatterTest.java b/barcode-tasking-sample/src/test/java/ReceiptFormatterTest.java new file mode 100644 index 0000000000000000000000000000000000000000..a77c729ab956f2d406179372f01b448478210420 --- /dev/null +++ b/barcode-tasking-sample/src/test/java/ReceiptFormatterTest.java @@ -0,0 +1,33 @@ +import org.example.Receipt; +import org.example.ReceiptFormatter; +import org.example.ReceiptProduct; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class ReceiptFormatterTest { + @Test + void should_get_empty_receipt_content_if_receipt_contains_no_products() { + // Given + Receipt emptyReceipt = new Receipt(); + + // When + String receiptContent = ReceiptFormatter.FormatReceipt(emptyReceipt); + + // Then + Assertions.assertEquals(receiptContent, "Receipt\n-------\n-------\nTotal: 0.00"); + } + + @Test + void should_get_receipt_content_if_receipt_contains_one_products() { + // Given + ReceiptProduct product = new ReceiptProduct("Cola", 3, 3.00, 9.00); + Receipt receipt = new Receipt(); + receipt.addProduct(product); + + // When + String receiptContent = ReceiptFormatter.FormatReceipt(receipt); + + // Then + Assertions.assertEquals(receiptContent, "Receipt\n-------\nName: Cola, Amount: 3, Price: 3.00, Total: 9.00\n-------\nTotal: 9.00"); + } +} diff --git a/barcode-tasking-sample/src/test/java/ReceiptPrinterTest.java b/barcode-tasking-sample/src/test/java/ReceiptPrinterTest.java new file mode 100644 index 0000000000000000000000000000000000000000..33479776af74d20c9de17157b992d5f117dede2a --- /dev/null +++ b/barcode-tasking-sample/src/test/java/ReceiptPrinterTest.java @@ -0,0 +1,87 @@ +import org.example.Product; +import org.example.ReceiptPrinter; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +public class ReceiptPrinterTest { + @Test + void should_get_empty_receipt_content_if_contains_no_selected_barcodes() { + // Given + List selectedBarcodes = new ArrayList<>(); + List products = List.of(new Product("Cola", 3.00, "ITEM000001")); + + // When + String receiptContent = ReceiptPrinter.CreateValidReceipt(selectedBarcodes, products); + + // Then + Assertions.assertEquals(receiptContent, "Receipt\n-------\n-------\nTotal: 0.00"); + } + + @Test + void should_get_empty_receipt_content_if_contains_no_products() { + // Given + List selectedBarcodes = List.of("ITEM000001"); + List products = new ArrayList<>(); + + // When + String receiptContent = ReceiptPrinter.CreateValidReceipt(selectedBarcodes, products); + + // Then + Assertions.assertEquals(receiptContent, "Receipt\n-------\n-------\nTotal: 0.00"); + } + + @Test + void should_get_receipt_content_if_contains_one_selected_barcodes() { + // Given + List selectedBarcodes = List.of("ITEM000001"); + List products = List.of(new Product("Cola", 3.00, "ITEM000001")); + + // When + String receiptContent = ReceiptPrinter.CreateValidReceipt(selectedBarcodes, products); + + // Then + Assertions.assertEquals(receiptContent, "Receipt\n-------\nName: Cola, Amount: 1, Price: 3.00, Total: 3.00\n-------\nTotal: 3.00"); + } + + @Test + void should_get_error_receipt_content_if_contains_invalid_barcode() { + // Given + List selectedBarcodes = List.of("ITEM000001$$$$"); + List products = List.of(new Product("Cola", 3.00, "ITEM000001")); + + // When + String receiptContent = ReceiptPrinter.PrintReceipt(selectedBarcodes, products); + + // Then + Assertions.assertEquals(receiptContent, "ERROR\n-------\nThe barcode cannot be recognized"); + } + + @Test + void should_get_error_receipt_content_if_product_not_found() { + // Given + List selectedBarcodes = List.of("ITEM000002"); + List products = List.of(new Product("Cola", 3.00, "ITEM000001")); + + // When + String receiptContent = ReceiptPrinter.PrintReceipt(selectedBarcodes, products); + + // Then + Assertions.assertEquals(receiptContent, "ERROR\n-------\nThe product cannot be found: ITEM000002"); + } + + @Test + void should_get_receipt_content_if_valid_receipt() { + // Given + List selectedBarcodes = List.of("ITEM000001"); + List products = List.of(new Product("Cola", 3.00, "ITEM000001")); + + // When + String receiptContent = ReceiptPrinter.PrintReceipt(selectedBarcodes, products); + + // Then + Assertions.assertEquals(receiptContent, "Receipt\n-------\nName: Cola, Amount: 1, Price: 3.00, Total: 3.00\n-------\nTotal: 3.00"); + } +}