├── .github └── workflows │ └── gradle.yml ├── .gitignore ├── LICENSE.txt ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src ├── main └── java │ └── org │ └── soujava │ ├── player │ ├── Email.java │ ├── FluentPlayer.java │ ├── Player.java │ ├── PlayerBuilder.java │ ├── Position.java │ └── Team.java │ └── sandwich │ ├── Bread.java │ ├── Checkout.java │ ├── Drink.java │ ├── DrinkType.java │ ├── Order.java │ ├── OrderFluent.java │ ├── PricingTables.java │ ├── Sandwich.java │ ├── SandwichStyle.java │ └── Size.java └── test └── java └── org └── soujava ├── player ├── EmailTest.java ├── PlayerTest.java ├── PlayerTestDataBuilder.java └── TeamTest.java └── sandwich └── OrderTest.java /.github/workflows/gradle.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a Java project with Gradle and cache/restore any dependencies to improve the workflow execution time 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle 3 | 4 | name: Java CI with Gradle 5 | 6 | on: 7 | push: 8 | branches: [ main ] 9 | pull_request: 10 | branches: [ main ] 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | 17 | steps: 18 | - uses: actions/checkout@v2 19 | - name: Set up JDK 11 20 | uses: actions/setup-java@v2 21 | with: 22 | java-version: '11' 23 | distribution: 'adopt' 24 | cache: gradle 25 | - name: Grant execute permission for gradlew 26 | run: chmod +x gradlew 27 | - name: Build with Gradle 28 | run: ./gradlew build 29 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target/ 2 | build/ 3 | .gradle/ 4 | pom.xml.tag 5 | pom.xml.releaseBackup 6 | pom.xml.versionsBackup 7 | pom.xml.next 8 | test-output/ 9 | /doc 10 | *.iml 11 | *.idea 12 | *.log 13 | /.idea 14 | .checkstyle 15 | 16 | # Eclipse metadata 17 | .settings/ 18 | .project 19 | .factorypath 20 | .classpath 21 | -project 22 | /.resourceCache 23 | /.project 24 | 25 | # Annotation processor metadata 26 | .apt_generated/ 27 | .apt_generated_tests/ 28 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2021 Zup Java 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # player-fluent-api 2 | This is a sample of fluent API using Player in a soccer team. 3 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'java' 3 | } 4 | 5 | group 'org.br.soujava' 6 | version '1.0.0-SNAPSHOT' 7 | 8 | repositories { 9 | mavenCentral() 10 | } 11 | 12 | dependencies { 13 | implementation 'org.javamoney.moneta:moneta-core:1.4.2' 14 | testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.2' 15 | testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.2' 16 | } 17 | 18 | test { 19 | useJUnitPlatform() 20 | } -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soujava/sample-fluent-api/2fea7fa52ca0f4766b6424791965ef2e1f0ba9cc/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.1-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # 4 | # Copyright 2015 the original author or authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | ## 21 | ## Gradle start up script for UN*X 22 | ## 23 | ############################################################################## 24 | 25 | # Attempt to set APP_HOME 26 | # Resolve links: $0 may be a link 27 | PRG="$0" 28 | # Need this for relative symlinks. 29 | while [ -h "$PRG" ] ; do 30 | ls=`ls -ld "$PRG"` 31 | link=`expr "$ls" : '.*-> \(.*\)$'` 32 | if expr "$link" : '/.*' > /dev/null; then 33 | PRG="$link" 34 | else 35 | PRG=`dirname "$PRG"`"/$link" 36 | fi 37 | done 38 | SAVED="`pwd`" 39 | cd "`dirname \"$PRG\"`/" >/dev/null 40 | APP_HOME="`pwd -P`" 41 | cd "$SAVED" >/dev/null 42 | 43 | APP_NAME="Gradle" 44 | APP_BASE_NAME=`basename "$0"` 45 | 46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 48 | 49 | # Use the maximum available, or set MAX_FD != -1 to use that value. 50 | MAX_FD="maximum" 51 | 52 | warn () { 53 | echo "$*" 54 | } 55 | 56 | die () { 57 | echo 58 | echo "$*" 59 | echo 60 | exit 1 61 | } 62 | 63 | # OS specific support (must be 'true' or 'false'). 64 | cygwin=false 65 | msys=false 66 | darwin=false 67 | nonstop=false 68 | case "`uname`" in 69 | CYGWIN* ) 70 | cygwin=true 71 | ;; 72 | Darwin* ) 73 | darwin=true 74 | ;; 75 | MSYS* | MINGW* ) 76 | msys=true 77 | ;; 78 | NONSTOP* ) 79 | nonstop=true 80 | ;; 81 | esac 82 | 83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 84 | 85 | 86 | # Determine the Java command to use to start the JVM. 87 | if [ -n "$JAVA_HOME" ] ; then 88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 89 | # IBM's JDK on AIX uses strange locations for the executables 90 | JAVACMD="$JAVA_HOME/jre/sh/java" 91 | else 92 | JAVACMD="$JAVA_HOME/bin/java" 93 | fi 94 | if [ ! -x "$JAVACMD" ] ; then 95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 96 | 97 | Please set the JAVA_HOME variable in your environment to match the 98 | location of your Java installation." 99 | fi 100 | else 101 | JAVACMD="java" 102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 103 | 104 | Please set the JAVA_HOME variable in your environment to match the 105 | location of your Java installation." 106 | fi 107 | 108 | # Increase the maximum file descriptors if we can. 109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 110 | MAX_FD_LIMIT=`ulimit -H -n` 111 | if [ $? -eq 0 ] ; then 112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 113 | MAX_FD="$MAX_FD_LIMIT" 114 | fi 115 | ulimit -n $MAX_FD 116 | if [ $? -ne 0 ] ; then 117 | warn "Could not set maximum file descriptor limit: $MAX_FD" 118 | fi 119 | else 120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 121 | fi 122 | fi 123 | 124 | # For Darwin, add options to specify how the application appears in the dock 125 | if $darwin; then 126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 127 | fi 128 | 129 | # For Cygwin or MSYS, switch paths to Windows format before running java 130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then 131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 133 | 134 | JAVACMD=`cygpath --unix "$JAVACMD"` 135 | 136 | # We build the pattern for arguments to be converted via cygpath 137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 138 | SEP="" 139 | for dir in $ROOTDIRSRAW ; do 140 | ROOTDIRS="$ROOTDIRS$SEP$dir" 141 | SEP="|" 142 | done 143 | OURCYGPATTERN="(^($ROOTDIRS))" 144 | # Add a user-defined pattern to the cygpath arguments 145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 147 | fi 148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 149 | i=0 150 | for arg in "$@" ; do 151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 153 | 154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 156 | else 157 | eval `echo args$i`="\"$arg\"" 158 | fi 159 | i=`expr $i + 1` 160 | done 161 | case $i in 162 | 0) set -- ;; 163 | 1) set -- "$args0" ;; 164 | 2) set -- "$args0" "$args1" ;; 165 | 3) set -- "$args0" "$args1" "$args2" ;; 166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;; 167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 172 | esac 173 | fi 174 | 175 | # Escape application args 176 | save () { 177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 178 | echo " " 179 | } 180 | APP_ARGS=`save "$@"` 181 | 182 | # Collect all arguments for the java command, following the shell quoting and substitution rules 183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 184 | 185 | exec "$JAVACMD" "$@" 186 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'player-fluent-api' 2 | 3 | -------------------------------------------------------------------------------- /src/main/java/org/soujava/player/Email.java: -------------------------------------------------------------------------------- 1 | package org.soujava.player; 2 | 3 | import java.util.Objects; 4 | import java.util.function.Supplier; 5 | import java.util.regex.Pattern; 6 | 7 | public final class Email implements Supplier { 8 | 9 | private static final String EMAIL_PATTERN = 10 | "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" 11 | + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; 12 | 13 | private static final Pattern PATTERN = Pattern.compile(EMAIL_PATTERN); 14 | 15 | private final String value; 16 | 17 | @Override 18 | public String get() { 19 | return value; 20 | } 21 | 22 | private Email(String value) { 23 | this.value = value; 24 | } 25 | 26 | 27 | @Override 28 | public boolean equals(Object o) { 29 | if (this == o) { 30 | return true; 31 | } 32 | if (o == null || getClass() != o.getClass()) { 33 | return false; 34 | } 35 | Email email = (Email) o; 36 | return Objects.equals(value, email.value); 37 | } 38 | 39 | @Override 40 | public int hashCode() { 41 | return Objects.hashCode(value); 42 | } 43 | 44 | @Override 45 | public String toString() { 46 | return value; 47 | } 48 | 49 | public static Email of(String value) { 50 | Objects.requireNonNull(value, "o valor é obrigatório"); 51 | if (!PATTERN.matcher(value).matches()) { 52 | throw new IllegalArgumentException("org.br.soujava.Email nao válido"); 53 | } 54 | 55 | return new Email(value); 56 | } 57 | } -------------------------------------------------------------------------------- /src/main/java/org/soujava/player/FluentPlayer.java: -------------------------------------------------------------------------------- 1 | package org.soujava.player; 2 | 3 | import javax.money.MonetaryAmount; 4 | import java.time.Year; 5 | 6 | public interface FluentPlayer { 7 | 8 | PlayerEnd start(Year start); 9 | 10 | interface PlayerEnd { 11 | PlayerPosition end(Year start); 12 | } 13 | 14 | interface PlayerPosition { 15 | PlayerSalary position(Position position); 16 | } 17 | 18 | interface PlayerSalary { 19 | PlayerEmail salary(MonetaryAmount salary); 20 | } 21 | 22 | interface PlayerEmail { 23 | Player email(Email email); 24 | 25 | Player build(); 26 | 27 | } 28 | 29 | } -------------------------------------------------------------------------------- /src/main/java/org/soujava/player/Player.java: -------------------------------------------------------------------------------- 1 | package org.soujava.player; 2 | 3 | import javax.money.MonetaryAmount; 4 | import java.time.Year; 5 | import java.util.Objects; 6 | import java.util.Optional; 7 | 8 | public class Player { 9 | 10 | static final Year SOCCER_BORN = Year.of(1863); 11 | 12 | private String name; 13 | 14 | private Year start; 15 | 16 | private Year end; 17 | 18 | private Email email; 19 | 20 | private Position position; 21 | 22 | private MonetaryAmount salary; 23 | 24 | private int goal = 0; 25 | 26 | Player(String name, Year start, Year end, Email email, Position position, MonetaryAmount salary) { 27 | this.name = name; 28 | this.start = start; 29 | this.end = end; 30 | this.email = email; 31 | this.position = position; 32 | this.salary = salary; 33 | } 34 | 35 | @Deprecated 36 | Player() { 37 | } 38 | 39 | public String getName() { 40 | return name; 41 | } 42 | 43 | public Year getStart() { 44 | return start; 45 | } 46 | 47 | public Optional getEmail() { 48 | return Optional.ofNullable(email); 49 | } 50 | 51 | public Position getPosition() { 52 | return position; 53 | } 54 | 55 | public MonetaryAmount getSalary() { 56 | return salary; 57 | } 58 | 59 | public Optional getEnd() { 60 | return Optional.ofNullable(end); 61 | } 62 | 63 | public int getGoal() { 64 | return goal; 65 | } 66 | 67 | public void goal() { 68 | goal++; 69 | } 70 | 71 | public void setEnd(Year end) { 72 | if (end != null && end.isBefore(start)) { 73 | throw new IllegalArgumentException("the last year of a player must be equal or higher than the start."); 74 | } 75 | this.end = end; 76 | } 77 | 78 | public static FluentPlayer name(String name) { 79 | return new PlayerBuilder(Objects.requireNonNull(name, "name is required")); 80 | } 81 | 82 | 83 | } -------------------------------------------------------------------------------- /src/main/java/org/soujava/player/PlayerBuilder.java: -------------------------------------------------------------------------------- 1 | package org.soujava.player; 2 | 3 | import javax.money.MonetaryAmount; 4 | import java.time.Year; 5 | import java.util.Objects; 6 | 7 | public class PlayerBuilder implements FluentPlayer, 8 | FluentPlayer.PlayerEnd, FluentPlayer.PlayerEmail, FluentPlayer.PlayerPosition, 9 | FluentPlayer.PlayerSalary { 10 | 11 | static final Year SOCCER_BORN = Year.of(1863); 12 | 13 | PlayerBuilder(String name) { 14 | this.name = name; 15 | } 16 | 17 | private String name; 18 | 19 | private Year start; 20 | 21 | private Year end; 22 | 23 | private Email email; 24 | 25 | private Position position; 26 | 27 | private MonetaryAmount salary; 28 | 29 | @Override 30 | public PlayerEnd start(Year start) { 31 | Objects.requireNonNull(start, "start is required"); 32 | if (Year.now().isBefore(start)) { 33 | throw new IllegalArgumentException("you cannot start in the future"); 34 | } 35 | if (SOCCER_BORN.isAfter(start)) { 36 | throw new IllegalArgumentException("Soccer was not born on this time"); 37 | } 38 | this.start = start; 39 | return this; 40 | } 41 | 42 | @Override 43 | public PlayerPosition end(Year end) { 44 | Objects.requireNonNull(end, "end is required"); 45 | 46 | if (start != null && start.isAfter(end)) { 47 | throw new IllegalArgumentException("the last year of a player must be equal or higher than the start."); 48 | } 49 | 50 | if (SOCCER_BORN.isAfter(end)) { 51 | throw new IllegalArgumentException("Soccer was not born on this time"); 52 | } 53 | this.end = end; 54 | return this; 55 | } 56 | 57 | @Override 58 | public PlayerSalary position(Position position) { 59 | Objects.requireNonNull(position, "position is required"); 60 | this.position = position; 61 | return this; 62 | } 63 | 64 | @Override 65 | public PlayerEmail salary(MonetaryAmount salary) { 66 | Objects.requireNonNull(salary, "salary is required"); 67 | if (salary.isNegativeOrZero()) { 68 | throw new IllegalArgumentException("A player needs to earn money to play; otherwise, it is illegal."); 69 | } 70 | this.salary = salary; 71 | return this; 72 | } 73 | 74 | @Override 75 | public Player email(Email email) { 76 | this.email = Objects.requireNonNull(email, "email is required"); 77 | return new Player(name, start, end, email, position, salary); 78 | } 79 | 80 | @Override 81 | public Player build() { 82 | return new Player(name, start, end, email, position, salary); 83 | } 84 | } -------------------------------------------------------------------------------- /src/main/java/org/soujava/player/Position.java: -------------------------------------------------------------------------------- 1 | package org.soujava.player; 2 | 3 | public enum Position { 4 | 5 | GOALKEEPER, DEFENDER, MIDFIELDER, FORWARD; 6 | } -------------------------------------------------------------------------------- /src/main/java/org/soujava/player/Team.java: -------------------------------------------------------------------------------- 1 | package org.soujava.player; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Collections; 5 | import java.util.List; 6 | import java.util.Objects; 7 | 8 | public class Team { 9 | 10 | static final int SIZE = 20; 11 | 12 | private String name; 13 | 14 | private List players = new ArrayList<>(); 15 | 16 | @Deprecated 17 | Team() { 18 | } 19 | 20 | private Team(String name) { 21 | this.name = name; 22 | } 23 | 24 | public String getName() { 25 | return name; 26 | } 27 | 28 | public void add(Player player) { 29 | Objects.requireNonNull(player, "player is required"); 30 | 31 | if (players.size() == SIZE) { 32 | throw new IllegalArgumentException("The team is full"); 33 | } 34 | this.players.add(player); 35 | } 36 | 37 | public List getPlayers() { 38 | return Collections.unmodifiableList(players); 39 | } 40 | 41 | public static Team of(String name) { 42 | return new Team(Objects.requireNonNull(name, "name is required")); 43 | } 44 | } -------------------------------------------------------------------------------- /src/main/java/org/soujava/sandwich/Bread.java: -------------------------------------------------------------------------------- 1 | package org.soujava.sandwich; 2 | 3 | public enum Bread { 4 | ITALIAN, PLAIN, GLUTEN_FREE; 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/org/soujava/sandwich/Checkout.java: -------------------------------------------------------------------------------- 1 | package org.soujava.sandwich; 2 | 3 | import javax.money.MonetaryAmount; 4 | import java.util.Optional; 5 | 6 | public class Checkout { 7 | 8 | private final Sandwich sandwich; 9 | 10 | private final int quantity; 11 | 12 | private final Drink drink; 13 | 14 | private final int drinkQuantity; 15 | 16 | private final MonetaryAmount total; 17 | 18 | Checkout(Sandwich sandwich, int quantity, Drink drink, int drinkQuantity, MonetaryAmount total) { 19 | this.sandwich = sandwich; 20 | this.quantity = quantity; 21 | this.drink = drink; 22 | this.drinkQuantity = drinkQuantity; 23 | this.total = total; 24 | } 25 | 26 | public Sandwich getSandwich() { 27 | return sandwich; 28 | } 29 | 30 | public int getQuantity() { 31 | return quantity; 32 | } 33 | 34 | public Optional getDrink() { 35 | return Optional.ofNullable(drink); 36 | } 37 | 38 | public MonetaryAmount getTotal() { 39 | return total; 40 | } 41 | 42 | public int getDrinkQuantity() { 43 | return drinkQuantity; 44 | } 45 | 46 | @Override 47 | public String toString() { 48 | return "Checkout{" + 49 | "sandwich=" + sandwich + 50 | ", quantity=" + quantity + 51 | ", drink=" + drink + 52 | ", total=" + total + 53 | '}'; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/org/soujava/sandwich/Drink.java: -------------------------------------------------------------------------------- 1 | package org.soujava.sandwich; 2 | 3 | import javax.money.MonetaryAmount; 4 | 5 | public class Drink { 6 | 7 | private final DrinkType type; 8 | 9 | private final MonetaryAmount price; 10 | 11 | Drink(DrinkType type, MonetaryAmount price) { 12 | this.type = type; 13 | this.price = price; 14 | } 15 | 16 | public DrinkType getType() { 17 | return type; 18 | } 19 | 20 | public MonetaryAmount getPrice() { 21 | return price; 22 | } 23 | 24 | @Override 25 | public String toString() { 26 | return "Drink{" + 27 | "type=" + type + 28 | ", price=" + price + 29 | '}'; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/soujava/sandwich/DrinkType.java: -------------------------------------------------------------------------------- 1 | package org.soujava.sandwich; 2 | 3 | public enum DrinkType { 4 | SOFT_DRINK, COCKTAIL; 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/org/soujava/sandwich/Order.java: -------------------------------------------------------------------------------- 1 | package org.soujava.sandwich; 2 | 3 | import java.util.Objects; 4 | 5 | public interface Order { 6 | 7 | 8 | interface SizeOrder { 9 | StyleOrder size(Size size); 10 | } 11 | 12 | interface StyleOrder { 13 | 14 | StyleQuantityOrder vegan(); 15 | 16 | StyleQuantityOrder meat(); 17 | } 18 | 19 | interface StyleQuantityOrder extends DrinksOrder { 20 | DrinksOrder quantity(int quantity); 21 | } 22 | 23 | 24 | interface DrinksOrder { 25 | Checkout softDrink(int quantity); 26 | 27 | Checkout cocktail(int quantity); 28 | 29 | Checkout softDrink(); 30 | 31 | Checkout cocktail(); 32 | 33 | Checkout noBeveragesThanks(); 34 | } 35 | 36 | static SizeOrder bread(Bread bread) { 37 | Objects.requireNonNull(bread, "Bread is required o the order"); 38 | return new OrderFluent(bread); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/org/soujava/sandwich/OrderFluent.java: -------------------------------------------------------------------------------- 1 | package org.soujava.sandwich; 2 | 3 | import javax.money.MonetaryAmount; 4 | import java.util.Objects; 5 | 6 | class OrderFluent implements Order.SizeOrder, Order.StyleOrder, Order.StyleQuantityOrder, Order.DrinksOrder { 7 | 8 | private final PricingTables pricingTables = PricingTables.INSTANCE; 9 | 10 | private final Bread bread; 11 | 12 | private Size size; 13 | 14 | private Sandwich sandwich; 15 | 16 | private int quantity; 17 | 18 | private Drink drink; 19 | 20 | private int drinkQuantity; 21 | 22 | OrderFluent(Bread bread) { 23 | this.bread = bread; 24 | } 25 | 26 | @Override 27 | public Order.StyleOrder size(Size size) { 28 | Objects.requireNonNull(size, "Size is required"); 29 | this.size = size; 30 | return this; 31 | } 32 | 33 | @Override 34 | public Order.StyleQuantityOrder vegan() { 35 | createSandwich(SandwichStyle.VEGAN); 36 | return this; 37 | } 38 | 39 | @Override 40 | public Order.StyleQuantityOrder meat() { 41 | createSandwich(SandwichStyle.MEAT); 42 | return this; 43 | } 44 | 45 | @Override 46 | public Order.DrinksOrder quantity(int quantity) { 47 | if (quantity <= 0) { 48 | throw new IllegalArgumentException("You must request at least one sandwich"); 49 | } 50 | this.quantity = quantity; 51 | return this; 52 | } 53 | 54 | @Override 55 | public Checkout softDrink(int quantity) { 56 | if (quantity <= 0) { 57 | throw new IllegalArgumentException("You must request at least one sandwich"); 58 | } 59 | this.drinkQuantity = quantity; 60 | this.drink = new Drink(DrinkType.SOFT_DRINK, pricingTables.getPrice(DrinkType.SOFT_DRINK)); 61 | return checkout(); 62 | } 63 | 64 | @Override 65 | public Checkout cocktail(int quantity) { 66 | if (quantity <= 0) { 67 | throw new IllegalArgumentException("You must request at least one sandwich"); 68 | } 69 | this.drinkQuantity = quantity; 70 | this.drink = new Drink(DrinkType.COCKTAIL, pricingTables.getPrice(DrinkType.COCKTAIL)); 71 | return checkout(); 72 | } 73 | 74 | @Override 75 | public Checkout softDrink() { 76 | return softDrink(1); 77 | } 78 | 79 | @Override 80 | public Checkout cocktail() { 81 | return cocktail(1); 82 | } 83 | 84 | @Override 85 | public Checkout noBeveragesThanks() { 86 | return checkout(); 87 | } 88 | 89 | private Checkout checkout() { 90 | MonetaryAmount total = sandwich.getPrice().multiply(quantity); 91 | if (drink != null) { 92 | MonetaryAmount drinkTotal = drink.getPrice().multiply(drinkQuantity); 93 | total = total.add(drinkTotal); 94 | } 95 | return new Checkout(sandwich, quantity, drink, drinkQuantity, total); 96 | } 97 | 98 | private void createSandwich(SandwichStyle style) { 99 | MonetaryAmount breadPrice = pricingTables.getPrice(this.bread); 100 | MonetaryAmount sizePrice = pricingTables.getPrice(this.size); 101 | MonetaryAmount stylePrice = pricingTables.getPrice(SandwichStyle.VEGAN); 102 | MonetaryAmount total = breadPrice.add(sizePrice).add(stylePrice); 103 | this.sandwich = new Sandwich(style, this.bread, this.size, total); 104 | } 105 | } -------------------------------------------------------------------------------- /src/main/java/org/soujava/sandwich/PricingTables.java: -------------------------------------------------------------------------------- 1 | package org.soujava.sandwich; 2 | 3 | import org.javamoney.moneta.Money; 4 | 5 | import javax.money.CurrencyUnit; 6 | import javax.money.Monetary; 7 | import javax.money.MonetaryAmount; 8 | import java.util.EnumMap; 9 | import java.util.Locale; 10 | import java.util.Map; 11 | 12 | import static java.util.Optional.ofNullable; 13 | 14 | enum PricingTables { 15 | 16 | INSTANCE; 17 | 18 | private final Map sizePrice; 19 | 20 | private final Map stylePrice; 21 | 22 | private final Map drinkPrice; 23 | 24 | private final Map breadPrice; 25 | 26 | PricingTables() { 27 | CurrencyUnit currency = Monetary.getCurrency(Locale.US); 28 | 29 | this.sizePrice = new EnumMap<>(Size.class); 30 | this.sizePrice.put(Size.SMALL, Money.of(1, currency)); 31 | this.sizePrice.put(Size.MEDIUM, Money.of(5, currency)); 32 | this.sizePrice.put(Size.LARGE, Money.of(10, currency)); 33 | 34 | this.stylePrice = new EnumMap<>(SandwichStyle.class); 35 | this.stylePrice.put(SandwichStyle.MEAT, Money.of(10, currency)); 36 | this.stylePrice.put(SandwichStyle.VEGAN, Money.of(12, currency)); 37 | 38 | this.drinkPrice = new EnumMap<>(DrinkType.class); 39 | this.drinkPrice.put(DrinkType.SOFT_DRINK, Money.of(1, currency)); 40 | this.drinkPrice.put(DrinkType.COCKTAIL, Money.of(6, currency)); 41 | 42 | this.breadPrice = new EnumMap<>(Bread.class); 43 | this.breadPrice.put(Bread.PLAIN, Money.of(1, currency)); 44 | this.breadPrice.put(Bread.ITALIAN, Money.of(2, currency)); 45 | this.breadPrice.put(Bread.GLUTEN_FREE, Money.of(3, currency)); 46 | } 47 | 48 | MonetaryAmount getPrice(DrinkType type) { 49 | return ofNullable(this.drinkPrice.get(type)) 50 | .orElseThrow(() -> new IllegalArgumentException("There is not price to the drink " + type)); 51 | } 52 | 53 | MonetaryAmount getPrice(Bread bread) { 54 | return ofNullable(this.breadPrice.get(bread)) 55 | .orElseThrow(() -> new IllegalArgumentException("There is not price to the bread " + bread)); 56 | } 57 | 58 | 59 | MonetaryAmount getPrice(SandwichStyle style) { 60 | return ofNullable(this.stylePrice.get(style)) 61 | .orElseThrow(() -> new IllegalArgumentException("There is not price to the sandwich style " + style)); 62 | } 63 | 64 | MonetaryAmount getPrice(Size size) { 65 | return ofNullable(this.sizePrice.get(size)) 66 | .orElseThrow(() -> new IllegalArgumentException("There is not price to the size " + size)); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/org/soujava/sandwich/Sandwich.java: -------------------------------------------------------------------------------- 1 | package org.soujava.sandwich; 2 | 3 | import javax.money.MonetaryAmount; 4 | 5 | public class Sandwich { 6 | 7 | private final SandwichStyle style; 8 | 9 | private final Bread bread; 10 | 11 | private final Size size; 12 | 13 | private final MonetaryAmount price; 14 | 15 | Sandwich(SandwichStyle style, Bread bread, Size size, MonetaryAmount price) { 16 | this.style = style; 17 | this.bread = bread; 18 | this.size = size; 19 | this.price = price; 20 | } 21 | 22 | public SandwichStyle getStyle() { 23 | return style; 24 | } 25 | 26 | public Bread getBread() { 27 | return bread; 28 | } 29 | 30 | public Size getSize() { 31 | return size; 32 | } 33 | 34 | public MonetaryAmount getPrice() { 35 | return price; 36 | } 37 | 38 | @Override 39 | public String toString() { 40 | return "Sandwich{" + 41 | "style=" + style + 42 | ", bread=" + bread + 43 | ", size=" + size + 44 | ", price=" + price + 45 | '}'; 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/soujava/sandwich/SandwichStyle.java: -------------------------------------------------------------------------------- 1 | package org.soujava.sandwich; 2 | 3 | public enum SandwichStyle { 4 | VEGAN, MEAT; 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/org/soujava/sandwich/Size.java: -------------------------------------------------------------------------------- 1 | package org.soujava.sandwich; 2 | 3 | public enum Size { 4 | SMALL, MEDIUM, LARGE; 5 | } 6 | -------------------------------------------------------------------------------- /src/test/java/org/soujava/player/EmailTest.java: -------------------------------------------------------------------------------- 1 | package org.soujava.player; 2 | 3 | import org.junit.jupiter.api.Assertions; 4 | import org.junit.jupiter.api.Test; 5 | import org.soujava.player.Email; 6 | 7 | import static org.junit.jupiter.api.Assertions.assertNotNull; 8 | 9 | public class EmailTest { 10 | 11 | 12 | @Test 13 | public void shouldReturnErrorWhenIsNull() { 14 | Assertions.assertThrows(NullPointerException.class, () -> Email.of(null)); 15 | } 16 | 17 | @Test 18 | public void ShouldReturnErrorWhenIsInvalid() { 19 | Assertions.assertThrows(IllegalArgumentException.class, () -> Email.of("invalid.email")); 20 | } 21 | 22 | @Test 23 | public void shouldCreateEmailInstance() { 24 | assertNotNull(Email.of("email@email.com")); 25 | assertNotNull(Email.of("email.test@email.com")); 26 | assertNotNull(Email.of("email.test@gmail.com.br")); 27 | Email email = Email.of("gmail@gmail.com"); 28 | assertNotNull(email); 29 | Assertions.assertEquals("gmail@gmail.com", email.get()); 30 | } 31 | } -------------------------------------------------------------------------------- /src/test/java/org/soujava/player/PlayerTest.java: -------------------------------------------------------------------------------- 1 | package org.soujava.player; 2 | 3 | import org.javamoney.moneta.Money; 4 | import org.junit.jupiter.api.Assertions; 5 | import org.junit.jupiter.api.Test; 6 | 7 | import javax.money.CurrencyUnit; 8 | import javax.money.Monetary; 9 | import javax.money.MonetaryAmount; 10 | import java.time.Year; 11 | import java.time.temporal.ChronoUnit; 12 | import java.util.Locale; 13 | 14 | import static org.soujava.player.PlayerTestDataBuilder.*; 15 | import static org.junit.jupiter.api.Assertions.*; 16 | 17 | class PlayerTest { 18 | 19 | 20 | @Test 21 | public void shouldReturnErrorWhenStartIsFuture() { 22 | Year year = Year.now().plus(1, ChronoUnit.YEARS); 23 | assertThrows(IllegalArgumentException.class, () -> Player.name("Marta").start(year)); 24 | } 25 | 26 | @Test 27 | public void shouldReturnErrorWhenStartBeforeSoccer() { 28 | Year year = Player.SOCCER_BORN.plus(-1, ChronoUnit.YEARS); 29 | assertThrows(IllegalArgumentException.class, () -> Player.name("Marta").start(year)); 30 | } 31 | 32 | @Test 33 | public void shouldReturnErrorWhenEndBeforeSoccer() { 34 | Year year = Player.SOCCER_BORN.plus(-1, ChronoUnit.YEARS); 35 | assertThrows(IllegalArgumentException.class, () -> Player.name("Marta").start(year).end(year)); 36 | } 37 | 38 | @Test 39 | public void shouldReturnErrorWhenThereIsInvalidPeriod() { 40 | Year start = Year.now().plus(1, ChronoUnit.YEARS); 41 | Year end = Year.now(); 42 | assertThrows(IllegalArgumentException.class, () -> Player.name("Marta").start(start).end(end)); 43 | } 44 | 45 | @Test 46 | public void shouldRefuseNegativeSalary() { 47 | CurrencyUnit usd = Monetary.getCurrency(Locale.US); 48 | MonetaryAmount salary = Money.of(-1, usd); 49 | var start = Year.now(); 50 | var end = start.plusYears(1L); 51 | assertThrows(IllegalArgumentException.class, () -> 52 | Player.name("Marta").start(start).end(end).position(Position.FORWARD).salary(salary)); 53 | } 54 | 55 | @Test 56 | public void shouldCreateInstance() { 57 | Player marta = Player.name(NAME) 58 | .start(START) 59 | .end(START.plusYears(1L)) 60 | .position(POSITION) 61 | .salary(SALARY) 62 | .email(EMAIL); 63 | 64 | Assertions.assertNotNull(marta); 65 | 66 | Assertions.assertEquals(NAME, marta.getName()); 67 | Assertions.assertEquals(START, marta.getStart()); 68 | Assertions.assertEquals(POSITION, marta.getPosition()); 69 | Assertions.assertEquals(SALARY, marta.getSalary()); 70 | Assertions.assertTrue(marta.getEmail().isPresent()); 71 | Assertions.assertEquals(EMAIL, marta.getEmail().get()); 72 | } 73 | 74 | @Test 75 | public void shouldCreatePlayerWithEmailAsOptional() { 76 | Player marta = Player.name(NAME) 77 | .start(START) 78 | .end(START.plusYears(1L)) 79 | .position(POSITION) 80 | .salary(SALARY) 81 | .build(); 82 | 83 | Assertions.assertEquals(NAME, marta.getName()); 84 | Assertions.assertEquals(START, marta.getStart()); 85 | Assertions.assertEquals(POSITION, marta.getPosition()); 86 | Assertions.assertEquals(SALARY, marta.getSalary()); 87 | Assertions.assertTrue(marta.getEmail().isEmpty()); 88 | } 89 | 90 | @Test 91 | public void shouldNotAllowSetWrongPeriod() { 92 | Year end = START.plus(-1, ChronoUnit.YEARS); 93 | Player marta = PlayerTestDataBuilder.martaPlayer(); 94 | assertThrows(IllegalArgumentException.class, () -> marta.setEnd(end)); 95 | } 96 | 97 | @Test 98 | public void shouldCreateInstanceDSL() { 99 | CurrencyUnit usd = Monetary.getCurrency(Locale.US); 100 | MonetaryAmount salary = Money.of(1_000_000, usd); 101 | Email email = Email.of("marta@marta.com"); 102 | Year start = Year.now(); 103 | Year end = start.plus(1, ChronoUnit.YEARS); 104 | 105 | Player marta = Player.name("Marta") 106 | .start(start) 107 | .end(end) 108 | .position(Position.FORWARD) 109 | .salary(salary).email(email); 110 | Assertions.assertNotNull(marta); 111 | } 112 | 113 | } -------------------------------------------------------------------------------- /src/test/java/org/soujava/player/PlayerTestDataBuilder.java: -------------------------------------------------------------------------------- 1 | package org.soujava.player; 2 | 3 | import org.javamoney.moneta.Money; 4 | import org.soujava.player.Email; 5 | import org.soujava.player.Player; 6 | import org.soujava.player.Position; 7 | 8 | import javax.money.CurrencyUnit; 9 | import javax.money.Monetary; 10 | import javax.money.MonetaryAmount; 11 | import java.time.Year; 12 | import java.util.Locale; 13 | 14 | public class PlayerTestDataBuilder { 15 | 16 | public static final CurrencyUnit USD = Monetary.getCurrency(Locale.US); 17 | 18 | public static final MonetaryAmount SALARY = Money.of(1_000_000, USD); 19 | 20 | public static final Email EMAIL = Email.of("marta@marta.com"); 21 | 22 | public static final Year START = Year.now(); 23 | 24 | public static final String NAME = "Marta"; 25 | 26 | public static final Position POSITION = Position.FORWARD; 27 | 28 | public static Player martaPlayer() { 29 | return Player 30 | .name(NAME) 31 | .start(START) 32 | .end(START.plusYears(1L)) 33 | .position(POSITION) 34 | .salary(SALARY) 35 | .email(EMAIL); 36 | } 37 | } -------------------------------------------------------------------------------- /src/test/java/org/soujava/player/TeamTest.java: -------------------------------------------------------------------------------- 1 | package org.soujava.player; 2 | 3 | import org.javamoney.moneta.Money; 4 | import org.junit.jupiter.api.Test; 5 | import org.soujava.player.Email; 6 | import org.soujava.player.Player; 7 | import org.soujava.player.Position; 8 | import org.soujava.player.Team; 9 | 10 | import javax.money.CurrencyUnit; 11 | import javax.money.Monetary; 12 | import javax.money.MonetaryAmount; 13 | import java.time.Year; 14 | import java.util.Locale; 15 | 16 | import static org.junit.jupiter.api.Assertions.assertThrows; 17 | 18 | class TeamTest { 19 | 20 | @Test 21 | public void shouldReturnNPEWhenNameIsNull() { 22 | assertThrows(NullPointerException.class, () -> Team.of(null)); 23 | } 24 | 25 | @Test 26 | public void shouldReturnErrorWhenPlayerIsNull() { 27 | Team bahia = Team.of("Bahia"); 28 | assertThrows(NullPointerException.class, () -> bahia.add(null)); 29 | } 30 | 31 | @Test 32 | public void shouldReturnErrorWhenTeamIsOutOfLimit() { 33 | Team bahia = Team.of("Bahia"); 34 | CurrencyUnit usd = Monetary.getCurrency(Locale.US); 35 | Year start = Year.now(); 36 | 37 | for (int index = 0; index < Team.SIZE; index++) { 38 | MonetaryAmount salary = Money.of(1_000_000, usd); 39 | Email email = Email.of(index + "email@email.com"); 40 | 41 | Player player = Player 42 | .name("Player " + index) 43 | .start(start) 44 | .end(start.plusYears(1L)) 45 | .position(Position.FORWARD) 46 | .salary(salary) 47 | .email(email); 48 | bahia.add(player); 49 | } 50 | 51 | MonetaryAmount salary = Money.of(1_000_000, usd); 52 | Email email = Email.of("email@email.com"); 53 | Player player = Player.name("Marta") 54 | .start(start) 55 | .end(start.plusYears(1L)) 56 | .position(Position.FORWARD) 57 | .salary(salary) 58 | .email(email); 59 | 60 | assertThrows(IllegalArgumentException.class, () -> bahia.add(player)); 61 | } 62 | 63 | 64 | } -------------------------------------------------------------------------------- /src/test/java/org/soujava/sandwich/OrderTest.java: -------------------------------------------------------------------------------- 1 | package org.soujava.sandwich; 2 | 3 | import org.javamoney.moneta.Money; 4 | import org.junit.jupiter.api.Assertions; 5 | import org.junit.jupiter.api.BeforeEach; 6 | import org.junit.jupiter.api.Test; 7 | 8 | import javax.money.CurrencyUnit; 9 | import javax.money.Monetary; 10 | import java.util.Locale; 11 | 12 | public class OrderTest { 13 | 14 | private CurrencyUnit currency; 15 | 16 | @BeforeEach 17 | public void setUp() { 18 | this.currency = Monetary.getCurrency(Locale.US); 19 | } 20 | //validation invalid quantity 21 | //validation invalid quantity drinks 22 | 23 | @Test 24 | public void shouldReturnErrorWhenDrinkQuantityIsInvalid() { 25 | Assertions.assertThrows(IllegalArgumentException.class, () -> 26 | Order 27 | .bread(Bread.GLUTEN_FREE) 28 | .size(Size.LARGE) 29 | .meat() 30 | .softDrink(0)); 31 | Assertions.assertThrows(IllegalArgumentException.class, () -> 32 | Order 33 | .bread(Bread.GLUTEN_FREE) 34 | .size(Size.LARGE) 35 | .meat() 36 | .softDrink(-1)); 37 | 38 | Assertions.assertThrows(IllegalArgumentException.class, () -> 39 | Order 40 | .bread(Bread.GLUTEN_FREE) 41 | .size(Size.LARGE) 42 | .meat() 43 | .cocktail(0)); 44 | Assertions.assertThrows(IllegalArgumentException.class, () -> 45 | Order 46 | .bread(Bread.GLUTEN_FREE) 47 | .size(Size.LARGE) 48 | .meat() 49 | .cocktail(-1)); 50 | } 51 | 52 | @Test 53 | public void shouldReturnErrorWhenSandwichQuantityIsInvalid() { 54 | Assertions.assertThrows(IllegalArgumentException.class, () -> 55 | Order 56 | .bread(Bread.GLUTEN_FREE) 57 | .size(Size.LARGE) 58 | .meat() 59 | .quantity(0) 60 | .noBeveragesThanks()); 61 | 62 | Assertions.assertThrows(IllegalArgumentException.class, () -> 63 | Order 64 | .bread(Bread.GLUTEN_FREE) 65 | .size(Size.LARGE) 66 | .meat() 67 | .quantity(-1) 68 | .noBeveragesThanks()); 69 | 70 | } 71 | 72 | @Test 73 | public void shouldCreateAMeatOrderNoBeverages() { 74 | Checkout checkout = Order 75 | .bread(Bread.GLUTEN_FREE) 76 | .size(Size.LARGE) 77 | .meat() 78 | .quantity(2) 79 | .noBeveragesThanks(); 80 | 81 | var drink = checkout.getDrink(); 82 | Assertions.assertTrue(drink.isEmpty()); 83 | Assertions.assertEquals(0, checkout.getDrinkQuantity()); 84 | Sandwich sandwich = checkout.getSandwich(); 85 | Assertions.assertEquals(Bread.GLUTEN_FREE, sandwich.getBread()); 86 | Assertions.assertEquals(Size.LARGE, sandwich.getSize()); 87 | Assertions.assertEquals(SandwichStyle.MEAT, sandwich.getStyle()); 88 | Assertions.assertEquals(Money.of(25, currency), sandwich.getPrice()); 89 | Assertions.assertEquals(sandwich.getPrice().multiply(2L), checkout.getTotal()); 90 | } 91 | 92 | @Test 93 | public void shouldCreateAVeganOrderNoBeverages() { 94 | Checkout checkout = Order.bread(Bread.ITALIAN) 95 | .size(Size.MEDIUM) 96 | .meat() 97 | .quantity(1) 98 | .noBeveragesThanks(); 99 | 100 | var drink = checkout.getDrink(); 101 | Assertions.assertTrue(drink.isEmpty()); 102 | Assertions.assertEquals(0, checkout.getDrinkQuantity()); 103 | Sandwich sandwich = checkout.getSandwich(); 104 | Assertions.assertEquals(Bread.ITALIAN, sandwich.getBread()); 105 | Assertions.assertEquals(Size.MEDIUM, sandwich.getSize()); 106 | Assertions.assertEquals(SandwichStyle.MEAT, sandwich.getStyle()); 107 | Assertions.assertEquals(Money.of(19, currency), sandwich.getPrice()); 108 | Assertions.assertEquals(sandwich.getPrice(), checkout.getTotal()); 109 | } 110 | 111 | @Test 112 | public void shouldCreateAnOrderWithDrink() { 113 | Checkout checkout = Order.bread(Bread.PLAIN) 114 | .size(Size.SMALL) 115 | .meat() 116 | .quantity(2) 117 | .softDrink(2); 118 | 119 | var drink = checkout.getDrink(); 120 | Assertions.assertFalse(drink.isEmpty()); 121 | Assertions.assertEquals(2, checkout.getDrinkQuantity()); 122 | Sandwich sandwich = checkout.getSandwich(); 123 | Assertions.assertEquals(Bread.PLAIN, sandwich.getBread()); 124 | Assertions.assertEquals(Size.SMALL, sandwich.getSize()); 125 | Assertions.assertEquals(SandwichStyle.MEAT, sandwich.getStyle()); 126 | Assertions.assertEquals(Money.of(14, currency), sandwich.getPrice()); 127 | Assertions.assertEquals(Money.of(1, currency), drink.get().getPrice()); 128 | Assertions.assertEquals(Money.of(30, currency), checkout.getTotal()); 129 | } 130 | } 131 | --------------------------------------------------------------------------------