diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..097f9f9 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,9 @@ +# +# https://help.github.com/articles/dealing-with-line-endings/ +# +# Linux start script should use lf +/gradlew text eol=lf + +# These are Windows script files and should use crlf +*.bat text eol=crlf + diff --git a/.gitignore b/.gitignore index 0dc7b4b..aa0df5e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,9 @@ -/.vscode \ No newline at end of file +/.vscode +bin/ +out/ + +# Ignore Gradle project-specific cache directory +.gradle + +# Ignore Gradle build output directory +build diff --git a/README.md b/README.md index 64472f5..b26d9e1 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,16 @@ > Factory method > > Abstract factory + + +### How to run + +##### Factory Method +``` +$> ./gradlew runFactoryMethod +``` + +##### Abstract Factory +``` +$> ./gradlew runAbstractFactory +``` \ No newline at end of file diff --git a/bin/CreationalPatterns/AbstarctFactory/Factories/ArtDecoFurnitureFactory.class b/bin/CreationalPatterns/AbstarctFactory/Factories/ArtDecoFurnitureFactory.class deleted file mode 100644 index d73158a..0000000 Binary files a/bin/CreationalPatterns/AbstarctFactory/Factories/ArtDecoFurnitureFactory.class and /dev/null differ diff --git a/bin/CreationalPatterns/AbstarctFactory/Factories/ModernFurnitureFactory.class b/bin/CreationalPatterns/AbstarctFactory/Factories/ModernFurnitureFactory.class deleted file mode 100644 index e9f581a..0000000 Binary files a/bin/CreationalPatterns/AbstarctFactory/Factories/ModernFurnitureFactory.class and /dev/null differ diff --git a/bin/CreationalPatterns/AbstarctFactory/Factories/VictorianFurnitureFactory.class b/bin/CreationalPatterns/AbstarctFactory/Factories/VictorianFurnitureFactory.class deleted file mode 100644 index 837bb5f..0000000 Binary files a/bin/CreationalPatterns/AbstarctFactory/Factories/VictorianFurnitureFactory.class and /dev/null differ diff --git a/bin/CreationalPatterns/AbstarctFactory/FurnitureFactoryApp.class b/bin/CreationalPatterns/AbstarctFactory/FurnitureFactoryApp.class deleted file mode 100644 index 07da148..0000000 Binary files a/bin/CreationalPatterns/AbstarctFactory/FurnitureFactoryApp.class and /dev/null differ diff --git a/bin/CreationalPatterns/AbstarctFactory/Interfaces/IChair.class b/bin/CreationalPatterns/AbstarctFactory/Interfaces/IChair.class deleted file mode 100644 index eb98d06..0000000 Binary files a/bin/CreationalPatterns/AbstarctFactory/Interfaces/IChair.class and /dev/null differ diff --git a/bin/CreationalPatterns/AbstarctFactory/Interfaces/ICoffeeTable.class b/bin/CreationalPatterns/AbstarctFactory/Interfaces/ICoffeeTable.class deleted file mode 100644 index 402b446..0000000 Binary files a/bin/CreationalPatterns/AbstarctFactory/Interfaces/ICoffeeTable.class and /dev/null differ diff --git a/bin/CreationalPatterns/AbstarctFactory/Interfaces/IFurnitureFactory.class b/bin/CreationalPatterns/AbstarctFactory/Interfaces/IFurnitureFactory.class deleted file mode 100644 index 322ef0d..0000000 Binary files a/bin/CreationalPatterns/AbstarctFactory/Interfaces/IFurnitureFactory.class and /dev/null differ diff --git a/bin/CreationalPatterns/AbstarctFactory/Interfaces/ISofa.class b/bin/CreationalPatterns/AbstarctFactory/Interfaces/ISofa.class deleted file mode 100644 index 1364575..0000000 Binary files a/bin/CreationalPatterns/AbstarctFactory/Interfaces/ISofa.class and /dev/null differ diff --git a/bin/CreationalPatterns/AbstarctFactory/Models/Chairs/ArtDecoChair.class b/bin/CreationalPatterns/AbstarctFactory/Models/Chairs/ArtDecoChair.class deleted file mode 100644 index b4967c7..0000000 Binary files a/bin/CreationalPatterns/AbstarctFactory/Models/Chairs/ArtDecoChair.class and /dev/null differ diff --git a/bin/CreationalPatterns/AbstarctFactory/Models/Chairs/ModernChair.class b/bin/CreationalPatterns/AbstarctFactory/Models/Chairs/ModernChair.class deleted file mode 100644 index 71d529b..0000000 Binary files a/bin/CreationalPatterns/AbstarctFactory/Models/Chairs/ModernChair.class and /dev/null differ diff --git a/bin/CreationalPatterns/AbstarctFactory/Models/Chairs/VictorianChair.class b/bin/CreationalPatterns/AbstarctFactory/Models/Chairs/VictorianChair.class deleted file mode 100644 index c579754..0000000 Binary files a/bin/CreationalPatterns/AbstarctFactory/Models/Chairs/VictorianChair.class and /dev/null differ diff --git a/bin/CreationalPatterns/AbstarctFactory/Models/CoffeeTables/ArtDecoCoffeeTable.class b/bin/CreationalPatterns/AbstarctFactory/Models/CoffeeTables/ArtDecoCoffeeTable.class deleted file mode 100644 index d3ccb18..0000000 Binary files a/bin/CreationalPatterns/AbstarctFactory/Models/CoffeeTables/ArtDecoCoffeeTable.class and /dev/null differ diff --git a/bin/CreationalPatterns/AbstarctFactory/Models/CoffeeTables/ModernCoffeeTable.class b/bin/CreationalPatterns/AbstarctFactory/Models/CoffeeTables/ModernCoffeeTable.class deleted file mode 100644 index 5b805d7..0000000 Binary files a/bin/CreationalPatterns/AbstarctFactory/Models/CoffeeTables/ModernCoffeeTable.class and /dev/null differ diff --git a/bin/CreationalPatterns/AbstarctFactory/Models/CoffeeTables/VictorianCoffeeTable.class b/bin/CreationalPatterns/AbstarctFactory/Models/CoffeeTables/VictorianCoffeeTable.class deleted file mode 100644 index f51d690..0000000 Binary files a/bin/CreationalPatterns/AbstarctFactory/Models/CoffeeTables/VictorianCoffeeTable.class and /dev/null differ diff --git a/bin/CreationalPatterns/AbstarctFactory/Models/Sofas/ArtDecoSofa.class b/bin/CreationalPatterns/AbstarctFactory/Models/Sofas/ArtDecoSofa.class deleted file mode 100644 index 16f549d..0000000 Binary files a/bin/CreationalPatterns/AbstarctFactory/Models/Sofas/ArtDecoSofa.class and /dev/null differ diff --git a/bin/CreationalPatterns/AbstarctFactory/Models/Sofas/ModernSofa.class b/bin/CreationalPatterns/AbstarctFactory/Models/Sofas/ModernSofa.class deleted file mode 100644 index b2e6df3..0000000 Binary files a/bin/CreationalPatterns/AbstarctFactory/Models/Sofas/ModernSofa.class and /dev/null differ diff --git a/bin/CreationalPatterns/AbstarctFactory/Models/Sofas/VictorianSofa.class b/bin/CreationalPatterns/AbstarctFactory/Models/Sofas/VictorianSofa.class deleted file mode 100644 index 6872494..0000000 Binary files a/bin/CreationalPatterns/AbstarctFactory/Models/Sofas/VictorianSofa.class and /dev/null differ diff --git a/bin/CreationalPatterns/FactoryMethod/Enums/TransporterTypes.class b/bin/CreationalPatterns/FactoryMethod/Enums/TransporterTypes.class deleted file mode 100644 index bdb9fe9..0000000 Binary files a/bin/CreationalPatterns/FactoryMethod/Enums/TransporterTypes.class and /dev/null differ diff --git a/bin/CreationalPatterns/FactoryMethod/Factories/LogisticFactory.class b/bin/CreationalPatterns/FactoryMethod/Factories/LogisticFactory.class deleted file mode 100644 index 98ccf91..0000000 Binary files a/bin/CreationalPatterns/FactoryMethod/Factories/LogisticFactory.class and /dev/null differ diff --git a/bin/CreationalPatterns/FactoryMethod/LogisticsApp.class b/bin/CreationalPatterns/FactoryMethod/LogisticsApp.class deleted file mode 100644 index 9840f7d..0000000 Binary files a/bin/CreationalPatterns/FactoryMethod/LogisticsApp.class and /dev/null differ diff --git a/bin/CreationalPatterns/FactoryMethod/Models/Logistic.class b/bin/CreationalPatterns/FactoryMethod/Models/Logistic.class deleted file mode 100644 index bbf51a9..0000000 Binary files a/bin/CreationalPatterns/FactoryMethod/Models/Logistic.class and /dev/null differ diff --git a/bin/CreationalPatterns/FactoryMethod/Models/RoadLogistics.class b/bin/CreationalPatterns/FactoryMethod/Models/RoadLogistics.class deleted file mode 100644 index bf04575..0000000 Binary files a/bin/CreationalPatterns/FactoryMethod/Models/RoadLogistics.class and /dev/null differ diff --git a/bin/CreationalPatterns/FactoryMethod/Models/SeaLogistics.class b/bin/CreationalPatterns/FactoryMethod/Models/SeaLogistics.class deleted file mode 100644 index ec6c641..0000000 Binary files a/bin/CreationalPatterns/FactoryMethod/Models/SeaLogistics.class and /dev/null differ diff --git a/bin/CreationalPatterns/FactoryMethod/Models/Vechile.class b/bin/CreationalPatterns/FactoryMethod/Models/Vechile.class deleted file mode 100644 index d09505e..0000000 Binary files a/bin/CreationalPatterns/FactoryMethod/Models/Vechile.class and /dev/null differ diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..bd965a7 --- /dev/null +++ b/build.gradle @@ -0,0 +1,35 @@ +/* + * This file was generated by the Gradle 'init' task. + * + * This generated file contains a sample Java application project to get you started. + * For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.6/userguide/building_java_projects.html in the Gradle documentation. + */ + +plugins { + // Apply the application plugin to add support for building a CLI application in Java. + id 'application' + id 'java' +} + +repositories { + // Use Maven Central for resolving dependencies. + mavenCentral() +} + +// Apply a specific Java toolchain to ease working on different environments. +java { + toolchain { + languageVersion = JavaLanguageVersion.of(17) + } +} + + +task runLogisticsApp(type: JavaExec) { + mainClass = 'creationalpatterns.factorymethod.logistics.LogisticsApp' + classpath = sourceSets.main.runtimeClasspath +} + +task runFurnitureApp(type: JavaExec) { + mainClass = 'creationalpatterns.abstarctfactory.FurnitureFactoryApp' + classpath = sourceSets.main.runtimeClasspath +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml new file mode 100644 index 0000000..4ac3234 --- /dev/null +++ b/gradle/libs.versions.toml @@ -0,0 +1,2 @@ +# This file was generated by the Gradle 'init' task. +# https://docs.gradle.org/current/userguide/platforms.html#sub::toml-dependencies-format diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar new file mode 100644 index 0000000..d64cd49 Binary files /dev/null and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..a80b22c --- /dev/null +++ b/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,7 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip +networkTimeout=10000 +validateDistributionUrl=true +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/gradlew b/gradlew new file mode 100755 index 0000000..1aa94a4 --- /dev/null +++ b/gradlew @@ -0,0 +1,249 @@ +#!/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/HEAD/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 + +# This is normally unused +# shellcheck disable=SC2034 +APP_BASE_NAME=${0##*/} +# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) +APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit + +# 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 + if ! command -v java >/dev/null 2>&1 + then + 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 +fi + +# Increase the maximum file descriptors if we can. +if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then + case $MAX_FD in #( + max*) + # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + MAX_FD=$( ulimit -H -n ) || + warn "Could not query maximum file descriptor limit" + esac + case $MAX_FD in #( + '' | soft) :;; #( + *) + # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. + # shellcheck disable=SC2039,SC3045 + 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 + + +# 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"' + +# Collect all arguments for the java command: +# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# and any embedded shellness will be escaped. +# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be +# treated as '${Hostname}' itself on the command line. + +set -- \ + "-Dorg.gradle.appname=$APP_BASE_NAME" \ + -classpath "$CLASSPATH" \ + org.gradle.wrapper.GradleWrapperMain \ + "$@" + +# Stop when "xargs" is not available. +if ! command -v xargs >/dev/null 2>&1 +then + die "xargs is not available" +fi + +# 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/gradlew.bat b/gradlew.bat new file mode 100644 index 0000000..25da30d --- /dev/null +++ b/gradlew.bat @@ -0,0 +1,92 @@ +@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=. +@rem This is normally unused +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% equ 0 goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto execute + +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 + +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% equ 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! +set EXIT_CODE=%ERRORLEVEL% +if %EXIT_CODE% equ 0 set EXIT_CODE=1 +if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% +exit /b %EXIT_CODE% + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..09f020f --- /dev/null +++ b/settings.gradle @@ -0,0 +1,14 @@ +/* + * This file was generated by the Gradle 'init' task. + * + * The settings file is used to specify which projects to include in your build. + * For more detailed information on multi-project builds, please refer to https://docs.gradle.org/8.6/userguide/multi_project_builds.html in the Gradle documentation. + */ + +plugins { + // Apply the foojay-resolver plugin to allow automatic download of JDKs + id 'org.gradle.toolchains.foojay-resolver-convention' version '0.7.0' +} + +rootProject.name = 'DesignPatterns' +include('DesignPatterns') \ No newline at end of file diff --git a/src/CreationalPatterns/AbstarctFactory/Factories/FurnitureFactory.java b/src/CreationalPatterns/AbstarctFactory/Factories/FurnitureFactory.java deleted file mode 100644 index 6fa0fd4..0000000 --- a/src/CreationalPatterns/AbstarctFactory/Factories/FurnitureFactory.java +++ /dev/null @@ -1,116 +0,0 @@ -package CreationalPatterns.AbstarctFactory.Factories; - -import java.util.ArrayList; -import java.util.List; - -import CreationalPatterns.AbstarctFactory.Models.Chair; -import CreationalPatterns.AbstarctFactory.Models.CoffeeTable; -import CreationalPatterns.AbstarctFactory.Models.Order; -import CreationalPatterns.AbstarctFactory.Models.Sofa; -import CreationalPatterns.AbstarctFactory.Models.Enums.FurnitureStyles; - -public class FurnitureFactory -{ - private FurnitureStyles _furnitureStyle; - private List _orders; - private FurnitureFactory _artDecoFactory, _modernFactory, _victorianFactory, _factory; - - public FurnitureFactory() { } - - public FurnitureFactory(FurnitureStyles furnitureStyle) - { - this._furnitureStyle = furnitureStyle; - this._orders = new ArrayList(); - } - - public FurnitureFactory getInstance(FurnitureStyles furnitureStyle) - { - if (furnitureStyle == FurnitureStyles.ArtDeco) - { - if (this._artDecoFactory == null) - { - this._artDecoFactory = new FurnitureFactory(furnitureStyle); - } - - return this._artDecoFactory; - } - else if (furnitureStyle == FurnitureStyles.Modern) - { - if (this._modernFactory == null) - { - this._modernFactory = new FurnitureFactory(furnitureStyle); - } - - return this._modernFactory; - } - else if (furnitureStyle == FurnitureStyles.Victorian) - { - if (this._victorianFactory == null) - { - this._victorianFactory = new FurnitureFactory(furnitureStyle); - } - - return this._victorianFactory; - } - else - { - if (this._factory == null) - { - this._factory = new FurnitureFactory(); - } - - return this._factory; - } - } - - public Order createOrder() - { - return new Order(createChair(), createSofa(), createCoffeeTable()); - } - - public void addOrder(Order order) - { - this._orders.add(order); - } - - public List getOrders() - { - var orders = new ArrayList(); - if (this._artDecoFactory != null) - { - orders.addAll(this._artDecoFactory.getOrders()); - } - - if (this._modernFactory != null) - { - orders.addAll(this._modernFactory.getOrders()); - } - - if (this._victorianFactory != null) - { - orders.addAll(this._victorianFactory.getOrders()); - } - - if (this._furnitureStyle == null) - { - this._orders = orders; - } - - return this._orders; - } - - private Chair createChair() - { - return new Chair(this._furnitureStyle); - } - - private Sofa createSofa() - { - return new Sofa(_furnitureStyle); - } - - private CoffeeTable createCoffeeTable() - { - return new CoffeeTable(_furnitureStyle); - } -} diff --git a/src/CreationalPatterns/FactoryMethod/Enums/VechileTypes.java b/src/CreationalPatterns/FactoryMethod/Enums/VechileTypes.java deleted file mode 100644 index b0dc0bc..0000000 --- a/src/CreationalPatterns/FactoryMethod/Enums/VechileTypes.java +++ /dev/null @@ -1,7 +0,0 @@ -package CreationalPatterns.FactoryMethod.Enums; - -public enum VechileTypes -{ - Truck, - Boat -} diff --git a/src/CreationalPatterns/FactoryMethod/Factories/LogisticFactory.java b/src/CreationalPatterns/FactoryMethod/Factories/LogisticFactory.java deleted file mode 100644 index e9d57eb..0000000 --- a/src/CreationalPatterns/FactoryMethod/Factories/LogisticFactory.java +++ /dev/null @@ -1,106 +0,0 @@ -package CreationalPatterns.FactoryMethod.Factories; - -import java.util.ArrayList; -import java.util.List; - -import CreationalPatterns.FactoryMethod.Enums.VechileTypes; -import CreationalPatterns.FactoryMethod.Models.Logistic; -import CreationalPatterns.FactoryMethod.Models.RoadLogistic; -import CreationalPatterns.FactoryMethod.Models.SeaLogistic; -import CreationalPatterns.FactoryMethod.Models.Vechile; - -public class LogisticFactory -{ - private Logistic _seaLogisticSingletonInstance, _roadLogisticSingletonInstance; - private List _vechiles, _vechilesOnRoad, _vechilesNotOnRoad; - - public LogisticFactory() - { - this._vechiles = this._vechilesOnRoad = this._vechilesNotOnRoad = new ArrayList(); - } - - public Logistic getLogistic(VechileTypes vechileType) throws Exception - { - if (vechileType == VechileTypes.Boat) - { - if (this._seaLogisticSingletonInstance == null) - { - this._seaLogisticSingletonInstance = new SeaLogistic(); - } - - return this._seaLogisticSingletonInstance; - } - else if (vechileType == VechileTypes.Truck) - { - if (this._roadLogisticSingletonInstance == null) - { - this._roadLogisticSingletonInstance = new RoadLogistic(); - } - - return this._roadLogisticSingletonInstance; - } - else - { - throw new Exception("Out of range"); - } - } - - public void upsertVechiles(List vechiles) throws Exception - { - if (vechiles == null) - { - throw new Exception("Vechiles shouldn't be null."); - } - - for (var vechile : vechiles) - { - if (vechile.getVechileType() == VechileTypes.Boat && vechile.isUpdated()) - { - upsertVechileBasedOnType(this._seaLogisticSingletonInstance, vechile); - } - else if (vechile.getVechileType() == VechileTypes.Truck && vechile.isUpdated()) - { - upsertVechileBasedOnType(this._roadLogisticSingletonInstance, vechile); - } - } - - this._vechiles = new ArrayList(); - this._vechiles.addAll(this._seaLogisticSingletonInstance.getVechiles()); - this._vechiles.addAll(this._roadLogisticSingletonInstance.getVechiles()); - - this._vechilesOnRoad = this._vechiles.stream().filter(x -> x.getOnRoad() == true).toList(); - this._vechilesNotOnRoad = this._vechiles.stream().filter(x -> x.getOnRoad() == false).toList(); - } - - public List getAllVechiles() - { - return this._vechiles; - } - - public List getVechilesOnRoad() - { - return this._vechilesOnRoad; - } - - public List getVechilesNotOnRoad() - { - return this._vechilesNotOnRoad; - } - - public List getVechilesByType(VechileTypes vechileType) - { - return this._vechiles.stream().filter(x -> x.getVechileType() == vechileType).toList(); - } - - public void upsertVechileBasedOnType(Logistic logistic, Vechile vechile) throws Exception - { - if (logistic == null) - { - throw new Exception("Not Found Instance"); - } - else - { - logistic.upsertVechile(vechile); - } - } -} diff --git a/src/CreationalPatterns/FactoryMethod/LogisticsApp.java b/src/CreationalPatterns/FactoryMethod/LogisticsApp.java deleted file mode 100644 index da03a82..0000000 --- a/src/CreationalPatterns/FactoryMethod/LogisticsApp.java +++ /dev/null @@ -1,86 +0,0 @@ -package CreationalPatterns.FactoryMethod; - -import java.util.List; -import java.util.Random; - -import CreationalPatterns.FactoryMethod.Enums.VechileTypes; -import CreationalPatterns.FactoryMethod.Factories.LogisticFactory; -import CreationalPatterns.FactoryMethod.Models.Logistic; -import CreationalPatterns.FactoryMethod.Models.Vechile; - -public class LogisticsApp -{ - public static void main(String[] args) - { - try - { - LogisticFactory logisticFactory = new LogisticFactory(); - Logistic roadLogistics = logisticFactory.getLogistic(VechileTypes.Truck); - Logistic seaLogistics = logisticFactory.getLogistic(VechileTypes.Boat); - - setMock(roadLogistics); - setMock(seaLogistics); - - var roadTransporters = roadLogistics.getVechiles(); - var seaTransporters = seaLogistics.getVechiles(); - - logisticFactory.upsertVechiles(roadTransporters); - logisticFactory.upsertVechiles(seaTransporters); - - printTransporters("[Road Logistic] : Transporters", roadTransporters); - printTransporters("[Sea Logistic] : Transporters", seaTransporters); - - printTransporters("[Logistic] : All transporters", logisticFactory.getAllVechiles()); - - printTransporters("[Logistic] : Transporters On Road", logisticFactory.getVechilesOnRoad()); - printTransporters("[Logistic] : Transporters Not On Road", logisticFactory.getVechilesNotOnRoad()); - - printTransporters("[Logistic] : Trucks", logisticFactory.getVechilesByType(VechileTypes.Truck)); - printTransporters("[Logistic] : Boats", logisticFactory.getVechilesByType(VechileTypes.Boat)); - } - catch (Exception ex) - { - System.out.println(ex.getMessage()); - } - } - - private static void setMock(Logistic logistic) - { - try - { - Random random = new Random(); - for(Integer i = 0; i < 5; i++) - { - var newVechile = logistic.createVechile(); - newVechile.setLoadWeight(random.nextDouble()); - newVechile.setRoadDistance(random.nextInt()); - - if (i % 2 == 0) - { - newVechile.setOnRoad(true); - } - - logistic.upsertVechile(newVechile); - } - } - catch (Exception ex) - { - System.out.println(ex.getMessage()); - } - } - - private static void printTransporters(String title, List vechiles) - { - System.out.println("\n" + title); - for(var vechile : vechiles) - { - System.out.println("---"); - System.out.println(vechile.getId()); - System.out.println(vechile.getLoadWeight()); - System.out.println(vechile.getRoadDistance()); - System.out.println(vechile.getOnRoad()); - System.out.println(vechile.getVechileType()); - System.out.println("--- ---\n"); - } - } -} \ No newline at end of file diff --git a/src/CreationalPatterns/FactoryMethod/Models/Logistic.java b/src/CreationalPatterns/FactoryMethod/Models/Logistic.java deleted file mode 100644 index 7edb03b..0000000 --- a/src/CreationalPatterns/FactoryMethod/Models/Logistic.java +++ /dev/null @@ -1,65 +0,0 @@ -package CreationalPatterns.FactoryMethod.Models; - -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; - -import CreationalPatterns.FactoryMethod.Enums.VechileTypes; - -public class Logistic -{ - private List _vechiles; - private VechileTypes _vechileType; - - public Logistic(VechileTypes vechileType) - { - this._vechiles = new ArrayList(); - this._vechileType = vechileType; - } - - public Vechile createVechile() - { - var vechile = new Vechile(this._vechileType); - _vechiles.add(vechile); - - return vechile; - } - - public void upsertVechile(Vechile vechile) throws Exception - { - if (vechile == null) - { - throw new Exception("Vechile shouldn't be null."); - } - - if (!vechile.isUpdated()) - { - throw new Exception("This vechile's not updated."); - } - else - { - var oldVechile = _vechiles.stream().filter(item -> item.getId() == vechile.getId()).findFirst().get(); - _vechiles.set(_vechiles.indexOf(oldVechile), vechile); - } - } - - public List getVechileOnRoad() - { - return this._vechiles.stream().filter(x -> x.getOnRoad() == true).toList(); - } - - public List getVechileNotOnRoad() - { - return this._vechiles.stream().filter(x -> x.getOnRoad() == false).toList(); - } - - public List getVechiles() - { - return this._vechiles; - } - - public Vechile getVechileById(UUID id) - { - return _vechiles.stream().filter(x -> x.getId() == id).findFirst().get(); - } -} diff --git a/src/CreationalPatterns/FactoryMethod/Models/RoadLogistic.java b/src/CreationalPatterns/FactoryMethod/Models/RoadLogistic.java deleted file mode 100644 index c2a101c..0000000 --- a/src/CreationalPatterns/FactoryMethod/Models/RoadLogistic.java +++ /dev/null @@ -1,12 +0,0 @@ -package CreationalPatterns.FactoryMethod.Models; - -import CreationalPatterns.FactoryMethod.Enums.VechileTypes; - -public class RoadLogistic extends Logistic -{ - public RoadLogistic() - { - super(VechileTypes.Truck); - } - -} diff --git a/src/CreationalPatterns/FactoryMethod/Models/SeaLogistic.java b/src/CreationalPatterns/FactoryMethod/Models/SeaLogistic.java deleted file mode 100644 index 4ab4436..0000000 --- a/src/CreationalPatterns/FactoryMethod/Models/SeaLogistic.java +++ /dev/null @@ -1,13 +0,0 @@ -package CreationalPatterns.FactoryMethod.Models; - -import CreationalPatterns.FactoryMethod.Enums.VechileTypes; - -public class SeaLogistic extends Logistic -{ - - public SeaLogistic() - { - super(VechileTypes.Boat); - } - -} diff --git a/src/CreationalPatterns/FactoryMethod/Models/Vechile.java b/src/CreationalPatterns/FactoryMethod/Models/Vechile.java deleted file mode 100644 index 7470119..0000000 --- a/src/CreationalPatterns/FactoryMethod/Models/Vechile.java +++ /dev/null @@ -1,97 +0,0 @@ -package CreationalPatterns.FactoryMethod.Models; - -import java.util.UUID; - -import CreationalPatterns.FactoryMethod.Enums.VechileTypes; - -public class Vechile -{ - private UUID _id; - private Integer _roadDistance; - private Double _loadWeight; - private Boolean _onRoad = false; - private VechileTypes _vechileType; - private Boolean _isUpdated; - - public Vechile() - { - this._id = UUID.randomUUID(); - } - - public Vechile(VechileTypes vechileType) - { - this._id = UUID.randomUUID(); - this._vechileType = vechileType; - } - - //Getters - public UUID getId() throws NullPointerException - { - throwNullExceptionIfNull(this._id, "Id"); - return this._id; - } - - public Integer getRoadDistance() throws NullPointerException - { - throwNullExceptionIfNull(this._roadDistance, "Road Distance"); - return this._roadDistance; - } - - public Double getLoadWeight() throws NullPointerException - { - throwNullExceptionIfNull(this._loadWeight, "Load Weight"); - return this._loadWeight; - } - - public Boolean getOnRoad() throws NullPointerException - { - throwNullExceptionIfNull(this._onRoad, "On Road"); - return this._onRoad; - } - - public VechileTypes getVechileType() throws NullPointerException - { - throwNullExceptionIfNull(this._vechileType, "Transporter Type"); - return this._vechileType; - } - - public Boolean isUpdated() - { - return this._isUpdated; - } - - //Setters - public void setRoadDistance(Integer roadDistance) throws NullPointerException - { - throwNullExceptionIfNull(roadDistance, "Road Distance"); - this._roadDistance = roadDistance; - } - - public void setLoadWeight(Double loadWeight) throws NullPointerException - { - throwNullExceptionIfNull(loadWeight, "Load Weight"); - this._loadWeight = loadWeight; - } - - public void setOnRoad(Boolean isOnRoad) throws NullPointerException - { - throwNullExceptionIfNull(isOnRoad, "Is On Road"); - this._onRoad = isOnRoad; - } - - public void setTransporterType(VechileTypes transporterType) - { - throwNullExceptionIfNull(transporterType, "Transporter Type"); - this._vechileType = transporterType; - } - - private void throwNullExceptionIfNull(Object obj, String propertyName) - { - if (obj == null) - { - throw new NullPointerException(propertyName + " is null."); - } - - this._isUpdated = true; - } -} diff --git a/src/CreationalPatterns/AbstarctFactory/FurnitureFactoryApp.java b/src/main/java/creationalpatterns/abstarctfactory/FurnitureFactoryApp.java similarity index 94% rename from src/CreationalPatterns/AbstarctFactory/FurnitureFactoryApp.java rename to src/main/java/creationalpatterns/abstarctfactory/FurnitureFactoryApp.java index eb500ce..19b03a1 100644 --- a/src/CreationalPatterns/AbstarctFactory/FurnitureFactoryApp.java +++ b/src/main/java/creationalpatterns/abstarctfactory/FurnitureFactoryApp.java @@ -1,9 +1,9 @@ -package CreationalPatterns.AbstarctFactory; +package creationalpatterns.abstarctfactory; import java.util.Scanner; -import CreationalPatterns.AbstarctFactory.Factories.FurnitureFactory; -import CreationalPatterns.AbstarctFactory.Models.Enums.FurnitureStyles; +import creationalpatterns.abstarctfactory.factories.FurnitureFactory; +import creationalpatterns.abstarctfactory.models.Enums.FurnitureStyles; public class FurnitureFactoryApp { diff --git a/src/main/java/creationalpatterns/abstarctfactory/factories/FurnitureFactory.java b/src/main/java/creationalpatterns/abstarctfactory/factories/FurnitureFactory.java new file mode 100644 index 0000000..cf0183a --- /dev/null +++ b/src/main/java/creationalpatterns/abstarctfactory/factories/FurnitureFactory.java @@ -0,0 +1,116 @@ +package creationalpatterns.abstarctfactory.factories; + +import java.util.ArrayList; +import java.util.List; + +import creationalpatterns.abstarctfactory.models.Chair; +import creationalpatterns.abstarctfactory.models.CoffeeTable; +import creationalpatterns.abstarctfactory.models.Order; +import creationalpatterns.abstarctfactory.models.Sofa; +import creationalpatterns.abstarctfactory.models.Enums.FurnitureStyles; + +public class FurnitureFactory +{ + private FurnitureStyles furnitureStyle; + private List orders; + private FurnitureFactory artDecoFactory, modernFactory, victorianFactory, _factory; + + public FurnitureFactory() { } + + public FurnitureFactory(FurnitureStyles furnitureStyle) + { + this.furnitureStyle = furnitureStyle; + this.orders = new ArrayList(); + } + + public FurnitureFactory getInstance(FurnitureStyles furnitureStyle) + { + if (furnitureStyle == FurnitureStyles.ArtDeco) + { + if (this.artDecoFactory == null) + { + this.artDecoFactory = new FurnitureFactory(furnitureStyle); + } + + return this.artDecoFactory; + } + else if (furnitureStyle == FurnitureStyles.Modern) + { + if (this.modernFactory == null) + { + this.modernFactory = new FurnitureFactory(furnitureStyle); + } + + return this.modernFactory; + } + else if (furnitureStyle == FurnitureStyles.Victorian) + { + if (this.victorianFactory == null) + { + this.victorianFactory = new FurnitureFactory(furnitureStyle); + } + + return this.victorianFactory; + } + else + { + if (this._factory == null) + { + this._factory = new FurnitureFactory(); + } + + return this._factory; + } + } + + public Order createOrder() + { + return new Order(createChair(), createSofa(), createCoffeeTable()); + } + + public void addOrder(Order order) + { + this.orders.add(order); + } + + public List getOrders() + { + var orders = new ArrayList(); + if (this.artDecoFactory != null) + { + orders.addAll(this.artDecoFactory.getOrders()); + } + + if (this.modernFactory != null) + { + orders.addAll(this.modernFactory.getOrders()); + } + + if (this.victorianFactory != null) + { + orders.addAll(this.victorianFactory.getOrders()); + } + + if (this.furnitureStyle == null) + { + this.orders = orders; + } + + return this.orders; + } + + private Chair createChair() + { + return new Chair(this.furnitureStyle); + } + + private Sofa createSofa() + { + return new Sofa(furnitureStyle); + } + + private CoffeeTable createCoffeeTable() + { + return new CoffeeTable(furnitureStyle); + } +} diff --git a/src/CreationalPatterns/AbstarctFactory/Models/Chair.java b/src/main/java/creationalpatterns/abstarctfactory/models/Chair.java similarity index 81% rename from src/CreationalPatterns/AbstarctFactory/Models/Chair.java rename to src/main/java/creationalpatterns/abstarctfactory/models/Chair.java index 210b6d9..eb2d097 100644 --- a/src/CreationalPatterns/AbstarctFactory/Models/Chair.java +++ b/src/main/java/creationalpatterns/abstarctfactory/models/Chair.java @@ -1,6 +1,6 @@ -package CreationalPatterns.AbstarctFactory.Models; +package creationalpatterns.abstarctfactory.models; -import CreationalPatterns.AbstarctFactory.Models.Enums.FurnitureStyles; +import creationalpatterns.abstarctfactory.models.Enums.FurnitureStyles; public class Chair { diff --git a/src/CreationalPatterns/AbstarctFactory/Models/CoffeeTable.java b/src/main/java/creationalpatterns/abstarctfactory/models/CoffeeTable.java similarity index 89% rename from src/CreationalPatterns/AbstarctFactory/Models/CoffeeTable.java rename to src/main/java/creationalpatterns/abstarctfactory/models/CoffeeTable.java index f81fca2..6482d46 100644 --- a/src/CreationalPatterns/AbstarctFactory/Models/CoffeeTable.java +++ b/src/main/java/creationalpatterns/abstarctfactory/models/CoffeeTable.java @@ -1,6 +1,6 @@ -package CreationalPatterns.AbstarctFactory.Models; +package creationalpatterns.abstarctfactory.models; -import CreationalPatterns.AbstarctFactory.Models.Enums.FurnitureStyles; +import creationalpatterns.abstarctfactory.models.Enums.FurnitureStyles; public class CoffeeTable { diff --git a/src/CreationalPatterns/AbstarctFactory/Models/Enums/FurnitureStyles.java b/src/main/java/creationalpatterns/abstarctfactory/models/Enums/FurnitureStyles.java similarity index 57% rename from src/CreationalPatterns/AbstarctFactory/Models/Enums/FurnitureStyles.java rename to src/main/java/creationalpatterns/abstarctfactory/models/Enums/FurnitureStyles.java index de02b55..ac6e0c8 100644 --- a/src/CreationalPatterns/AbstarctFactory/Models/Enums/FurnitureStyles.java +++ b/src/main/java/creationalpatterns/abstarctfactory/models/Enums/FurnitureStyles.java @@ -1,4 +1,4 @@ -package CreationalPatterns.AbstarctFactory.Models.Enums; +package creationalpatterns.abstarctfactory.models.Enums; public enum FurnitureStyles { diff --git a/src/CreationalPatterns/AbstarctFactory/Models/Order.java b/src/main/java/creationalpatterns/abstarctfactory/models/Order.java similarity index 92% rename from src/CreationalPatterns/AbstarctFactory/Models/Order.java rename to src/main/java/creationalpatterns/abstarctfactory/models/Order.java index 69e33a0..01b4266 100644 --- a/src/CreationalPatterns/AbstarctFactory/Models/Order.java +++ b/src/main/java/creationalpatterns/abstarctfactory/models/Order.java @@ -1,4 +1,4 @@ -package CreationalPatterns.AbstarctFactory.Models; +package creationalpatterns.abstarctfactory.models; import java.util.UUID; diff --git a/src/CreationalPatterns/AbstarctFactory/Models/Sofa.java b/src/main/java/creationalpatterns/abstarctfactory/models/Sofa.java similarity index 81% rename from src/CreationalPatterns/AbstarctFactory/Models/Sofa.java rename to src/main/java/creationalpatterns/abstarctfactory/models/Sofa.java index 0d6cc58..0e8bdea 100644 --- a/src/CreationalPatterns/AbstarctFactory/Models/Sofa.java +++ b/src/main/java/creationalpatterns/abstarctfactory/models/Sofa.java @@ -1,6 +1,6 @@ -package CreationalPatterns.AbstarctFactory.Models; +package creationalpatterns.abstarctfactory.models; -import CreationalPatterns.AbstarctFactory.Models.Enums.FurnitureStyles; +import creationalpatterns.abstarctfactory.models.Enums.FurnitureStyles; public class Sofa { diff --git a/src/main/java/creationalpatterns/factorymethod/logistics/Logistic.java b/src/main/java/creationalpatterns/factorymethod/logistics/Logistic.java new file mode 100644 index 0000000..17a281c --- /dev/null +++ b/src/main/java/creationalpatterns/factorymethod/logistics/Logistic.java @@ -0,0 +1,48 @@ +package creationalpatterns.factorymethod.logistics; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public abstract class Logistic { + private final List vehicleList = new ArrayList<>(); + + public void addVehicle(int distance, int loadCapacity) { + if (distance > getMaxRange()) { + throw new IllegalArgumentException(String.format("%s does not support the distance %d km", this.getClass().getSimpleName(), distance)); + } + if (loadCapacity > getMaxLoadCapacity()) { + throw new IllegalArgumentException(String.format("%s does not support the load %d ton", this.getClass().getSimpleName(), loadCapacity)); + } + + var vehicle = createVehicle(distance, loadCapacity); + this.vehicleList.add(vehicle); + } + + public List getAllVehicles() { + return Collections.unmodifiableList(vehicleList); + } + + public List getVechilesOnRoad() { + return this.vehicleList.stream().filter(Vehicle::isOnRoad).toList(); + } + + public List findSuitableVehicles(int distance, double weight) { + return this.vehicleList.stream().filter(v -> !v.isOnRoad() && v.getRange() >= distance && v.getLoadCapacity() >= weight).toList(); + } + + public void parkAllVehicles() { + vehicleList.forEach(v -> v.setOnRoad(false)); + } + + protected abstract Vehicle createVehicle(int range, int loadCapacity); + + protected abstract int getMaxRange(); + + protected abstract int getMaxLoadCapacity(); + + @Override + public String toString() { + return this.getClass().getSimpleName(); + } +} diff --git a/src/main/java/creationalpatterns/factorymethod/logistics/LogisticFactory.java b/src/main/java/creationalpatterns/factorymethod/logistics/LogisticFactory.java new file mode 100644 index 0000000..ca1aaea --- /dev/null +++ b/src/main/java/creationalpatterns/factorymethod/logistics/LogisticFactory.java @@ -0,0 +1,17 @@ +package creationalpatterns.factorymethod.logistics; + +public final class LogisticFactory { + + public static Logistic createLogistic(int distance, int loadWeight) { + + if (distance <= 1500 && loadWeight <= 50) { + return new RoadLogistic(); + } else if (distance <= 20_000 && loadWeight <= 100_000) { + return new SeaLogistic(); + } else { + throw new IllegalArgumentException("There is no any logistic for longer than 20.000Km distance and heavier than 100 Gross Ton load"); + } + + } + +} diff --git a/src/main/java/creationalpatterns/factorymethod/logistics/LogisticsApp.java b/src/main/java/creationalpatterns/factorymethod/logistics/LogisticsApp.java new file mode 100644 index 0000000..00c7054 --- /dev/null +++ b/src/main/java/creationalpatterns/factorymethod/logistics/LogisticsApp.java @@ -0,0 +1,41 @@ +package creationalpatterns.factorymethod.logistics; + + +import java.util.Random; + +public class LogisticsApp { + public static void main(String[] args) { + try { + + buildLogisticsFor(650, 20); + buildLogisticsFor(300, 45); + buildLogisticsFor(11_000, 50); + buildLogisticsFor(5_000, 75_000); + + + } catch (Exception ex) { + ex.printStackTrace(System.err); + } + } + + public static void buildLogisticsFor(int distance, int load) { + var logistic = LogisticFactory.createLogistic(distance, load); + println("==========================================================================="); + println("Logistics for %d km distance and %d ton load: %s", distance, load, logistic); + + var random = new Random(System.currentTimeMillis()); + + for (int i = 0; i < random.nextInt(1, 10); i++) { + var range = random.nextInt(distance, logistic.getMaxRange()); + var loadCapacity = random.nextInt(load, logistic.getMaxLoadCapacity()); + logistic.addVehicle(range, loadCapacity); + } + + println("Vehicles:"); + logistic.getAllVehicles().forEach(v -> println("\tVehicle: %s", v)); + } + + public static void println(String message, Object... args) { + System.out.printf(message + "%n", args); + } +} \ No newline at end of file diff --git a/src/main/java/creationalpatterns/factorymethod/logistics/RoadLogistic.java b/src/main/java/creationalpatterns/factorymethod/logistics/RoadLogistic.java new file mode 100644 index 0000000..c1ceb94 --- /dev/null +++ b/src/main/java/creationalpatterns/factorymethod/logistics/RoadLogistic.java @@ -0,0 +1,23 @@ +package creationalpatterns.factorymethod.logistics; + +import static creationalpatterns.factorymethod.logistics.VehicleType.Truck; + +public class RoadLogistic extends Logistic { + public static final int MAX_RANGE = 1_500; + public static final int MAX_LOAD = 50; + + @Override + protected Vehicle createVehicle(int range, int loadCapacity) { + return new Vehicle(Truck, range, loadCapacity); + } + + @Override + protected int getMaxRange() { + return MAX_RANGE; + } + + @Override + protected int getMaxLoadCapacity() { + return MAX_LOAD; + } +} diff --git a/src/main/java/creationalpatterns/factorymethod/logistics/SeaLogistic.java b/src/main/java/creationalpatterns/factorymethod/logistics/SeaLogistic.java new file mode 100644 index 0000000..3971c54 --- /dev/null +++ b/src/main/java/creationalpatterns/factorymethod/logistics/SeaLogistic.java @@ -0,0 +1,23 @@ +package creationalpatterns.factorymethod.logistics; + +import static creationalpatterns.factorymethod.logistics.VehicleType.Ship; + +public class SeaLogistic extends Logistic { + public static final int MAX_RANGE = 20_000; + public static final int MAX_LOAD = 100_000; + + @Override + protected Vehicle createVehicle(int range, int loadCapacity) { + return new Vehicle(Ship, range, loadCapacity); + } + + @Override + protected int getMaxRange() { + return MAX_RANGE; + } + + @Override + protected int getMaxLoadCapacity() { + return MAX_LOAD; + } +} diff --git a/src/main/java/creationalpatterns/factorymethod/logistics/Vehicle.java b/src/main/java/creationalpatterns/factorymethod/logistics/Vehicle.java new file mode 100644 index 0000000..c42aeac --- /dev/null +++ b/src/main/java/creationalpatterns/factorymethod/logistics/Vehicle.java @@ -0,0 +1,47 @@ +package creationalpatterns.factorymethod.logistics; + +import java.util.UUID; + +public class Vehicle { + private final UUID id; + private final VehicleType vehicleType; + private final int range; + private final int loadCapacity; + private boolean isOnRoad = false; + + public Vehicle(VehicleType vehicleType, int range, int loadCapacity) { + this.id = UUID.randomUUID(); + this.range = range; + this.loadCapacity = loadCapacity; + this.vehicleType = vehicleType; + } + + public UUID getId() { + return id; + } + + public int getRange() { + return range; + } + + public int getLoadCapacity() { + return loadCapacity; + } + + public VehicleType getVehicleType() { + return vehicleType; + } + + public boolean isOnRoad() { + return isOnRoad; + } + + public void setOnRoad(boolean b) { + this.isOnRoad = b; + } + + @Override + public String toString() { + return String.format("%s [%s] [%d km] [%d ton]", id, vehicleType, range, loadCapacity); + } +} diff --git a/src/main/java/creationalpatterns/factorymethod/logistics/VehicleType.java b/src/main/java/creationalpatterns/factorymethod/logistics/VehicleType.java new file mode 100644 index 0000000..3ab3893 --- /dev/null +++ b/src/main/java/creationalpatterns/factorymethod/logistics/VehicleType.java @@ -0,0 +1,6 @@ +package creationalpatterns.factorymethod.logistics; + +public enum VehicleType { + Truck, + Ship +}